Using HtmlHelper for a model to insert links in returned errors

I work with CakePHP and try to figure out how to make my applications consistent and logical.

Now I'm trying to work with Model data validation and handle Model data validation errors in view , I have doubts about how to do this if I like to insert some link inside the returned error, for example, for a forgotten password.

Is it good to use (if possible) the HtmlHelper inside the model to return consistent links inside my application, or should I think of something else?

 <?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 does not work, because it seems that I cannot associate a message string with an HTML string. Is there a smartest way to do this, or should I just insert an html string without an HtmlHelper ?

+4
source share
5 answers

If you really want HTML in your validation messages, CakePHP provides a way to do this without breaking Cake, without writing a lot of code.

In your $validation just use any HTML that you would like to present to the user.

In your view, when you create 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 $options['error'] ...

Alternatively, if you want all inputs without HTML escaping, you can pass $options['inputDefaults'] when creating the form.

+5
source

This is a difficult topic because

  • you may need to break MVC
  • validation is performed in the same way as in your case, usually in $validate and cannot contain dynamic material

for 1)

  • you can also use Router::url() with manual HTML
  • you can use BBcode or pseudo-markup and translate this into real links in the view / element flashmessage

for 2)

  • use __construct() and $this->validate to use dynamic elements if necessary

In PHP, class properties (e.g. $validate ) must be initialized with constant values .

 <?php class User extends AppModel { public $validate = array( 'email' => array( 'checkUnique' => array( 'rule' => array('isUnique'), 'message' => 'This email address has already been claimed, possibly by you. If this is your email address, use the <a href=":link">reset password</a> facility to regain access to your account' ), ), ); public function beforeValidate($options = array()) { $this->validate['email']['checkUnique']['message'] = String::insert( $this->validate['email']['checkUnique']['message'], array('link' => Router::url(array('action' => 'password-recover'))) ); return true; } 
+2
source

You do it yourself. Helpers are not available in the model and controller. And not in vain: M and C should not touch V.

There are ways to do exactly what you want (but involve significantly more code). Since you are asking for the smartest way: what is wrong with the reset password echo link in the view after the login form? Just echo 'Forgot your password? '.$this->Html->link('Click here', array('controller' => 'users', 'action' => 'password-recover')); echo 'Forgot your password? '.$this->Html->link('Click here', array('controller' => 'users', 'action' => 'password-recover'));

0
source

I do not agree to violate the MVC logic. I also tried using all the array('escape' => false) possible paths (in Form-> input, in Form-> error and even in the model), and none of them worked with me! (cakephp 2.0)

The answer "Anh Pham" is the easiest and easiest way. In addition to this, I returned an empty error message from the model check ('errorMessage' => false; does not work in cakePhp 2.0).

Since I wanted to pass the variable to the view in order to build the link there (MVC), in the controller , I check if the field was invalid:

 $invlaidFields = array_keys($this->Model->validationErrors(); if ( in_array('myField', $invalidFields) ){ ... } 

In view I check to see if this field has been invalidated, and then echo my error message sending a class error message, so it looks the same as the rest of the error messages.

 if ($this->Form->('myFields')) { ... echo '<span class="error-message">error message'. $this->Html->link(...).'</span>'; } 

Hope this helps someone out there.

PS It is always useful to mention which version of cakePHP you are using ...

0
source

In cakephp2 you can use the following:

// model check

 'company' => array('notempty' => array('rule' => array('notempty'),'message' => "select one company o send email to <a href="mailto: email@gmail.com ">contact</a>",),) 

//front

 <?php if ($this->Form->isFieldError('Register.company')): ?> <span class="text-danger"><?php echo $this->Form->error('Register.company', null, array('escape'=>false)); ?></span> <?php endif; ?> 
0
source

All Articles