dongyanfeng0546 2015-02-04 15:44
浏览 33
已采纳

Symfony 2.3 - 我的包中的Validator.yml无法正常工作

I have 2 bundles, 1 works with the validation.yml file and one does not.

Both bundles are set up exactly the same, i have googled high and low and i cannot seem to understand why.

I have created a form type here:

<?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 function 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;
}

}

This is my validation.yml in bundle/Resources/config:

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

This is my controller that builds the form from the class and renders it renders fine:

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

?>

In app/config/config.yml i have these params set for validation:

    validation:      { enabled: true, enable_annotations: false }

Now when I submit the form with no data the request hits the controller and the method

$form->isValid();

is called, this returns true.

This should return false as my constraints in my validation.yml file do not allow blank fields to be processed and should trigger the form to render the field errors within the template.

I am clearly missing something here, Any help would be greatly appreciated.

Thanks

Adam

  • 写回答

1条回答 默认 最新

  • douzhang3898 2015-02-04 15:51
    关注

    You should not validate your UserType, but the User object itself. Also the second argument of createForm() needs to be a User object.

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

    validation.yml:

    Brs\UserBundle\Entity\User:
        properties:
           # constraint list
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘