Frogfry's logo
  • Home
  • About Frogfry
  • Article
  • Portfolio
  • Contact

Error message display in cakephp

Posted by Administrator on Sun, Mar 27 2011 21:10:00

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.

1233 Comments

CakePHP error in validation

Posted by Administrator on Sun, Mar 27 2011 21:07:00

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.

Posted in Cakephp Articles | 85 Comments

Print a web page Onload CakePHP

Posted by Administrator on Sun, Mar 27 2011 21:00:00

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.

 

function print()
{
$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 . :)

Posted in Cakephp Articles | 1448 Comments

CakePHPs error handling

Posted by Administrator on Sun, Mar 27 2011 20:53:00

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:

 

class AppError extends ErrorHandler {

function __construct($method, $messages) {
Configure::write('debug', 1);
parent::__construct($method, $messages);
}

function _outputMessage($template) {
$this->controller->render($template);
$this->controller->afterFilter();

   App::import('Core', 'Email');

   $email = new EmailComponent;

   $email->from = 'CakePHP <cakephpapp@yourdomain.com>';
$email->to = 'Developer <somedeveloper@yourdomain.com>';
$email->sendAs = 'html';
$email->subject = 'Error in my CakePHP app';

   $email->send($this->controller->output);
}

}

 

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:

 

$this->controller->output = null;
$this->controller->render('someGenericErrorTemplate');
echo $this->controller->output;

 

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.

Posted in Cakephp Articles | 30 Comments

Anchors to CakePHP Generated Urls

Posted by Administrator on Sun, Mar 27 2011 19:37:00

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.

Use the Pound Sign (#) as a Key in Your Link Options
echo $html->link('Click Me, Please', array('controller'=>'blog', 'action'=>'view', 1219, '#' => 'destination'));
or, Use the Pound Sign (#) as a Key in Your Redirect Call
$this->redirect(array('controller'=>'blog', 'action'=>'view', 1219, '#'=>'destination'));
Posted in Cakephp Articles | 42 Comments
1 | 2
Frogfry's logo

About

I am 18years old Green smart looking Frog, Owner of Technology hotel named Frog Fry, and serving different types of Fries ( Applications ) in common pound ( open source ). Our Cooking style is very easy to understand .We want to share the cooking knowledge that we have. (Tutorials)

Categories

  • Cakephp Articles
  • PHP And Other Article

Recent Posts

  • Error message display in cakephp
  • CakePHP error in validation
  • Print a web page Onload CakePHP
  • CakePHPs error handling
  • Anchors to CakePHP Generated Urls

Meta

  • Site Admin
  • Entries (RSS)
  • Comments (RSS)
Copyright © All rights reserved. frogfry.