cakephp - Validate rule in cake php -
i trying validate form on submit not working , form submitted without error. no message displayed in view form. useing below mentioned code in user model
public $validate = array( 'fname' => array( 'required' => array( 'rule' => array('notempty'), 'message' => 'a username required' ), 'between' => array( 'rule' => array('between', 3, 15), 'message' => 'between 5 15 characters' ), ), 'email' => array('email' => array( 'rule' => 'email', 'message' => 'email must valid email address!' ), 'unique' => array( 'rule' => 'isunique', 'message' => 'user registered!' ) ), 'password' => array( 'required' => array( 'rule' => array('notempty'), 'message' => 'a password required' ), 'between' => array( 'rule' => array('between', 8, 15), 'message' => 'between 8 15 characters' ), 'slug' => array( 'rule' => '/^[a-z0-9a-z@#_]{8,}$/i', 'message' => 'only letters, integers ,@, # , underscore(_), min 8 characters' ), ), );
userscontroller.php
class userscontroller extends appcontroller { public $name = 'users'; public function register(){ if(($this->request->is('post'))||($this->request->is('post'))){ $this->user->create(); $this->user->save($this->request); } }
register.ctp
<div class="col-lg-12"> <div class="row"> <h2 class="col-lg-12 heading">register</h2> <?php echo $this->form->create('user', array('type'=>'file')); echo '<div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">first name</label>'); echo $this->form->input('fname', array('type'=>'text', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'first name')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">middle name : </label>'); echo $this->form->input('mname', array('type'=>'text', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'middle name')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">last name : </label>'); echo $this->form->input('lname', array('type'=>'text', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'last name')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">date of birth : </label>'); echo $this->form->input('dob', array('type'=>'text', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'date of birht')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">gender : </label>'); echo $this->form->input('gender', array('options'=>array('0' => ' male', '1' => ' female', '2' => ' other'),'type'=>'radio', 'separator'=>' ', 'div'=>array('class'=>'col-lg-7'), 'label'=>false, 'class'=>'','legend' => false)); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">e-mail : </label>'); echo $this->form->input('email', array('type'=>'text', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'e-mail')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">password : </label>'); echo $this->form->input('password', array('type'=>'password', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'password')); echo '</div><div class="clearfix"></div><div class="form-group">'; echo $this->html->div('col-sm-2', '<label for="inputtextl1" class="control-label">confirm password : </label>'); echo $this->form->input('cpassword', array('type'=>'password', 'div'=>array('class'=>'col-sm-5'), 'label'=>false, 'class'=>'form-control', 'placeholder'=>'confirm password')); echo '</div><div class="clearfix"></div><div class="col-sm-12" align="center">'; echo $this->form->submit('register', array('div'=>false, 'label'=>false, 'class'=>'btn btn-primary')); echo '</div>'; ?> </div> </div>
change line-
echo $this->form->create('user', array('type'=>'file'));
to:
echo $this->form->create('user', array('type'=>'file', 'novalidate' => true));
and missed line @ end of form:
echo $this->form->end();
also check field name in model. should same in database table.
it solve problem.
Comments
Post a Comment