1

我有一个 Zend_Form 子类。一些元素被设置为属于数组。

class My_Form extends Zend_Form {
   public function __construct() {
       $elem = $this->createElement('text','PROJECT_NAME',                
           array(
              'required' => true
       ));
       $elem->setBelongsTo('project');
       $this->addElement($elem);

       $elem = $this->createElement(
                'text','PLANNED_END_DATE',
                array(
                    'required' => true
                )
        );
        $elem->setBelongsTo('project');
        $elem->addValidator(new Zend_Validate_Date(array('format'=>'yyyy-MM-dd')));        
        $this->addElement($elem);     
        //and so on
   }

} 

我有一个通用验证控制器,它确实创建表单并检查错误,并以 json 格式返回它们:

class ValidateController extends Zend_Controller_Action
{

    public function formAction()
    {
         $params = $this->_getAllParams();
         if (isset($params['_frm'])) {
            $formName   = detect_the_form_class($params['_frm']);
            if (class_exists($formName)) {
                $form = new $formName();
                if ($form instanceof Zend_Form) {
                    $result = $form->isValidPartial($params);

                    $messages = $form->getMessages();


                    $this->getResponse()
                            ->setHeader('Content-Type', 'application/json')
                            ->setBody(json_encode(array(
                                'result' => $result,
                                'messages' => $messages
                            )));
                } else {
                    $this->getResponse()->setHttpResponseCode(500);
                }
            }

         }    
    }
}

这个控制器非常适用于非数组表单,但我现在需要验证 hase 数组的表单,例如名称为“project[PROJECT_NAME]”的元素。

但是 $form->getMessages() 返回以元素的基本名称为索引的消息,没有数组前缀。

实际结果是:

{ result: false,
  messages: {
     PROJECT_NAME: {isEmpty: "Value is required"},
     PROJECT_END_DATE: {isEmpty: "Value is required"}
  }
}

我需要的结果是:

{ result: false,
  messages: {
     "project[PROJECT_NAME]": {isEmpty: "Value is required"},
     "project[PROJECT_END_DATE]": {isEmpty: "Value is required"}
  }
}

或类似的东西,所以我可以找到验证消息的元素。

有任何想法吗?

4

1 回答 1

0

对于子表单使用Zend_Form_Subform类:

class My_Form extends Zend_Form
{

    public function init()
    {
        $subForm = new Zend_Form_SubForm();

        $elem = $this->createElement('text', 'PROJECT_NAME', array(
            'required' => true
            ));
        $subForm->addElement($elem);

        $elem = $this->createElement('text', 'PLANNED_END_DATE', array(
            'required' => true
            ));

        $subForm->addElement($elem);
        $this->addSubForm($subForm, 'project');
    }
}

回复:

{
    "project":{
        "PROJECT_NAME":{"isEmpty":"Value is required and can't be empty"},
        "PLANNED_END_DATE":{"isEmpty":"Value is required and can't be empty"}
    }
}
  1. 对于表单配置,建议使用init()方法。

  2. 对于 json 响应,您可以使用内置操作助手: $this->_helper->json($form->getMessages());

于 2012-08-15T01:11:22.657 回答