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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?