I am trying to add some validation for my registration form, but i can’t get it to work, so far i have added my User entity
Snippet.
class User {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $userid;
/**
* @ORM\Column(type="string", length=30)
*/
protected $username;
For this i’ve added a validation.yml to my bundle.
# src/Blomster/UserBundle/Resources/config/validation.yml
Blomster/UserBundle/Entity/User:
properties:
username:
- NotBlank: ~
- MinLength: { limit: 3, message: "Username is too short." }
- MaxLength: { limit: 15, message: "Username is too long." }
Using $form->isValid() always return true, so i tried to var_dump my form
'validation_groups' => null
'validation_constraint' => null
'constraints' =>
array (size=0)
...
The form itself is working great, it adds to my data base when submitted, do i need to add the constraints to the form somehow?
Added UserType.php
class UserType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username', null, array('label' => 'Username'));
$builder->add('email', 'email', array('label' => 'Email'));
$builder->add('password', 'repeated', array(
'type' => 'password',
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat password'),
));
}
public function getDefaultOptions(array $options) {
return array('data_class' => 'Blomster\UserBundle\Entity\User');
}
public function getName() {
return 'user';
}
}
You need to use a length limit :
here is the documentation.
In fact : you need to specify a limit because you can also set other options like a message and so on.