Customize CakePHP’s Error Messages With Component

CakePHP has wonderful way to show error messages after validating Form fields. It shows two types of error messages. One is at the top which says some thing liek this “User can not be saved. Please try again.”. another type of error messages shows bellow the input fields Like ‘Name can not be empty’. In minimalistic way this error message are more than enough. But Because of HTML design or on clients request you may need to change the way error message will appear. On this post I am going to show how you can show all the error message on top of the Form.

First we are going to make two elements to show error messages when the $this->Session->setFlash() is called. You can mention an element’s name on this function on which the error message will show. Like this

$this->Session->setFlash(__('Old password does not match. The user 
could not be saved. Please, try again.', true), 'flash_failure');

$this->Session->setFlash(__('Profile Saved', true), 'flash_success');

flash_failure and flash_success are two elements who are going to show errors. For showing the errors smoothly I use http://twitter.github.com/bootstrap/components.html#alerts. So My flash_failure.ctp contains code like this

<div class="alert alert-block alert-error">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<?php echo $message ?>
</div>

flash_success.ctp contains code like this

<div class="alert alert-block alert-success">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<?php echo $message ?>
</div>

Now we are going to develop an component. I always have an common component on my projects which houses lot of common functions. On this component we are going to add a function customize_error_msg() which takes two parameters first one is array of errors and another ne is the default error message – this is optional. This component uses the Core session component so that it can use setFlash function

function customize_error_msg($errors = array(),
 $main_msg = 'We found error while processing your entries') 
{        
	$return_msg = "<h4>" . $main_msg . "</h4><ul>";
	
	foreach($errors as $key => $value):
		$return_msg .= "<li>{$value}</li>";
	endforeach;
	
	$return_msg .= "</ul>";
	$this->Session->setFlash($return_msg, 'flash_failure');
}

So now we can add the Common component our controller and call the customize_error_msg() function when we need.

$this->Common->customize_error_msg($this->User->invalidFields(), 
'The user could not be updated. Please, try again.');

When creating Form we need to add error’ => false in the inputDefaults array otherwise the error message will show under the input fields

<?php echo $this->Form->create('User', array('type'=>"file", 
'inputDefaults' => array('error' => false)));?>

The final output will be like this

You can download the component from here. I also provided an example how you can use this component.

About Zakir Hyder

This entry was written by .

08. November 2012 by Zakir Hyder
Categories: CakePHP, Component | Tags: , , , , | Comments

Comments

  1. […] Customize CakePHP’s Error Messages With Component November 8, 2012 6:42 PM CakePHP has wonderful way to show error messages after validating Form fields. It shows two types of error messages. One is at the top which says some thing liek this “User can not be saved. Please try again.”. another type of error messages shows bellow the input fields Like ‘Name can not be empty’. In […] […]