简体   繁体   中英

Cakephp 2.+ $this->Model->validates(); always return true

i've search through stackoverflow. Most of the question resemble this problem,but it doesn't helped me. I've read the documentation of cakephp 2.0 and still can't find my answer.

My problem is: I've got seperate forms in a view. Each post has a different $this->request->data. Which what i wanted. I want each forms use a subset of validation of this model. But the problem is that they always 'return true'. Even when i've specified the fieldList.

View:

    <?php echo $this->Form->create('User'); ?>
    <?php echo $this->Form->input('Postcode');?>
    <?php echo $this->Form->input('Postcodeletter');?>
    <?php echo $this->Form->submit('Submit');?>
    <?php echo $this->Form->end();?>

    <?php echo $this->Form->create('User'); ?>
    <?php echo $this->Form->input('Password');?>
    <?php echo $this->Form->input('checkPassword');?>
    <?php echo $this->Form->submit('Submit');?>
    <?php echo $this->Form->end();?>

Controller:

unset($this->request->data['User']['Id']);
$this->User->set($this->request->data);

if ($this->User->validates(array('fieldList' => array('Postcode')))) {

            //valid

            } else {
           //error
    }

Model:

    class User extends AppModel {

public function beforeSave($options = array()){
    if(isset($this->data['User']['Password'])){
        $this->data['User']['Password'] = AuthComponent::password($this->data['User']['Password']);
    }
    return true;
}
public $primaryKey = 'Id';

public $validate = array(

   'Firstname' => array(
            'rule' => 'notEmpty',
            'allowEmpty' => false,
            'on' => 'create',
            'message' => '*Vult uw voornaam in' 
    ),
'Postcode' => array(
        'numeric' => array(
            'rule' => 'numeric',
            'allowEmpty' => false,
            'on' => 'create',
            'message' => 'Needs to be numbers'  
            ),
        'minLength' => array(
            'rule' => array('minLength',4),
            'allowEmpty' => false,
            'on' => 'create',
            'message' => 'Min 4 numbers'
            )
          )

);// end $validate

}

Is there's something i've did wrong in the 'if-statement'?

Ps: I've shorten my codes, I think the problem is clear and i'm sorry for my poor english.

UPDATE :

added $this->User->set($this->request->data);
added unset($this->request->data['User']['Id']) now it works.

Is your Debug-Level set to 2 in Core-Config? (app/Config/core.php)

Can you add the output of this in your Controller to your question?

// Put this before your If-Statement

debug( $this->User->invalidFields(); );

// You may copy the rendered output

I don't know about your Software, but what helps me often figuring out why cake's behaviour's such against my expectation is debugging my Code using a PHP-IDE and X-Debug for example.

Do not use 'on' => 'create' when it is not justified. In your case it is not.

You would want to always validate the minLength or notEmpty, even on edit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM