user3301561
user3301561

Reputation:

How to assign validations for model and controller in cakephp?

I am developing the one register form.but i dont know how to do the validations in cakephp for that register form in controller class.

model class:

user.php

<?php
class User extends AppModel
{
var $name='User';
//var $useTable = false;
var $validate= array(
        'username'=>array(
                'rule'=>'notEmpty',
                'required'=>true,
                'message'=>'Enter your name'
        ),
        'email'=>array(
                'email'=>array(
                        'rule'=>'email',
                        'message'=>'Enter a valid emial address'
                ),
                'email'=>array(
                        'rule'=>'notEmpty',
                        'required'=>true,
                        'message'=>'Enter your email address'
                )

        ),

        'password'=>array(
                'alphaNumeric' => array(
                        'rule' => 'alphaNumeric',
                        'required' => true,
                        'message' => 'Alphabets and numbers only'
                ),
                'password' => array(
                        'rule' => array('minLength', '8'),
                        'message' => 'Minimum 8 characters long'
                ),
               ),
        'firstname'=>array(
                'rule'=>'notEmpty',
                'required'=>true,
                'message'=>'Enter your firstname'
        ),
        'lastname'=>array(
                'rule'=>'notEmpty',
                'required'=>true,
                'message'=>'Enter your lastname'
        ),
);

**controller class:

UserController.php**

<?php
class UsersController extends AppController
{
    var $name = 'Users';    

    function send(){
        if(!empty($this->data)){
            $this->User->set($this->data);
            if($this->User->validates()){
                if(!empty($this->data['User']['username'])) {
                    $this->username->from = $this->data['User']['username'] ;
                }
                else{
                    $this->username->from = $this->data['User']['username'] .
                    ' <' . $this->data['User']['username'] . '>';
                    $this->render('success');
                }
            }
            else {
                $this->render('register');
            }
        }
    }
    }

Users.register view *register.ctp*

<h3>Registration Form<h3>
    <form action="../users/register" method="POST" name="form">
    UserName:<input type="text" name="username">
    Password:<input type="password" name="password">
    Email:<input type="text" name="email">
    FirstName:<input type="text" name="firstname">
    LastName:<input type="text" name="lastname">
    <input type="submit" value="Register">
    </form>

please help me how to do the validations in cakephp.

Upvotes: 1

Views: 797

Answers (3)

Fury
Fury

Reputation: 4776

  1. when you want to save your data you will need to assign the validation. true activates the validation. how ever in default validation is set to true.

    $this->User->save($this->data)
    //to 
    $this->User->save($this->data , true ) //or false - set the validation here
    
    // your function needs to be public    
    public function register(){
       if(!empty($this->request->data)){        
          if($this->User->save($this->request->data)){
                //save data
            else{
                //data didn't save
            }
        }
    
    }
    
  2. In some cases like create or update you need to set where you want to assign your validation behavior. e.g. on save on create or update.

  3. In your model 'required'=>true, is used for other reason. Use 'rule'=>'notEmpty', is for the inputs that is required

  4. Your Form it's not standard for cakephp validation: Create your Form and inputs cakephp based so you will have clear code and make the job available for your validation

    <?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'register')));?>
            <fieldset>
                    <legend><?php echo __('Register User'); ?></legend>
                    <div class="seven columns">
            <?php
                    echo $this->Form->input('username');    
                    echo $this->Form->input('password');    
                    echo $this->Form->input('first_name');
                    echo $this->Form->input('last_name');
                    echo $this->Form->input('phone_number');
                    echo $this->Form->input('email');
                    echo $this->Form->input('address1');
                    echo $this->Form->input('address2');
            ?>
                    </div>
    
            </fieldset>
        </div>
    
    <?php echo $this->Form->end(__('Register'));?>
    
  5. remove var $name = 'Users'; => this line is already set in default

  6. Model: variables should be public

    var $name='User';        
    var $validate= array(); 
    
    //change them to 
    
     public $name='User';        
     public $validate= array(); 
    

Upvotes: 0

Kuldeep Choudhary
Kuldeep Choudhary

Reputation: 775

in your code you haven't written any code for data save.....if you will save or edit data..your model will take care of validation automatically as you have written var $validate
in your model code..........

Upvotes: 0

Fazal Rasel
Fazal Rasel

Reputation: 4526

create an action named register() and created a view register.ctp. Your register aciton code looks like that-

function register(){
    if(!empty($this->data)){
        $this->User->create();
        if($this->User->save($this->data)){
                 $this->Session->setFlash("User saved!");
                $this->redirect('/login');
            }
            else{
                 $this->Session->setFlash("Unable to save now, Try later!");
            }
        }
 }

Now when you try to save data, your data will validate automatically if you define validate rules on model.

Upvotes: 1

Related Questions