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 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥15 vba参数转c++ SAFEARRAY
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题
  • ¥15 unity3d机械臂
  • ¥20 判断两个表是否完全相同
  • ¥20 工控机出现散热器转一下停一下这种情况,是什么原因造成的