kristijanhusak/laravel-form-builder

Allow setting default classes on a per-form basis

Open

#217 opened on Feb 16, 2016

 (1 comment) (2 reactions) (0 assignees)PHP (305 forks)github user discovery
help wanted

Repository metrics

Stars
 (1,715 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Currently, default classes are set and read from the laravel-form-builder config file. This works fine most of the time, but I've come across several cases where I would like to change these for a particular form, but don't want to have to manually specify them for each form field. I propose adding another layer to pull default values from, in the form's $formOptions attribute. I think this would just be a matter of checking for a value first in FormField::allDefaults, and if found using that instead of the config file value. This way, you could do something like this:

class SomeForm extends Form
{
    protected $formOptions = [
        'defaults' => [
            'label_class'   => 'control-label',
            'wrapper_class' => 'col-sm-2',
        ],
    ];

    public function buildForm()
    {
        $this->add('inline-sub-form', 'form', [
            'class'        => $this->formBuilder->plain([
                'defaults' => [
                    'label_class'   => 'fancy-control-label',
                    'wrapper_class' => 'col-sm-11',
                ],
            ])->add('asdf', 'number'),
        ]);
    }
}

Thoughts? I thought I would throw this out there first, but I can do a PR.

Contributor guide