1

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

1 Answer 1

2

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'

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried that before but Auth was interfering in validation so I declared them in view file But no worried I made manual validation through controller action
Could you explain how Auth was interfering with the validation in the model? I'm curious.
It wasen't allowing user to signup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.