Created
October 3, 2013 06:31
-
-
Save b-b3rn4rd/6805880 to your computer and use it in GitHub Desktop.
Get flatten Zend_Form error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Form extends \Zend_Form | |
{ | |
/** | |
* Flatten Zend_Form error messages | |
* | |
* @param \Zend_Form|null $form | |
* @param array|null $messages | |
* @param int $i | |
* @return array | |
*/ | |
public function getMessagesSimple(\Zend_Form $form = null, array $messages = null, &$i = 0) | |
{ | |
$return = array(); | |
if (is_null($form)) { | |
$form = $this; | |
$messages = parent::getMessages(); | |
} | |
foreach ($messages as $fieldname => $errors) { | |
$subForm = $form->getSubForm($fieldname); | |
if ($subForm instanceof \Zend_Form) { | |
$return += $this->getMessagesSimple($subForm, $errors, $i); | |
continue; | |
} | |
foreach ($errors as $name => $value) { | |
$element = $form->getElement($fieldname); | |
$label = $element->getLabel(); | |
$return[$i] = array( | |
'message' => $value, | |
'label' => $label, | |
'fieldname' => $fieldname); | |
$i++; | |
} | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment