Posted By

skwok on 10/06/09


Tagged

debug symfony sfPropelForm


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

rccc
memonic


Get all symfony form errors


 / Published in: PHP
 

Add snippet in BaseFormPropel.class.php

  1. public function getErrors()
  2. {
  3. $errors = array();
  4.  
  5. // individual widget errors
  6. foreach ($this as $form_field)
  7. {
  8. if ($form_field->hasError())
  9. {
  10. $error_obj = $form_field->getError();
  11. if ($error_obj instanceof sfValidatorErrorSchema)
  12. {
  13. foreach ($error_obj->getErrors() as $error)
  14. {
  15. // if a field has more than 1 error, it'll be over-written
  16. $errors[$form_field->getName()] = $error->getMessage();
  17. }
  18. }
  19. else
  20. {
  21. $errors[$form_field->getName()] = $error_obj->getMessage();
  22. }
  23. }
  24. }
  25.  
  26. // global errors
  27. foreach ($this->getGlobalErrors() as $validator_error)
  28. {
  29. $errors[] = $validator_error->getMessage();
  30. }
  31.  
  32. return $errors;
  33. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: gearond on December 6, 2009

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.

Posted By: wojtek on January 11, 2011

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; }

 if ($form_field->hasError())
 {
   $error_obj = $form_field->getError();
   if ($error_obj instanceof sfValidatorErrorSchema)
   {
     foreach ($error_obj->getErrors() as $error)
     {
       // add namespace for embedded form erros

       if($form->getName() != $this->getName() )
       {
         $errors[$form->getName()][$form_field->getName()][] = $error->getMessage();
       }
       else
       {
         $errors[$form_field->getName()][] = $error->getMessage();
       }

     }
   }
   else
   {
     if($form->getName() != $this->getName() )
     {
       $errors[$form->getName()][$form_field->getName()][] = $error_obj->getMessage();
     }
     else
     {
       $errors[$form_field->getName()][] = $error_obj->getMessage();
     }

   }
 }

}

// global errors

foreach ($this->getGlobalErrors() as $validatorerror) { $errors[] = $validatorerror->getMessage(); }

return $errors; }

You need to login to post a comment.