CakePHP - Customizing Message
Is there any way to set a layout to my messages in my model code?
Here is my model:
var $validate = array(
'email' => array(
'rule' => array('email', true开发者_开发百科),
'message' => 'Please supply a valid email address.'
)
);
This can be done with CakePHP. However, you have your concerns mixed up. The Model is there for data gathering, manipulation and massaging. The layout of the data is under the responsibilities of the View. In CakePHP specifically the Form Helper.
For more info about how to specify your own layout for a data validation message check out:
http://book.cakephp.org/view/1639/options-inputDefaults
They provide a pretty great code sample on exactly how to do this.
I also highly suggest you read through the whole book. It will prove invaluable.
Edit: Answer after clarification from comment
You would create an element
and put it in app/views/elements
. Should name the file using normal Cake conventions. Let's go with flash_error
.
You would set this up to be your HTML that you want displayed. To make sure your message is displayed simply add this bit of PHP wherever is appropriate
<?php echo $message; ?>
That's step 1.
Step 2 is in your $this->setFlash()
call pass the appropriate parameters. So your new calls would look like this with the element we named above:
$this->setFlash($message, 'flash_error');
Now your setFlash messages will use the layout defined in step 1. Wanna different layout? Just create a new element and pass the new element name.
The setFlash() method has 2 more parameters that come in handy (particularly if you want to have multiple flash() messages on the same page). Another link to the book:
http://book.cakephp.org/view/1313/setFlash
精彩评论