Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding default config doesn't work (radio) #537

Open
yanvv2 opened this issue Jul 8, 2019 · 14 comments
Open

Overriding default config doesn't work (radio) #537

yanvv2 opened this issue Jul 8, 2019 · 14 comments

Comments

@yanvv2
Copy link

yanvv2 commented Jul 8, 2019

return [
    'defaults' => [
        'wrapper_class' => 'form-group row',
        'wrapper_error_class' => '',
        'label_class'   => 'col-4 col-form-label text-right',
        'field_class'   => 'form-control w-75',
        'field_error_class' => 'is-invalid',
        'help_block_class' => 'form-text text-muted',
        'error_class' => 'invalid-feedback',
        'radio' => [
            'wrapper_class' => 'form-check',
            'field_class'   => 'form-check-input',
            'label_class'   => 'form-check-label',
            'choice_options' => [
                'wrapper_class' => 'custom-control custom-radio',
                'label_class'   => 'custom-control-label',
                'field_class'   => 'custom-control-input',
            ],
        ],
    ],

    'form' => 'fields.form',

    'custom_fields' => [
        \App\Common\UI\Http\View\Form\Fields\DatetimeType::DATETIME => \App\Common\UI\Http\View\Form\Fields\DatetimeType::class,
    ],
];

Stepping through the code with a debugger shows that these default values are being picked up, however it is not working for the radio button

@kristijanhusak
Copy link
Owner

How are you setting up the radio button?

@yanvv2
Copy link
Author

yanvv2 commented Jul 8, 2019

In my form class I create multiple radio buttons like this ->

$selectOptions = [
            0 => 'No',
            2 => 'Yes',
        ];

        $this->addField(
            new ChoiceType(
                'shipment',
                Field::CHOICE,
                $this,
                [
                    'label'    => __('Shipment'),
                    'choices'  => $selectOptions,
                    'expanded' => true,
                    'rules'    => [
                        'required',
                        Rule::in(array_keys($selectOptions)),
                    ],
                ]
            )
        );

And call them to the view ->

<fieldset class="form-group">
    <div class="row">
        {!! form_label($form->shipment) !!}
        <div class="col-8">
            {!!
                form_widget(
                    $form->shipment,
                    [
                        'choice_options' => [
                            'attr' => [
                                'class' => 'form-check-input'
                            ],
                            'label_attr' => [
                                'class' => 'form-check-label'
                            ],
                            'wrapper' => ['class' => 'form-check form-check-inline']
                        ]
                    ]
                )
            !!}
        </div>
    </div>
</fieldset>

@kristijanhusak
Copy link
Owner

Why are you using addField method when there's add method that simplifies it?

@yanvv2
Copy link
Author

yanvv2 commented Jul 8, 2019

Tested using add but the issue remains, classes defined in the defaults are not being applied

@rudiedirkx
Copy link
Collaborator

rudiedirkx commented Jul 8, 2019

Maybe use class_append instead of class in the view? Not sure it works there.

@rudiedirkx
Copy link
Collaborator

rudiedirkx commented Jul 8, 2019

(I love how the no/yes options are 0/2 and not 0/1 =) Was 1 taken?)

@yanvv2
Copy link
Author

yanvv2 commented Jul 9, 2019

Hi, classes that I define in the widget inside the view are working :) It's the defaults for 'radio' in config/laravel-form-builder.php that don't seem to get applied.

(Sidenote: the 'yes' option gets value 2 because I have to build up a binary value with multiple radio buttons due to the pre-existing database structure, so to answer that question: yes 1 was already taken :) )

@rudiedirkx
Copy link
Collaborator

rudiedirkx commented Jul 9, 2019

Yes, that's why you should use class_append. It appends the classes (in the view) to the existing (the defaults) classes. Did you try? In the view, replace all 'class' with 'class_append'. Some might not work. wrapper should. Let us know.

@yanvv2
Copy link
Author

yanvv2 commented Jul 16, 2019

Using class_append still doesn't apply the defaults.
In the view, 'wrapper' does work but others ('attr' and 'label_attr') don't as you mentioned.

@reyezJelle
Copy link

Any progress on this? I'm running into exactly the same issue.

@alexxxxkkkk
Copy link

alexxxxkkkk commented Dec 28, 2020

I have the same issue.

Default configurations are never applied to the choices field item.

<?php

return [
    'defaults' => [
        ....
        'radio' => [
            'choice_options' => [
                'field' => 'form-radio',
            ],
        ],
    ....
    ],
];

Looking into the source code, you can see that these configurations will never be read ($this->type will always have a "choice" value, not "radio").

src/Kris/LaravelFormBuilder/Fields/ChoiceType.php

<?php

namespace  Kris\LaravelFormBuilder\Fields;

class ChoiceType extends ParentType
{
    ...
    protected function setDefaultClasses(array $options = [])
    {
        ...
        $choice_wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.wrapper_class', '');
        $choice_label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.label_class', '');
        $choice_field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.field_class', '');
        ...
    }
    ...
}

@rudiedirkx
Copy link
Collaborator

rudiedirkx commented Dec 29, 2020

I don't use the choice types. If you make a PR, all my bonus points are yours.

@stfr
Copy link

stfr commented May 26, 2021

For update :

It seems that the configuration (or the code) isn't correct.
For choices field, it will called defaults.choice.checkbox_wrapper_class or defaults.choice.radio_wrapper_class configuration.

So, we should have in the config file :

//in defaults
'choice' => [
            'checkbox_wrapper_class' => 'field_wrapper_checkbox', //custom wrapper class for checkbox choices
            'radio_wrapper_class' => 'field_wrapper_radio' //custom wrapper class for radio choices
        ],

or maybe edit the ChoiceType::setDefaultClasses() method...

@rudiedirkx
Copy link
Collaborator

@stfr That sounds smart. You should PR that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants