Catch Symfony Form Errors

Sat, Jul 25, 2009 One-minute read

Have you built a form while developing with the symfony form framework that appears to work fine but fails because of an unknown error? Here is the simplest and easiest way to catch most errors.

First open the web page with the form and fill it in with valid input. Then, open the form php file, temporarily delete all your custom form render code and replace it with echo $form. Go back to the web page and click your form submit button. It should show you any validation errors that appear in the form but were missing on your customized version, because this time it’s rendering the form with the default settings. At the very least it could show you something you missed and hint at the solution.

Your test form would look like this:

<form action="<?php echo url_for('user/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>
<table>
  <tfoot>
  <tr>
    <td>
      <?php echo $form->renderHiddenFields() ?>
      <input type="submit" value="Save">
    </td>
  </tr>
  </tfoot>
  <tbody>
    <?php echo $form ?>
  </tbody>
</table>
</form>