【问题标题】:Custom error message in Zend Form is not showingZend Form 中的自定义错误消息未显示
【发布时间】:2013-12-04 08:52:09
【问题描述】:

我编写了一些代码来向 zend 框架元素添加自定义错误消息,问题是错误没有出现。我有以下相关代码部分:(我可以在 else 中添加错误消息)。

        if($this->_request->isPost() && $form->isValid($this->_request->getPost())){
            $afgehandeldValue = $form->getValue('afgehandeld');
            if($afgehandeldValue == 0)
            {
                $conversationValues['conversation_handled'] = 0;
            }
            else
            {
                $checkFollowUp = $form->getValue('opvolging');
                if($checkFollowUp == 0)
                {
                    $conversationValues['conversation_handled'] = 1;
                }
                else
                {
                    $form->getElement('opvolging')
                    ->addError('Je mag niemand opgeven voor een opvolgend gesprek als afgehandeld is aangevinkt.')
                    ->markAsError();
                }
            }

【问题讨论】:

    标签: php zend-framework frameworks


    【解决方案1】:

    这应该可以工作(markAsError() 不需要,因为addError() 会自动将此元素标记为无效)。也许你的条件不正确?尝试在设置错误之前回显一些东西,看看这部分代码是否真的在运行。

    【讨论】:

    • 我回显了一些我确实得到输出的东西,我摆脱了 markAsError 但仍然没有成功。
    【解决方案2】:

    假设您使用的是 zf1 您可以在 Zend 表单本身中添加自定义错误消息,而无需像您一样在控制器中执行此操作。

     $this->addElement('text', 'text', array(         
            'required'   => true,
            'validators' => array(              
                array('NotEmpty',true,'options' => array('messages' => 'please enter some text')),
            ),
            'label'      => 'Text Element:',
            'placeholder'=>"Insert Text",                                     
        ));
    

    【讨论】:

      【解决方案3】:

      如果您使用 Zend 框架 1.X 并希望特定的表单元素显示错误,您可以像这样在表单元素中执行此操作。

       $opvolging  = new Zend_Form_Element_Text('opvolging',
                                  array('placeholder' => 'opvolging')
                           );               
      
      
      $opvolging          ->setLabel('opvolging : ')
                        ->setRequired(true)
                           ->addFilter('StripTags')
                       ->setAttrib('accept-charset', 'utf-8')
                       ->addFilter('StringTrim')
                       ->addValidator('NotEmpty')
                           ->getValidator('NotEmpty')->setMessage('Je mag niemand opgeven voor een opvolgend gesprek als afgehandeld is aangevinkt.');
      

      在你的动作控制器中

       $opvolging =   $form->getValue('opvolging');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-20
        • 2020-05-22
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 2015-12-24
        相关资源
        最近更新 更多