dongpanbo4727 2012-07-31 11:39
浏览 4
已采纳

Zend_Form中的验证取决于元素/字段值?

Let's imagine that we have form asking: "Are you Mr./Mrs.?" Depending on answer value we're going to implement futher validation.

For example, Mr > Validate favourite car model Mrs > Validate favourite flower

Is it ok to override isValid function? Maybe some examples of best practices?

  • 写回答

1条回答 默认 最新

  • drpsrvu85668 2012-07-31 17:28
    关注

    I would write a custom validator and use the provided $context variable.

    A short example

    Controller

    class MyController extends Zend_Controller_Action {
        public function indexAction() {
            $form = new Application_Form_Gender();
            $this->view->form = $form;
    
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($this->getRequest()->getPost())) {
                    /*...*/
                }
            }
        }
    }
    

    Form

    class Application_Form_Gender extends Zend_Form {
    
        public function init()
        {
            $this->addElement('radio', 'radio1', array('multiOptions' => array('m' => 'male', 'w' => 'female')));
            $this->getElement('radio1')->isRequired(true);
            $this->getElement('radio1')->setAllowEmpty(false);
    
            $this->addElement('text', 'textm', array('label' => 'If you are male enter something here');
            $this->getElement('textm')->setAllowEmpty(false)->addValidator(new MyValidator('m'));
    
            $this->addElement('text', 'textf', array('label' => 'If you are female enter something here'));     
            $this->getElement('textf')->setAllowEmpty(false)->addValidator(new MyValidator('f'));
    
            $this->addElement('submit', 'submit');
        }
    

    Validator

    class MyValidator extends Zend_Validate_Abstract {
        const ERROR = 'error';
        protected $_gender;
        protected $_messageTemplates = array(
            self::ERROR      => "Your gender is %gender%, so you have to enter something here",
        );
        protected $_messageVariables = array('gender' => '_gender');
    
        function __construct($gender) {
            $this->_gender = $gender;
        }
    
        function isValid( $value, $context = null ) {
            if (!isset($context['radio1'])) {
                return true;
            }
            if ($context['radio1'] != $this->_gender) {
                return true;
            }
            if (empty($context[sprintf('text%s', $this->_gender)])) {
                $this->_error(self::ERROR);
                return false;
            }
            return true;
        }
    }
    

    As you can see in this example, all the data provided in $form->isValid() is made available via the $context variable and with that you can perform any checks you like.

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)