1

次のZFフォーム要素があります。

    $this->addElement('text', 'price', array(
        'required'  => true,
        'label'     => 'Price:',
        'attribs'   => array(
            'title'     => 'Please enter the value of your artwork'),
        'filters'   => array('Currency'),
        'validators'    => array(
            array('NotEmpty', true, array(
                'messages' => array(
                    Zend_Validate_NotEmpty::IS_EMPTY =>
                        "You must enter your artworks price"))),
            array('Float', true, array(
                'messages'  => array(
                    Zend_Validate_Float::INVALID => 
                        "You must enter a valid price",
                    Zend_Validate_Float::NOT_FLOAT =>
                        "You must enter a valid price"))),
            array('GreaterThan', true, array(
                'min'   => 0.99,
                'messages'  => array(
                    Zend_Validate_GreaterThan::NOT_GREATER =>
                        "You must enter a value of £1.00 or more"))))
    ));

最後のバリデーターはエラーメッセージが設定されたZend_Validate_GreaterThanです。私の問題は、このバリデーターが失敗したときにエラーメッセージがフォームに表示されないことです。私がレンダリングするのは、空の順序付けされていないリストだけです!!

<ul>
    <li></li>
</ul>

zend_formによって出力されたメッセージを確認すると、エラーとメッセージが表示されます。

array(1) {
  ["price"]=>
  array(1) {
    ["notGreaterThan"]=>
    string(39) "You must enter a value of £1.00 or more"
  }
}

メッセージがフォームに表示されない理由を誰かが知っていますか?

よろしくお願いします

ギャリー

編集

私が使用している唯一のデコレータは、私が持っているフォーム上にフォームをレンダリングするためのビュースクリプトです。

$this->setDecorators(array(
    array('ViewScript', array('viewScript' => 'forms/add-item.phtml'))
));

とviewscript自体。

$attribFilterObj = new Freedom_Zend_Filter_HtmlAttribs();
$attribs = $attribFilterObj->filter($this->element->getAttribs());
?>
<form <?php echo $attribs; ?>>
<dl>
    <?php echo $this->element->ArtworkTitle->title->render(); ?>
    <?php echo $this->element->ArtworkDescription->description->render(); ?>
    <?php echo $this->element->price->render(); ?>
    <?php echo $this->element->Genres->render(); ?>
    <?php echo $this->element->image->render(); ?>
    <?php echo $this->element->add->render(); ?>
</dl>
</form>

編集

var_dumpの出力は次のとおりです。

array(5) {
  ["Zend_Form_Decorator_ViewHelper"]=>
  object(Zend_Form_Decorator_ViewHelper)#157 (6) {
    ["_buttonTypes":protected]=>
    array(3) {
      [0]=>
      string(24) "Zend_Form_Element_Button"
      [1]=>
      string(23) "Zend_Form_Element_Reset"
      [2]=>
      string(24) "Zend_Form_Element_Submit"
    }
    ["_helper":protected]=>
    NULL
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(0) {
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Errors"]=>
  object(Zend_Form_Decorator_Errors)#158 (4) {
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(0) {
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Description"]=>
  object(Zend_Form_Decorator_Description)#159 (6) {
    ["_escape":protected]=>
    NULL
    ["_placement":protected]=>
    string(6) "APPEND"
    ["_tag":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(2) {
      ["tag"]=>
      string(1) "p"
      ["class"]=>
      string(11) "description"
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_HtmlTag"]=>
  object(Zend_Form_Decorator_HtmlTag)#160 (7) {
    ["_encoding":protected]=>
    NULL
    ["_placement":protected]=>
    NULL
    ["_tag":protected]=>
    NULL
    ["_tagFilter":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(2) {
      ["tag"]=>
      string(2) "dd"
      ["id"]=>
      array(1) {
        ["callback"]=>
        array(2) {
          [0]=>
          string(22) "Zend_Form_Element_Text"
          [1]=>
          string(16) "resolveElementId"
        }
      }
    }
    ["_separator":protected]=>
    string(2) "
"
  }
  ["Zend_Form_Decorator_Label"]=>
  object(Zend_Form_Decorator_Label)#161 (6) {
    ["_placement":protected]=>
    string(7) "PREPEND"
    ["_tag":protected]=>
    NULL
    ["_tagClass":protected]=>
    NULL
    ["_element":protected]=>
    NULL
    ["_options":protected]=>
    array(1) {
      ["tag"]=>
      string(2) "dt"
    }
    ["_separator":protected]=>
    string(2) "
"
  }
}
4

3 に答える 3

1

少し古いですが、このサイトをチェックしてくださいhttp://zendguru.wordpress.com/2008/12/04/handling-zend-framework-form-error-messages/

自分の好きなようにエラーメッセージを取得してから印刷する必要があると思います

$errorsMessages = $this->form->getMessages();
于 2012-06-09T13:49:53.807 に答える
1

これは、要素のフォーム デコレータに「エラー」を含めるのを忘れたことが原因である場合があります。カスタム デコレータを使用している場合は、まずそれを確認してください。

于 2012-06-06T13:10:20.923 に答える
0

私は同じ問題を抱えていました。あなたと同じようにViewScriptのみを使用していました。これで解決しました:

($this is in the form class I created)
$this->setDecorators(array(
        array('ViewScript', 
              array('viewScript' => '/formElementViewscripts/editRoughDraft/_form.phtml')),
        'Form'
    ));

これらのメッセージを受信できるようにする場合は、デフォルトの「フォーム」デコレータもフォームに追加する必要があります。コードを調べてみましたが、正確な理由を特定できませんでした。

于 2013-06-01T02:31:34.550 に答える