我正在尝试为我的注册表单添加一些验证,但我无法让它工作,到目前为止我已经添加了我的用户实体
片段。
class User {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $userid;
/**
* @ORM\Column(type="string", length=30)
*/
protected $username;
为此,我在我的包中添加了一个validation.yml。
# 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." }
使用 $form->isValid() 总是返回 true,所以我尝试 var_dump 我的表单
'validation_groups' => null
'validation_constraint' => null
'constraints' =>
array (size=0)
...
表单本身运行良好,提交时它会添加到我的数据库中,我是否需要以某种方式将约束添加到表单中?
添加了 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';
}
}