Forms Error Catching and Displaying in Symfony

In actions.class.php page of symfony framework, you can catch errors and display those errors in template. Set errors in your action/method using:

if ($error) {
$this->getRequest()->setError('userName', 'The user Name field cannot be left blank');
$this->getRequest()->setError('email', 'The Email field cannot be left blank');
return false;

Return false is important for returning to the same form, from where you get data for analyzing errors.

In template you will show errors like this:

<?php if ($sf_request->hasErrors()): ?>
<p>The data you entered seems to be incorrect.
Please correct the following errors and resubmit:</p>
<?php foreach($sf_request->getErrors() as $name => $error): ?>
<li><?php echo $name ?>: <?php echo $error ?></li>
<?php endforeach; ?>
<?php endif; ?>

If you are using forward('module', 'action') after setting error (setError()), then it is available in form errors
(hasError()). But these errors will be unavailable then you redirect. If you have set any flash variable ($this->setFlash())
then you can use it in both forward() and redirect() cases.

You have shorthand form for using form errors in template/form.
Instead of writing :

If ($sf_request->hasError('name')) {
echo $sf_request->getError('name');
write this:

echo form_error('name'); // error if error occurs
Now if you have set form error in action using serError() function or it has been set by validator then it will show error
message in case validation failed.