2012-04-16 9 views
0

朝、Zend Form Validator foreach

私はZend Form Validatorに奇妙な問題があります。私が見るエラーメッセージを出力したいとき:ArrayArray。

マイコード:

<?php 

// Load sms request form 
$smsRequestForm = new Application_Form_Sms_Request(); 

// Form posted? 
if ($this->getRequest()->getMethod() != 'POST') { 
    // Show the form 
    $this->view->showForm = true;    
    $this->view->smsRequestForm = $smsRequestForm; 
} elseif (!$smsRequestForm->isValid($_POST)) { 
    // Show the form and output the validation errors 
    $this->view->showForm = true;    
    $this->view->smsRequestForm = $smsRequestForm; 

    // Loop through the error messages 
    foreach($smsRequestForm->getMessages() as $message) 
    { 
     echo $message; 
    } 
} else { 

} 

私は、ドキュメントを読んで、そのエコー$メッセージを学びました。 errormessageをプレーンテキストで出力する必要があります。

foreach($ smsRequestForm-> getMessages()を$ key => $ message)とする。私の問題は解決しません。

誰かが間違っていることを知っていますか?

ありがとうございます!

答えて

2

あなたは、たとえば、このような配列を返しますgetMessages()、ここで間違っている:

array(2) { 
    ["username"] => array(2) { 
    ["stringLengthTooShort"] => string(33) "'' is less than 3 characters long" 
    ["alphaStringEmpty"] => string(21) "'' is an empty string" 
    } 
    ["password"] => array(1) { 
    [0] => string(7) "Message" 
    } 
} 

はそのため、あなたは次のように各フィールドのエラーを取得するために、それを反復処理する必要があります。

foreach($form->getMessages() as $fields) 
{ 
    foreach ($fields as $error) { 
     echo $error; 
    } 
} 

詳しい情報here in the manual

getMessages()は、要素名/メッセージの連想配列 を返します(messagesはエラーコード/エラーメッセージ のペアの連想配列です)。

マニュアルで読んだことは、$messages = $element->getMessages();を使用して要素メッセージを取得する方法です。単一要素に対して返されるエラーメッセージは、エラーコード/エラーメッセージのペアの連想配列です。