cakePHP Validations : one model for to forms in the same view -
i have 1 view wich has 2 forms 1 login , 1 registration following : signup.ctp //my view
<div> <?php echo $this->form->create("tbluser"); echo $this->form->hidden('formsent', array('value' => 'signup')); echo $this->form->input('username' ,array('label'=>'username')); echo $this->form->input('password' ,array('label'=>'password','type' => 'password')); echo $this->form->input('email' ,array('label'=>'email')); echo $this->form->end('register'); ?> </div> <div> <?php echo $this->form->create("tbluser"); ?> echo $this->form->hidden('formsent', array('value' => 'login')); echo $this->form->input('username' ,array('label'=>"username :")); echo $this->form->input('password' ,array('label'=>"password :",'type' => 'password')); echo $this->form->end('login'); ?> <div>
the model i'm using both forms following :
<?php class tbluser extends appmodel{ public $validate = array( 'username'=>array( array( 'rule'=>'alphanumeric', 'allowempty'=>false, 'message'=>'invalide username!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ), array( 'rule'=>'isunique', 'message'=>'username taken!' ) ), 'password' => array( array( 'rule' => 'alphanumeric', 'allowempty'=>false, 'message' => 'password must alphanumeric!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ) ), 'email'=>array( array( 'rule'=>array('email',true), 'required'=>true, 'allowempty'=>false, 'message'=>'invalide email adress!' ), array( 'rule'=>'isunique', 'message'=>'mail adress taken!' ) ) ); } ?>
the controller i'm using following :
<?php class tbluserscontroller extends appcontroller { public $uses = array( 'tbluser' ); public function signup() { if ($this->request->is('post')) { if ('signup' === $this->request->data['tbluser']['formsent']) { // registration part. }else if('login' === $this->request->data['tbluser']['formsent']){ //login part } } } ?>
my appcontroller looks :
<?php class appcontroller extends controller { public $helpers = array('form', 'html'); public $components = array('session','cookie','auth'=>array( 'authenticate'=>array( 'form' => array( 'usermodel' => 'tblforumuser', 'fields' => array( 'username' => 'username', 'password' => 'password' ) ) ) )); } ?>
right if fill wrong data signup form , submit validation occurs in login form fields how can set validation apply signup form , not both forms? thanks.
it looks validation being invoked on every read , write because telling model run validation without restriction. better way handle separate model each form.
by creating 2 new models userlogin
, userregister
extend tbluser
, can set specific validation rules each form. :
view/tbluser/signup.ctp
<div> <?php echo $this->form->create("userregister"); echo $this->form->hidden('formsent', array('value' => 'signup')); echo $this->form->input('username' ,array('label'=>'username')); echo $this->form->input('password' ,array('label'=>'password','type' => 'password')); echo $this->form->input('email' ,array('label'=>'email')); echo $this->form->end('register'); ?> </div> <div> <?php echo $this->form->create("userlogin"); ?> echo $this->form->hidden('formsent', array('value' => 'login')); echo $this->form->input('username' ,array('label'=>"username :")); echo $this->form->input('password' ,array('label'=>"password :",'type' => 'password')); echo $this->form->end('login'); ?> <div>
here, since we're using 2 separate models each $this->form-create();
helper, validation in specified model run. models contain validation applies form assigned it:
model/userregister.php
class userregister extends tbluser{ public $validate = array( 'username'=>array( array( 'rule'=>'alphanumeric', 'allowempty'=>false, 'message'=>'invalide username!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ), array( 'rule'=>'isunique', 'message'=>'username taken!' ) ), 'password' => array( array( 'rule' => 'alphanumeric', 'allowempty'=>false, 'message' => 'password must alphanumeric!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ) ), 'email'=>array( array( 'rule'=>array('email',true), 'required'=>true, 'allowempty'=>false, 'message'=>'invalide email adress!' ), array( 'rule'=>'isunique', 'message'=>'mail adress taken!' ) ) ); }
model/userlogin.php
class userlogin extends tbluser{ public $validate = array( 'username'=>array( array( 'rule'=>'alphanumeric', 'allowempty'=>false, 'message'=>'invalide username!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ), array( 'rule'=>'isunique', 'message'=>'username taken!' ) ), 'password' => array( array( 'rule' => 'alphanumeric', 'allowempty'=>false, 'message' => 'password must alphanumeric!' ), array( 'rule' => array('minlength', '4'), 'message' => 'username has more 3 chars' ) ) ); }
then in signup();
method, want load 2 new models created accordingly:
controller/tbluserscontroller.php
class tbluserscontroller extends appcontroller { public $uses = array( 'tblforumuser' ); public function signup() { $this->loadmodel('userlogin'); $this->loadmodel('userregistration'); if ($this->request->is('post')) { if ('signup' === $this->request->data['tblforumuser']['formsent']) { // registration part. }else if('login' === $this->request->data['tblforumuser']['formsent']){ //login part } } }
hope helps
Comments
Post a Comment