How to build DRY twig forms?

225 Views Asked by At

I am building an application with Symfony 2 and was wondering how I would best build DRY forms. So essentially I want to have one Template e.g. form.html.twig which I then use for creation as well as editing of the underlining entity. This implies, that I change the text on the submit button so that it say on creationg "Create" and on editing "Edit". How would I accomplish something like this best?

At the moment I have something like this:

My FormType:

class GroupType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('name', 'text')
    ->add('description', 'textarea')
    ->add('status', 'hidden', array(
        'data' => 0
    ))
    ->add('createAndFinish', 'submit')
    ->add('createAndAddUsers', 'submit');

}

public function getName() {

    return 'group';

}

}

My form in the according template:

{{ form_start(groupForm) }}

    {% if not groupForm.vars.valid %}
        <div class="alert alert-danger">
            {{ form_errors(groupForm) }}
        </div>
    {% endif %}

    <div class="form-group">

        {{ form_label(groupForm.name) }}
        {{ form_errors(groupForm.name) }}
        {{ form_widget(groupForm.name, {'attr': {'class': 'form-control'}}) }}

    </div>

    <div class="form-group">

        {{ form_label(groupForm.description) }}
        {{ form_errors(groupForm.description) }}
        {{ form_widget(groupForm.description, {'attr': {'class': 'form-control'}}) }}

    </div>

        {{ form_widget(groupForm.createAndFinish, {'attr': {'class': 'btn btn-primary'}}) }}

        {{ form_widget(groupForm.createAndAddUsers, {'attr': {'class': 'btn btn-primary'}}) }}



{{ form_end(groupForm) }}

What I essentially want is something like this:

{% if (formType == 'create') %}
    {{ form_widget(groupForm.createAndFinish, {'attr': {'class': 'btn btn-primary'}}) }}
    {{ form_widget(groupForm.createAndAddUsers, {'attr': {'class': 'btn btn-primary'}}) }}
{% elseif (formType == 'edit') %}
    {{ form_widget(groupForm.createAndFinish, {'attr': {'class': 'btn btn-primary'}, 'value': 'Edit'}) }}
{% endif %}

But is there any other sollution which is more ellegant and has less code in the template? Thanks for your thoughts!

2

There are 2 best solutions below

3
qooplmao On BEST ANSWER

You can use if's to check if an item is defined and then display it.

{% if groupForm.createAndFinish is defined %}
    {{ form_widget(groupForm.createAndFinish, {'attr': {'class': 'classes'}}) }}
{% endif %}
0
AlterPHP On

How you can code logic in GroupType :

class GroupType extends AbstractType {

private $action;

public function __construct($action) {
    $this->action = $action;
}

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('name', 'text')
    ->add('description', 'textarea')
    ->add('status', 'hidden', array(
        'data' => 0
    ))
    ->add('createAndFinish', 'submit');

    if ('create' == $this->action) {
        $builder->add('createAndAddUsers', 'submit');
    }

}

public function getName() {

    return 'group';

}

}

Then just pass $action parameter to GroupType constructor from your Controller.