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

Symfony 2.3 - Validator.yml within my bundle not working

我有 2 个包,1 个适用于 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';
}

public 函数 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

这是我的控制器,它从 class 构建表单并渲染它:

<?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();

被调用,这个return是真的。

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

我显然在这里遗漏了一些东西,将不胜感激任何帮助。

谢谢

亚当

您不应验证您的 UserType,而应验证 User 对象本身。 createForm() 的第二个参数也需要是一个用户对象。

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

validation.yml:

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