i am new in cakephp. in View i have 2 form(login and Registration) in both form have email id then how to validate that both from same value in cake php please help and if you have example then please sent that type of information.
Asked
Active
Viewed 160 times
-3
-
refer this: http://stackoverflow.com/questions/4673383/cakephp-one-form-multiple-models-not-displaying-one-models-validation-message – Insane Skull Aug 24 '15 at 13:03
1 Answers
0
First of all define validate function in model
public $validate = array(
'username' => array(
'nonEmpty' => array('rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false),),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required' ),
'min_length' => array(
'rule' => array('minLength', '6'),
'message' => 'Password must have a mimimum of 6 characters')),
'password_confirm' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please confirm your password' ), ),
'email' => array(
'required' => array(
'rule' => array('email', true),
'message' => 'Please provide a valid email address.' ),
),
);
Then in controller use
function register() {
if( isset($this->data) )
{
$this->User->create();
if($this->User->save($this->data))
{
$this->Session->setFlash( 'Thank you for registering!' );
}else
{
$this->Session->setFlash('An error occurred, try again!');
}
}
}
function login(){
if ($this->request->is('post')) {
$this->User->set($this->request->data);
if ($this->User->validates()) {
echo "This is valid!";
} else {
echo "This is invalid";
$errors = $this->User->validationErrors;
}
}
}
Registration ctp
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('password_confirm', array('label' => 'Confirm Password *', 'maxLength' => 255, 'title' => 'Confirm password', 'type'=>'password'));
echo $this->Form->submit('Add User', array('class' => 'form-submit', 'title' => 'Click here to add the user') ); ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
login.ctp
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->submit('Login'); ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>

Sudhir
- 835
- 11
- 31
-
but @sudhir this validation for single form not for two form in single view.I have 2 form login and registraion and in both form have same field like email,user name – Prakash Madhak Aug 28 '15 at 06:08
-
@PrakashMadhak in both form use user model validation . Have you try above code. If then what error you get – Sudhir Aug 31 '15 at 05:03
-
in my modul same code but in login form display error in valid user name is "not empty in " in login form but in my login form not any field like username.but registarion page is working perfact – Prakash Madhak Sep 01 '15 at 05:13
-
@PrakashMadhak i have edited and check it is working fine. Now you check – Sudhir Sep 01 '15 at 17:26