Use FormField classes for better API and reduce redundant code
See original GitHub issueRight now a lot of field types have the same template, but only the type="text | date | email "
is different.
If you want to change the layout of the fields, you need to override a lot of templates (for example if you want a .form-horizontal).
What about something like this:
$fields = new FieldList(
new EmailField('email', 'Email'),
new DropdownField('status', 'Status', ['yes', 'no']),
new TextField('name', 'Name') // 'Name' optional, so it will be taken from 'name'
);
instead of
$fields = [
[
'type' => 'email',
'label' => 'Email',
'name' => 'email',
],
[
'type' => 'select',
'label' => 'Status',
'name' => 'status',
'options' => ['yes', 'no']
],
[
'type' => 'text',
'label' => 'Name',
'name' => 'name',
]
];
For the EmailField you don’t need a seperate template and the API for the Dropdown Field is perfeclty clear without need to refer to documenation when you have something like this (quick boiler plating) :
class DropdownField extends FormField
{
protected $options = [];
protected $type = 'select';
public function __construct($name, $label, $options)
{
$this->options = $options;
parent::__construct($name, $label);
}
}
class EmailField extends TextField
{
protected $type = 'email';
}
class TextField extends FormField
{
public function view()
{
return view('fields.' . $this->template, ['field' => $this]);
}
}
abstract class FormField
{
protected $name;
protected $label;
protected $attributes = [];
protected $classes = [
'holder' => ['form-group'],
'label' => ['control-label'],
'field' => ['form-control'],
];
protected $template = 'text';
protected $type = 'text';
public function __construct($name, $label)
{
$this->name = $name;
$this->label = $label;
}
public function setAttribute($attr, $value)
{
$this->attributesp[$attr] = $value;
return $this;
}
public function addClass($class, $target = 'field') // ? field and holder?
{
$this->classes[$target][] = $class;
return $this;
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Creating forms from models - Django documentation
The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute.
Read more >Extend marketing forms using code - Dynamics 365
Extend marketing forms with JavaScript to apply custom business logic in Dynamics 365 Marketing.
Read more >Form field - Angular Material
API reference for Angular Material form-field ... the actual MatPrefix class which could cause unnecessary retention of the class and its directive metadata ......
Read more >Guided Task - Encapsulation and Reducing Redundancy
Another way to minimize redundancy is to create one path of construction for a class. In Java, a class can have multiple constructors...
Read more >How do I add a custom validation handler to an existing form ...
explains to implement hook_form_alter() and then add your validation handler][1] to the $form['#validate'] array, but in Drupal 8 forms are classes. Validation ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@axyr , you’ve got me thinking. Maybe the best way to write the CRUD code is the most familiar way. The Laravel way. So wouldn’t it make sense to make it as close as possible to the Schema builder?
Ideally, you should be able to copy-paste your schema and make a few changes. So that:
turns into:
What do you think?
@cristiantone @Ghitu - what do you guys think?
Yup, thank you. We’ll keep this in mind for v4 - the nearest term when we can consider a syntax change.