Reputation: 318
I have an entity User, which has $password with @Assert\Length(min=6)
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Assert\Length( min=6, minMessage="Password is too short (min 6 symbols)" )
*/
private $password;
I'm trying to generate a Login form using createFormBuilder
$LoginForm=$this->createFormBuilder(null,['data_class'=>User::class])
->add('email')
->add('password', ??? PasswordType::class ??? )
->getForm();
If I don't set the 'PasswordType::class' for my second field (in createFormBuilder), it generates HTML with "type='text'" field. The @Assert\Lenth 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 @Assert\Length) =|
===
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">
Upvotes: 0
Views: 313
Reputation: 441
The visibility of pattern option does not depend on form type field. If omit type field add
method and assume that you already defined constraints on entity then constraint attributes will be available on html side.
/**
* @var string $userName
* @ORM\Column(type="string")
* @Assert\Length(min=6, minMessage="Username is too short (min 6 symbols)")
*/
private $userName;
So, if you declare your form type as follow:
$LoginForm=$this->createFormBuilder(null,['data_class'=>User::class])
->add('email')
->add('username')
->getForm();
Then you can see pattern attribute on input. But if you add any FormType
field to ->add('username', TextType::class)
then pattern attribute will not available anymore. So, to add constraint to your form, add constraint directly to your form type.
->add('userName', TextType::class, [
'constraints' => [
new Length(['min' => 6]),
],
]);
For more information please have a look Symfony doc.
Upvotes: 0
Reputation: 591
You can use this alternative and by pass annotation
->add('password', PasswordType::class, [
'constraints' => [
new Length(['min' => 3]),
],
)
Upvotes: 1