I'm working with CakePHP
and trying to understand the best ways to make my application consistent and logical.
Now I'm trying to working with Model data validation
and handling validation errors in the view
, I have a doubt on how should I do if I like to insert some link inside the returned error, for example for a forgotten password.
Is it good to use (if it's possibile) HtmlHelper
inside the Model to return consistent links inside my application, or should I think about another way?
<?php
App::import('Helper', 'Html');
class User extends AppModel {
var $name = 'User';
var $validate = array (
'email' => array (
'checkEmail' => array (
'rule' => array('email', true),
'message' => 'Email not valid message.'
),
'checkUnique' => array (
'rule' => 'isUnique',
'message' => 'This email is allready in the db, if you forgot the password, '.(string)$this->Html->link('click here', array('controller' => 'users', 'action' => 'password-recover')).'.'
)
)
// the rest of the code...
This doesn't work because it seems I can't chain the message string with HTML string.
Does exist e smartest way to do that, or should I simply insert the html string without the HtmlHelper
?
If you really want HTML in your validation messages CakePHP provides a way to do this, no breaking Cake, no writing a lot of code.
In your $validation
just use whatever HTML you would like to have presented to the user.
In your view when you create your FormHelper::input($fieldName, array $options)
pass the following array to $options
:
$options = array('error' => array(
'attributes' => array('escape' => false)
))
See this page to learn more about the $options['error']
...options.
Alternatively, if you want all inputs with no HTML escaping you can pass $options['inputDefaults']
when you create the form.