form improvements

This commit is contained in:
2018-07-21 19:24:00 +02:00
parent 3242ae9cbe
commit a9ffd378a0
17 changed files with 200 additions and 174 deletions
@@ -49,7 +49,7 @@ class RegisterController extends Controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'name' => 'required|string|max:255|regex:/^[a-zA-Z0-9_.-]+$/',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
@@ -65,6 +65,7 @@ class RegisterController extends Controller
{
return User::create([
'name' => $data['name'],
'title' => $data['name'], // display name - by default, init to name
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
+18 -6
View File
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Table;
use Illuminate\Http\Request;
class TableController extends Controller
@@ -13,7 +14,7 @@ class TableController extends Controller
*/
public function create()
{
$exampleColSpec =
$exampleColumns =
"latin, string, Latin Name\n".
"common, string, Common Name\n".
"lifespan, int, Lifespan (years)";
@@ -24,21 +25,32 @@ class TableController extends Controller
"Patella vulgata, common limpet, 20";
return view('table.create',
compact('exampleColSpec', 'exampleData')
compact('exampleColumns', 'exampleData')
);
}
public function storeNew(Request $request)
{
$u = \Auth::user();
$this->validate($request, [
'table-name' => 'required',
'table-descr' => 'string|nullable',
'name' => 'required',
'title' => 'string|nullable',
'description' => 'string|nullable',
'license' => 'string|nullable',
'upstream' => 'string|nullable',
'col-spec' => 'required',
'row-data' => 'string|nullable',
'columns' => 'required',
'data' => 'string|nullable',
]);
// Check if table name is unique for user
$name = $request->get('name');
if ($u->tables()->where('name', $name)->exists()) {
return back()->withErrors([
'title' => "A table called \"$name\" already exists in your account.",
]);
}
return "Ok.";
}
}
+2 -1
View File
@@ -16,6 +16,7 @@ use Illuminate\Notifications\Notification;
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $name - unique, for vanity URL
* @property string $title - for display
* @property string $email - unique, for login and social auth chaining
* @property string $password - hashed pw
* @property Proposal[]|Collection $proposals
@@ -40,7 +41,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'title', 'email', 'password',
];
/**
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace App\View;
class CheckboxWidget extends Widget
{
public function checked($condition=true)
{
$this->value = (bool)$condition;
return $this;
}
}
+26 -10
View File
@@ -12,14 +12,17 @@ namespace App\View;
* @property-read string|null $help
* @property-read string|null $value
* @property-read string $attributes
* @property-read int $labelCols
* @property-read int $fieldCols
*/
class Widget
{
private $attributes = [],
protected $attributesArray = [],
$id, $name, $label, $value,
$viewName,
$help,
$style = [];
$labelCols, $fieldCols,
$styleArray = [];
public function __construct($viewName, $name, $label)
{
@@ -36,7 +39,7 @@ class Widget
'height', 'minHeight', 'maxHeight'
];
if (empty($args)) $args = [''];
if (empty($args)) $args = [null];
$arg = $args[0];
@@ -50,7 +53,7 @@ class Widget
$this->$method = $arg;
}
else {
$this->attributes[$method] = $arg;
$this->attributesArray[$method] = $arg;
}
return $this;
@@ -58,7 +61,15 @@ class Widget
public function css($prop, $val)
{
$this->style[$prop] = $val;
$this->styleArray[$prop] = $val;
return $this;
}
/** Apply a given layout (bootstrap grid, 12 cols total) */
public function layout($labelCols, $fieldCols)
{
$this->labelCols = $labelCols;
$this->fieldCols = $fieldCols;
return $this;
}
@@ -82,14 +93,19 @@ class Widget
public function compileAttribs()
{
// compile attribs string
$attribs_s = array_reduce(array_keys($this->attributes), function ($carry, $key) {
return $carry . ' ' . $key . '="' . e($this->attributes[$key]) . '"';
$attribs_s = array_reduce(array_keys($this->attributesArray), function ($carry, $key) {
if ($this->attributesArray[$key] === null) {
// simple attribs like 'required'
return $carry . ' ' . $key;
} else {
return $carry . ' ' . $key . '="' . e($this->attributesArray[$key]) . '"';
}
}, '');
// add a compiled list of styles
if (!empty($this->style)) {
$attribs_s .= 'style="'.trim(e(array_reduce(array_keys($this->style), function ($carry, $key) {
return $carry . $key . ': ' . $this->style[$key] . '; ';
if (!empty($this->styleArray)) {
$attribs_s .= 'style="'.trim(e(array_reduce(array_keys($this->styleArray), function ($carry, $key) {
return $carry . $key . ': ' . $this->styleArray[$key] . '; ';
}, ''))).'"';
}
+29 -4
View File
@@ -5,23 +5,48 @@ namespace App\View;
class WidgetFactory
{
private $labelCols = 3, $fieldCols = 8;
/** Configure layout used for future elements */
public function setLayout($labelCols, $fieldCols)
{
$this->labelCols = $labelCols;
$this->fieldCols = $fieldCols;
}
private function baseWidget($view, $name, $label)
{
return (new Widget($view, $name, $label))->layout($this->labelCols, $this->fieldCols);
}
public function text($name, $label)
{
return new Widget('input', $name, $label);
return $this->baseWidget('input', $name, $label);
}
public function password($name, $label)
{
return $this->baseWidget('input', $name, $label)->type('password');
}
public function number($name, $label)
{
return new Widget('number', $name, $label);
return $this->baseWidget('input', $name, $label)->type('number');
}
public function email($name, $label)
{
return new Widget('email', $name, $label);
return $this->baseWidget('input', $name, $label)->type('email');
}
public function checkbox($name, $label)
{
return (new CheckboxWidget('checkbox', $name, $label))
->layout($this->labelCols, $this->fieldCols);
}
public function textarea($name, $label)
{
return (new Widget('textarea', $name, $label))->minHeight('4em');
return $this->baseWidget('textarea', $name, $label)->minHeight('4em');
}
}