karmicdice
karmicdice

Reputation: 1061

Cakephp Not validating data in Form

I am Trying to validate data from Form(.ctp file) in cakephp 2.3.8.

echo $this->Form->create('User').'<br />'.
                             $this->Form->input('handle', array(
                                                'rule' => 'notEmpty',
                                                 'alphaNumeric' => array(
                                                     'rule'      => 'alphaNumeric',
                                                    'required'  => true,
                                                    'message'   => 'Username must be only letters and numbers, no special characters'
                                                ),
                                                    'between' => array(
                                                        'rule'      => array('between', 4, 8),
                                                        'message'   => 'Username must be between 4 and 8 characters',
                                                    ),
                                                                'isUnique' => array(
                                                                    'rule'      => 'isUnique',
                                                                    'message'   => 'This username is already taken. Please choose a different one.'
                                                                ))).'<br />'.
                              $this->Form->input('email',array(
                                                'type'=>'text',
                                                'placeholder'=>'Enter your E-mail',
                                                'class'=>'form-control')).'<br />'.
                              $this->Form->input('password',array(
                                                'type'=>'password',
                                                'placeholder'=>'Enter your password',
                                                'class'=>'form-control'));?>

This is my modle code

public function beforeSave($options = array()) {
        parent::beforeSave($options = array());
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        $this->data['User']['token'] = Security::hash(mt_rand(),'md5',true);
        $this->data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
        return true;
    }

but when i singup, it not validating data and i don't know where i am mistaking

Upvotes: 1

Views: 515

Answers (1)

bowlerae
bowlerae

Reputation: 944

I have not seen validation rules in the view before, only in either the model or the controller. This may very well be an alternate method. Try moving validation code to the model.

class User extends AppModel {
    public $validate = array(

        'handle' => array(
            'lengthCheck'=>array(
                'rule'=>array('between', 4, 8),
                'message'=>'The username must be between 4 and 8 characters.'
            ),
            'isUnique'=>array(
                'rule'=>'isUnique',
                'message'=>'This username is already taken. Please choose a different one.'
            ),
            'alphanumeric' => array(
                'rule' => array('alphanumeric'),
                'message' => 'Username must be only letters and numbers, no special characters',
            ),
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your username',
            ),
        ),
        'password' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your password',
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Invalid email',
            ),
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your email',
            ),
        ),
    );

In your view

<?php
echo $this->Form->create('User');
echo $this->Form->input('handle', array(
    'placeholder'=>'Enter your username',
    'class'=>'form-control'
));
echo $this->Form->input('email', array(
    'placeholder'=>'Enter your E-mail',
    'class'=>'form-control'
));
echo $this->Form->input('password', array(
    'placeholder'=>'Enter your password',
    'class'=>'form-control'
));
?>

By default, it will recognize the password field as 'type' => 'password'

Upvotes: 2

Related Questions