2012-04-16 66 views
0

早上,Zend表格驗證器foreach

我有一個Zend Form驗證器的奇怪問題。當我想輸出我看到的錯誤消息: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 { 

} 

我已經閱讀文檔,得知回聲$消息;應以純文本形式輸出錯誤消息。

做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 = $element->getMessages();獲取元素消息。針對單個元素返回的錯誤消息是錯誤代碼/錯誤消息對的關聯數組。