1

I have a function, in Symfony2, that creates a form. Then I load it using twig and everything works fine.

The problem is that I would like to add style to each label and each input, separately, and I can only add style to the whole form.

private function createEditForm(Client $entity)
{
    $form = $this->createForm(new ClientType(), $entity, array(
        'action' => $this->generateUrl('client_update', array('id' => $entity->getId())),
        'method' => 'PUT',

    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

Could I do it the way I'm doing it or do I have to rebuild the function so it doesn't create the form dynamically from an array?

Thank you.

1 Answer 1

4

You can customize the class rendered for example in your ClientType via something like this:

$builder->add('name' , 'text' , array( 'attr' => array( 'class' => 'nameInputField' ) ) );

Or you can add/override directly in your twig template as such:

{{ form_row(form.name, { 'attr': { 'class': 'nameInputField' }} ) }}

For advanced cases and re-usability refer to the form customization tutorial here:

http://symfony.com/doc/current/cookbook/form/form_customization.html

Sign up to request clarification or add additional context in comments.

3 Comments

:) Same answer, i deleted mine.
Thank you very much. The first solution solved the problem. As we say in spanish "Ole!"
Very well. Please kindly accept the answer, so other people can find this solution as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.