Error message display in cakephp
After validating , CakePHP automatically displays the error messages below the respective input control that we provide in particular model . Cake automatically generated their Own div and Error message class .we can also show the error message in our own style with the help of Helper . Ex :
form->error() Consider this input box for username in add.ctp
echo $form->input('User.user_name', array('type'=>'text', 'label'=>false, 'div'=>false, 'error' => false));
if we put error =>'false' then cake will not generate div and error message class automatically. To show the error message put 'class'=>'error-message' in to the attribute array.
echo $form->error('User.user_name', null, array('class'=>'error-message'));
Now we can see the error messages.
CakePHP error in validation
preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php.
I had an error in one of my cakephp application. THe Error was preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php.The Error triggered by the validation in model. After experiment(Commenting and Uncommenting) with validations rules in models. I found that The error comming from the rule "isNumeric ".
'user_id' =>array(
'rule1'=>array('rule'=>'isNumeric','message'=>'User is mandatory')
);
I replaced isNumeric with " numeric" .The error disappeared. There is no function named isNumeric exists in cakephp validation rule.
Print a web page Onload CakePHP
Today in this technology Hotel [Frog fry] serves you a delicious hot webpage fry [Print a web page -Onload].we are not only serves but also teach you to how to cook. This webpage fry is very easy to learn and cook without making any confusion and the waste of time. We need major two types of ingredients ie print function and the on load function .
Controller functions.
{
$this->layout = 'print';
$this->Model.name->recursive=-1;
// code for retrieving data from database.
$this->set('bodyAttr', 'onload="window.print();"');
}
Define a layout named print. Then set bodyAttr => 'onload="window.print();"’.In layout inside body put these code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
if (isset($bodyAttr)) { $bodyAttr = " $bodyAttr"; } else {
$bodyAttr = null; } ?>
<body <?php echo $bodyAttr; ?>>
<?php echo $content_for_layout; ?>
</body>
</html>
Create a new file print.ctp under the folder ‘ view ‘ folder associated with the controller and render data.Now your webpage fry is ready . :)
CakePHPs error handling
Technically we have two types of errors: PHP errors and CakePHP errors. CakePHP errors (application errors) would be something that is triggered by your application, for example a missing view file or a missing database table, PHP errors would be something like a fatal error that’s caused by a missing bracket or a notice of an undefined variable.
It goes without saying that in production your code should not have any missing brackets or missing views, but there are cases (i.e. division by zero) where a problem can slip by even with rigorous testing. Surely, you don’t want your users to see it, but it would be good for the developer/manager to know if that happens.
Let’s take a look at what to do with our PHP errors… It is important to realize that PHP errors should be dealt with completely separately from the CakePHP errors.
Thankfully you can pretty easily control what happens when a PHP error arises by using our good ol’ .htaccess or php.ini. A simple google search will teach you all you need to know about how to ensure that PHP errors are logged, but not displayed to the user.
So what about CakePHP errors?
To extend CakePHP’s error handling you should create a file called app_error.php in your /app/ directory.
You then neeed to create a class there: class AppError extends ErrorHandler…
Now, you can override any default CakePHP error handling methods, to suit your own needs.
Here’s a trick to allow CakePHP errors to be emailed to the developer, while running with debug = 0, set in core.php:
So now, the end-user will not see any errors (PHP or CakePHP), but the developer should have PHP errors logged on the server and CakePHP errors sent to him in an email.
It is also a good idea to add some output so that the user is not stuck looking at a blank page. You can easily extend above method to have:
You could just as easily log the errors to a file or define your own error methods (see cake’s default error.php) to add improved error logging and handling.
I’m sure this approach goes against some rules, but I have not seen a different way to handle this situation… so any suggestions/improvements are certainly welcomed.
Anchors to CakePHP Generated Urls
This may seem simple and trivial, but it took me a little while to figure out. If it saves someone a little trial-and-error then great (I know I won't forget how to do it).
I needed to create a redirect in CakePHP that redirected to an inline, on-page anchor- <a name="destination">Scroll to Here</a>. Typing the link directly into the address bar of the browser, it would need to look like this: http://www.mainelydesign.com/blog/view/1219/#destination. The #destination is what I needed to figure out how to append to the redirected url. Turns out it was easy.
