Symfony CreateFormBuilder ignores my Entity @AssertLength

I have an entity User, which has $password with @AssertLength(min=6)

* @var string The hashed password
* @ORMColumn(type="string")
* @AssertLength( min=6, minMessage="Password is too short (min 6 symbols)" )
private $password;

I’m trying to generate a Login form using createFormBuilder

->add('password', ??? PasswordType::class ??? )

If I don’t set the ‘PasswordType::class’ for my second field (in createFormBuilder), it generates HTML with “type=’text'” field. The @AssertLenth seems to work fine (pattern=6, presents)

<input type="text" id="form_password" name="form[password]" required="required" pattern=".{6,}">

If I do set the ‘PasswordType::class’, the HTML field becomes “type=’password'”, but it completely forgets about minLength constraint…

<input type="password" id="form_password" name="form[password]" required="required">

So, at this point I have to choose between Text field (with minLength pattern) or Password field (which ignores my entity @AssertLength) =|


UPD: about setting the constraints directly in the ‘add()’

add('userName', TextType::class, ['constraints' => [new Length(['min' => 6])]])

generates empty (well, no constraints there) HTML aswell:

<input type="text" id="form_userName" name="form[userName]" required="required">

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *