
As writing more complex app i faced some challenges. I have 2 models on the same page.Person and User(it is optional). I have to use model validation rules(inbuilt and a costume:matchpassword). With person it is not a challenge but with User i don't know how. Because if i implement the validation rules at the first inload,i can not have optional User anymore.Jquery validation is not safe enough i need to the server side.
Groupscontroller:
public function add() {
if ($this->request->is('post')) {
$this->Group->create();
if (
$this->Group->save($this->request->data)
) {
$this->Session->setFlash(__('The group has been saved.'));
$group_id=$this->Group->getInsertId();
$person=$this->request->data['Person'];
$person['group_id']=$group_id;
$this->Group->Person->create();
$this->Group->Person->save($person);
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
}
}
the view file Since user is optional i generate the input fields by jquery:
<div class="groups form">
<?php echo $this->Form->create('Group', array('class' => 'jquery-validation'));
echo $this->Html->script('http://code.jquery.com/jquery-1.11.3.min.js');
?>
<fieldset>
<legend><?php echo __('Add Group'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('comments');
?>
</fieldset>
<h2>People</h2>
<table id="mytable">
<tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th><th>Gender</th></tr>
<tr id="person0" style="display:none;">
<td><?php echo $this->Form->button(' - ',array('type'=>'button','title'=>'Click Here to remove this person')); ?></td>
<td><?php echo $this->Form->input('unused.lastName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.firstName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.email',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.gender',array('label'=>'','type'=>'select','options'=>array('M'=>'M','F'=>'F','T'=>'T'))); ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another person','onclick'=>'addPerson()')); ?> </td><td></td><td></td><td></td><td></td></tr>
</table>
<div id="user-options">
<?php
if (isset($this->data['User'])) {
echo $form->hidden("User.0.id");
echo $form->input("User.0.username", array('label' => "Option " . (0)));
echo $form->input("User.0.password", array('label' => "Option " . (0)));
echo $form->input("User.$i.password_confirm", array('label' => "Option " . (0)));
}
?>
</div>
if i would not use : $this->Form->input('unused.firstName'
and using $this->Form->input('User.firstName'
the model validation rules would works but in this case i can not add only the person because of the required User fields
the Person modell:
class Person extends AppModel {
/**
* Use database config
*
* @var string
*/
public $useDbConfig = 'test';
/**
* Display field
*
* @var string
*/
public $displayField = 'firstName';
public $validate = array(
'firstName' => array(
'rule'=>'alphaNumeric',
'required' => true,
'message'=>'Not empty'),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Enter your Password',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'Match passwords'=>array(
'rule'=>'matchPasswords',
'message'=>'passwords are not equal'
)
),
'password_confirm' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Confirm your password',
'update' operations
),
);][1]
User Model:
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Enter your Password',
),
'Match passwords'=>array(
'rule'=>'matchPasswords',
'message'=>'passwords are not equal'
)
),
'password_confirm' => array(
'notEmpty' => array(
),
Update1: I was think about a bit.Maybe i have to write a costume validation rule: where i can state that if (Model1 fields AND Model2 fields are not empty) OR Model1 fields not empty and Model2 fields empty) return true
User.0.idshould beUser.id,User.0.usernameshould beUser.username,User.0.passwordshould beUser.passwordandUser.$i.password_confirmshould beUser.password_confirm- Anant Kumar Singh