netgen/NetgenEzFormsBundle

Form options overriding

vladislavs1321 opened this issue · 5 comments

Hey guys, will be cool to have possibility to override existing form items options, like disable label rendering, placeholders, select configurations.

@vladislavs1321 can you please specify more details about this one ? Thanks.

@MarioBlazek for example typical user registration form, with ez User object and some additional inputs like address, phone (text types), and by our design for example we don't need to show labels, but need to create placeholders. Of course, we can try to manage this with form theme, but will be so good to have possibility to have a possibility to set this from form object

Did you try to add placeholders like this @vladislavs1321 ?

In Controller:

/** @var FormBuilderInterface $formBuilder */
$formBuilder = $this->something();

$field = $formBuilder->get('email');
$options = $field->getOptions();
$type = $field->getType()->getName();
$options['attr']['placeholder'] = "My placeholder";

$formBuilder->add('email', $type, $options);

In template:

<div class="form-group">
    <div class="control-label">
        {{ form_label( form.email ) }}:
    </div>
    {{ form_widget( form.email, {'attr': {'class': 'form-control', 'placeholder': 'My placeholder'} } ) }}
    {{ form_errors(form.email)}}
</div>

@vladislavs1321 did this helped you ?

@MarioBlazek a little bit, so i decide to override Form\Type\FieldType\UserCreateType to put placeholders in this form type and bring not ideal solution in my twig

                            {% if form_child.vars.name != 'user_account' %}
                                {{ form_row( form_child, { attr: { class: 'form-control', placeholder: form_child.vars.label } } ) }}
                            {% else %}
                                {{ form_row( form_child, { attr: { class: 'form-control' } } ) }}
                            {% endif %}
                        {% endfor %}

to avoid using hardcoded FormBuilder children elements names