question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use FormField classes for better API and reduce redundant code

See original GitHub issue

Right 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:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tabacitucommented, Jul 20, 2016

@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:

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('category_id')->unsigned();
    $table->string('title');
    $table->string('slug')->default('');
    $table->text('content');
    $table->string('image')->nullable();
    $table->enum('status', ['PUBLISHED', 'DRAFT'])->default('PUBLISHED');
    $table->date('date');
    $table->boolean('featured')->default(0);
    $table->timestamps();
    $table->softDeletes();
});

turns into:

$this->crud->setModel("App\Models\Article");
$this->crud->setRoute('admin/article');
$this->crud->setEntityNameStrings('article', 'articles');

$this->crud->addFields('create', function (CrudForm $form) {
    $form->increments('id');
    $form->select('category_id', 'App\Models\Category');
    $form->string('title');
    $form->string('slug');
    $form->text('content');
    $form->browse('image');
    $form->enum('status');
    $form->date('date');
    $form->boolean('featured');
});

What do you think?

@cristiantone @Ghitu - what do you guys think?

0reactions
tabacitucommented, Oct 12, 2016

Yup, thank you. We’ll keep this in mind for v4 - the nearest term when we can consider a syntax change.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found