cakephp 2.x多个模型(包含可选)验证

时间:2015-06-08 18:01:06

标签: php jquery cakephp

by + button a jquery function generates the fields,it is without model validation

在编写更复杂的应用程序时,我遇到了一些挑战。 我在同一页面上有2个模型。人和用户(可选)。 我必须使用模型验证规则(内置和服装:matchpassword)。 对于人而言,这不是一个挑战,但与用户我不知道如何。 因为如果我在第一个inload实现验证规则,我就不能再有可选的User.Jquery验证不够安全,我需要服务器端。

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.'));
        }
    }

视图文件 由于user是可选的,我通过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('&nbsp;-&nbsp;',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>

如果我不使用:$this->Form->input('unused.firstName' 并使用$this->Form->input('User.firstName' 模型验证规则可以工作,但在这种情况下,由于所需的用户字段

,我不能只添加人

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]

用户模型:

'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: 我想了一下。也许我必须写一个服装验证规则: 我可以声明if(Model1字段和Model2字段不为空)或Model1字段不为空且Model2字段为空)返回true

1 个答案:

答案 0 :(得分:0)

您可以在验证前取消设置规则,请参阅以下示例:

unset($this->User->validate['Match passwords']);
if($this->User->save($this->request->data)){
    //do something.
}

附加截图似乎,您可以一次添加多个用户,如果是User.0.password它是正确的,否则您只需要User.password