开发者

CakePHP integer field default null failing validation on save() after read() or create()

开发者 https://www.devze.com 2023-04-02 17:02 出处:网络
Schema: vacation_request_denial_reason_id int(11) DEFAULT NULL, Model: class VacationRequest extends AppModel {

Schema:

vacation_request_denial_reason_id int(11) DEFAULT NULL,

Model:

class VacationRequest extends AppModel {
    var $name = 'VacationRequest';
    var $actsAs = array('Containable');
    var $validate = array('employee_id' => array('numeric' => array('rule' => array('numeric'))),
        'approving_supervisor_id' => array('numeric' => array('rule' => array('numeric'))),
        'vacation_request_status_id' => array('numeric' => array('rule' => array('numeric'))),
        'vacation_hours' => array('numeric' => array('rule' => array('numeric'))),
        'vacation_request_denial_reason_id' => array('numeric' => array('rule' => array('numeric'))));

Controller:

function add() {
  if (!empty($this->data)) {
    $this->VacationRequest->create();
    if ($this->VacationRequest->save($this->data)) {
      // ...
    }
  }
}

This code fails validation for vacation_request_denial_reason_id...

I've narrowed the problem down to whenever I attempt to call Model->save() after calling either a Model->read() or Model->create(). Proven by the fact the code will work if I comment out the create() call.

I dare call t开发者_运维技巧his a bug. However, I feel like I've experienced this before for integer columns specifically and forgot the work around. Any guidance is appreciated.

UPDATE

I should note that vacation_request_denial_reason_id is not part of $this->data in the form submission. So it's definitely odd that it's failing validation. Furthermore, it is not my intention to use 'allowEmpty' => true in the validation rule. When it is present in $this->data, I want to ensure it is numeric.


In your validation array, try setting the empty option to true:

'vacation_request_denial_reason_id' => array('numeric' => array('rule' => array('numeric'), 'empty' => true

This will allow the value to be empty. I can't remember whether false is the default, but it seems like that might be the case.

0

精彩评论

暂无评论...
验证码 换一张
取 消