/ Published in: PHP
Add snippet in BaseFormPropel.class.php
Expand |
Embed | Plain Text
public function getErrors() { // individual widget errors foreach ($this as $form_field) { if ($form_field->hasError()) { $error_obj = $form_field->getError(); if ($error_obj instanceof sfValidatorErrorSchema) { foreach ($error_obj->getErrors() as $error) { // if a field has more than 1 error, it'll be over-written $errors[$form_field->getName()] = $error->getMessage(); } } else { $errors[$form_field->getName()] = $error_obj->getMessage(); } } } // global errors foreach ($this->getGlobalErrors() as $validator_error) { $errors[] = $validator_error->getMessage(); } return $errors; }
Comments
Subscribe to comments
You need to login to post a comment.

Great snippet! Thanks. I changed my implementation slightly. I made all instances of '$errors[$formfield->getName()]' as '$errors[$formfield->getName()][]' so that all errors per field could be caught.
What's about embedded forms? Lite modifications does the trick:
public function getErrors($form=false) {
$errors = array();
if(!$form) $form = $this;
// individual widget errors
foreach ($form as $formfield) { // if $formfield is a embedded form if($formfield instanceof sfFormFieldSchema) { $errors = arraymerge($errors, $this->getErrors($form_field) ); continue; }
}
// global errors
foreach ($this->getGlobalErrors() as $validatorerror) { $errors[] = $validatorerror->getMessage(); }
return $errors; }