duanmo6937 2014-07-10 14:12
浏览 43

Zend 2 Form Fieldset可选验证

I'm using a lot of nested Fieldset and was wondering if is it possible to set a required=false to a specific fieldset in order to not throw an error if there is any in it (and keep my form validating) ?

For example this is my UserFieldset :

-> {User Fieldset}
-----> Name
-----> Email address
-----> [other user fields...]
-----> {Avatar Fieldset}
----------> File
----------> [other avatar fields...]
-----> {other fieldsets...}

In my situation I want sometimes to force my user to add an avatar and sometimes not..

When I want to force there is no problem, my input file in my Avatar fieldset throws an error and blocks the validation process.

But when I want my user to simply register and eventually set its avatar, how can I do it ?

Thank you for your help.

  • 写回答

1条回答 默认 最新

  • dongwu9063 2014-07-10 14:48
    关注

    You can modify the elements that are validated using the setValidationGroup() method.

    The documentation states

    Zend\Form provides a proxy method to the underlying InputFilter‘s setValidationGroup() method, allowing us to perform [validation] on only a subset of form elements

    This could be done within the controller or perhaps a new factory; for example:

    // Module.php
    public function getFormElementConfig()
    {
      return array(
          'factories' => array(
              'MyModule\Form\AvatarRegistrationFieldset' => function($fem) {
    
                 $fieldset = new Form\AvatarFieldset();
    
                 // Include the name of the elements you WANT to be validated
                 $fieldset->setValidationGroup('foo', 'file');
    
                 return $fieldset;
              }
          ),
      );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路