Learn how to retrieve all the errors of an invalid form in Symfony 1.4 easily

Thanks to the model and form classes used in Symfony 1.4 you will be able to validate if the data of a form is what it needs to be in the backend to prevent exceptions while inserting data in the database. One of the most common problems when you submit a form and the process action handles the data, is that you will get a tedious error that a variable doesn't exist when the form isn't valid, for example check the following code:


protected function processForm(sfWebRequest $request, sfForm $form)
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if ($form->isValid())
        $horarios = $form->save();


The first problem is that you need to know which errors has the form after the submission. Normally you can debug the error schema of the form with the debug toolbar in the development environment of your project, however sometimes you will be unable to use the debug bar because of your programming patterns.

When a form isn't valid, the getErrorSchema method will contain the information that you need to know to understand why your form can't be processed:


$errors = array();

foreach ($form->getErrorSchema() as $key => $err) {  
    if ($key) {  
        $errors[$key] = $err->getMessage();  

// Display the array to see the form errors

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person