Symfony 2.3-我的捆绑包中的Validator.yml不工作


Symfony 2.3 - Validator.yml within my bundle not working

我有两个捆绑包,一个可以使用validation.yml文件,另一个不可以。

两个捆绑包的设置完全相同,我在谷歌上搜索了一下,似乎不明白为什么。

我在这里创建了一个表单类型:

<?php
namespace Brs'UserBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
class Usertype extends AbstractType
{
    protected $fname;
    protected $lname;
    protected $email;
    protected $mobile;
    protected $active;
    protected $mentor;
    protected $initialized;
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('fname','text',array('label'=>'First Name'))
        ->add('lname','text',array('label'=>'Last Name'))
        ->add('email','email',array('label'=>'Email'))
        ->add('mobile','text',array('label'=>'Mobile'))
        ->add('active','choice',array(
            'label'=>'Active?',
            'choices'=>array('0'=>'No','1'=>'Yes'),
            'expanded'=>true,
            'multiple'=>false
        ))
        ->add('mentor','choice',array(
            'label'=>'Mentor?',
            'choices'=>array('0'=>'No','1'=>'Yes'),
            'expanded'=>true,
            'multiple'=>false
        ))
        ->add('Add Player?','submit');
}
public function getName()
{
    return 'user';
}

公共函数setFname($fname){$this->fname=$fname;

    return $this;
}
public function getFname()
{
    return $this->fname;
}
public function setLname($lname)
{
    $this->lname = $lname;
    return $this;
}
public function getLname()
{
    return $this->lname;
}
public function setEmail($email)
{
    $this->email = $email;
    return $this;
}
public function getEmail()
{
    return $this->email;
}
public function setMobile($mobile)
{
    $this->mobile = $mobile;
    return $this;
}
public function getMobile()
{
    return $this->mobile;
}
public function setActive($active)
{
    $this->active = $active;
    return $this;
}
public function getActive()
{
    return $this->active;
}
public function setMentor($mentor)
{
    $this->mentor = $mentor;
    return $this;
}
public function getMentor()
{
    return $this->mentor;
}
public function setInitialized($initialized)
{
    $this->initialized = $initialized;
    return $this;
}
public function getInitialized()
{
    return $this->initialized;
}

}

这是我在bundle/Resources/config:中的validation.yml

Brs'UserBundle'Form'Type'UserType:
properties:
    fname:
        - NotBlank: ~
        - Length:
            min: 3
    lname:
        - NotBlank: ~
        - Length:
            min: 3
    email:
        - NotBlank: ~
        - Length:
            min: 3

这是我的控制器,它从类中构建表单并使其呈现良好:

<?php
namespace Brs'UserBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Component'HttpFoundation'Request;
// use Symfony'Component'HttpFoundation'Response;
// use Brs'UserBundle'Entity'User;
use Brs'UserBundle'Form'Type'UserType;
class UserController extends Controller{
    public function indexAction(Request $request){
        $user = new UserType();

        $form = $this->createForm(new UserType(),$user);
        $form->handleRequest($request);
        if($form->isValid()){
            echo 'yes';
        }
        //print_r($user);
        return $this->render(
            'BrsUserBundle:User:userForm.html.twig',
            array(
                'title'=>"Add Player",
                'form'=>$form->createView()
            ));
    }
}

?>

在app/config/config.yml中,我设置了以下参数进行验证:

    validation:      { enabled: true, enable_annotations: false }

现在,当我提交没有数据的表格时,请求会击中控制器和方法

$form->isValid();

则返回true。

这应该返回false,因为我的validation.yml文件中的约束不允许处理空白字段,并且应该触发表单在模板中呈现字段错误。

我显然错过了什么,如有任何帮助,我们将不胜感激。

感谢

Adam

您不应该验证UserType,而应该验证User对象本身。另外,createForm()的第二个参数需要是User对象。

$user = new User();
$form = $this->createForm(new UserType(),$user);

validation.yml:

Brs'UserBundle'Entity'User:
    properties:
       # constraint list