doubingqi5829 2014-01-13 18:47
浏览 111
已采纳

Symfony2:$ form-> isValid()始终为true

I have problem with form validation in Symfony2:

part of form class:

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('name', 'text',array("label"=> "Nazwa",'max_length' => 255, 'required'=>true))
            ->add('url', 'url',array("label"=> "Adres strony",'max_length' => 255, 'required'=>true))
            ->add('logo', 'file',array("label"=> "Logo",'max_length' => 255))
            ->add('Dodaj', 'submit');
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            'data_class' => 'Diliges\DiligesBundle\Entity\Company',
            'error_bubbling' => true
        ));
    }
}

part of Controller method:

public function addAction(Request $request){
        $company = new Company();
        $form = $this->createForm(new CompanyType(), $company);
        $form->handleRequest($request);

        if ($form->isValid()) {

            try{
                $company->setActive(false);
                return $this->redirect($this->generateUrl('add_company_thx'));
            }catch(PDOException $e){

            }

        }
        ...

Company entity

properties:
        name:
          - NotBlank: ~
          - MinLength: 3
        url:
          - NotBlank: ~

Im trying, and trying, and still $form->isValid() return true, always. Even when I submit empty form.

  • 写回答

1条回答 默认 最新

  • doumie6223 2014-01-13 18:54
    关注

    You must have your validation configuration in the wrong spot. Look at the docs for where the yml config is placed. Basics of Validation

    They put the config file in src/Acme/BlogBundle/Resources/config/validation.yml. Also, your format of the config is not correct. You are missing the entity definition.

    Diliges\DiligesBundle\Entity\Company:
        properties:
            name:
              - NotBlank: ~
              - MinLength: 3
            url:
              - NotBlank: ~
    

    Another option that could be making this not work is if you have enabled a different kind of validation (like annotations). Make sure yml is enabled

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。