0

I am using the SF2 forms and validator bundle by themselves. Is there anyway to generate HTML attributes off annotations that are created with the symfony validator? For example:

Convert this -

/**
 * @var
 * @Assert\Length(min="2", max="2")
 */
protected $state;

into the HTML code

<input type="text" maxlength="2" ... >

Typically, I know that is set in SF2 by setting a size for the string in a Doctrine annotation, but I am using Aura.SQL to handle interaction with the database.

Thanks for any information!

2 Answers 2

1

Well, according to the documentation, the "guessing" is activated when you omit the second argument to the add() method (or if you pass null to it).

If you use this feature, Symfony will try to guess the "type" for a field and the correct values of a number of field options based on validation metadata (and doctrine info).

So, in theory, it should give you the expected output if you omit the second argument or pass null to it (it would render the html5 attributes in the outputted input).

If for any reason it doesn't work, you can always force its rendering in the add method (it also overrides any guessing if it existed):

->add('state', null, array('attr' => array('minlength' => 2,'maxlength' => 2)))

or even in the twig template:

{{ form_widget(form.state, {'attr': {'maxlength' : 2, 'minlength' : 2 } }) }}
Sign up to request clarification or add additional context in comments.

1 Comment

I spent the rest of the day, and learned that the Validator library has its own guesser. I had to include it as a form extension, and that reduced the typing. After I did that I did what you recommended, setting the second argument as NULL, it worked! Thanks!
1

I think that whate you are looking for does not exists. By the way, you can implement it by creating your own form type guesser

Comments

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.