Cameron
Cameron

Reputation: 28853

CakePHP Validation

How do I show the messages when using the CakePHP validation? As I creating the input fields manually using input() instead using the shorthand form() helper.

e.g. Form:

<?php echo $this->Form->create('User', array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>

    <fieldset id="login">

        <ul class="clearfix">               
            <li id="li-username">
                <?php echo $this->Form->input('email', array( 'label' => array('class' => 'placeholder', 'text' => 'Email address or username') )); ?>
            </li>
            <li id="li-password">
                <?php echo $this->Form->input('password', array( 'type' => 'password', 'label' => array('class' => 'placeholder', 'text' => 'Password') )); ?>
                <span id="iforgot"><?php echo $this->Html->link('?', 
                array('controller' => 'users', 'action' => 'forgotpassword'),  array('title' => 'Forgot your password?')); ?></span>
            </li>
            <li id="li-submit">
                <button type="submit" title="Log in">Log in &#9658;</button>
            </li>
        </ul>

    </fieldset>

<?php echo $this->Form->end(); ?>

and this is my validation in the user model:

public $validate = array(
    'email' => array(
        'valid' => array(
            'rule' => 'email',
            'message' => 'The email is not valid'
        ),
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter an email'
        )
    )
);

However the validation error messages don't show?

EDIT: I tested this on my register form at /users/add/ and it works so it seems that the auto validation does not work with the login method???? How do I add validation for the login form then :/

Upvotes: 1

Views: 587

Answers (3)

Akshay Sharma
Akshay Sharma

Reputation: 1112

First you check your post data is going to validate function.you can simple check this in your else condition like this :

$this->{$this->modelClass}->set($this->data);
if($this->{$this->modelClass}->validates(){
    //Save data in DB
}else{
   pr($this->{$this->modelClass}->validationErrors); // This will show your error message
}

You can also show error in your view.ctp file like this


    $errors = '';
    foreach ($this->validationErrors[$model] as $key =>  $validationError) { 
        $errors .= $this->Html->tag('li', $validationError[0]);
    }
    echo $this->Html->tag('ul', $errors,array('class' => 'error'));

Upvotes: 0

Quy Le
Quy Le

Reputation: 2379

You can show your code login function?
I think in your code that have redirect in-case validate false
If you use $this->redirect() it'll not show validate messages :)

Upvotes: 0

Gevious
Gevious

Reputation: 3252

The validation is actually stored in the model object. I'm not entirely sure off-hand how to access the errors, but I think its in $this->User->validationErrors.

Have a look at the model api for more information.

For logging in, use the auth component. If you'd rather not, then just get the user from the db and display an error using $this->Session->SetFlash() if the user doesn't authenticate.

Upvotes: 1

Related Questions