Merge pull request #467 from tPl0ch/2.1-required-update-create

Added support for simplified required validation on 'create' or 'update'. Fixes #230.
This commit is contained in:
Mark Story 2012-02-22 09:43:13 -08:00
commit b7302850b5
2 changed files with 182 additions and 2 deletions

View file

@ -3081,13 +3081,16 @@ class Model extends Object implements CakeEventListener {
}
$validator = array_merge($default, $validator);
if (!empty($validator['on'])) {
if (!empty($validator['on']) || in_array($validator['required'], array('create', 'update'), true)) {
if ($exists === null) {
$exists = $this->exists();
}
if (($validator['on'] == 'create' && $exists) || ($validator['on'] == 'update' && !$exists)) {
if ($validator['on'] == 'create' && $exists || $validator['on'] == 'update' && !$exists) {
continue;
}
if ($validator['required'] === 'create' && !$exists || $validator['required'] === 'update' && $exists) {
$validator['required'] = true;
}
}
$valid = true;

View file

@ -814,4 +814,181 @@ class ModelValidationTest extends BaseModelTest {
$this->assertFalse($Article->validates());
}
/**
* Test for 'required' => [create|update] in validation rules.
*
* @return void
*/
function testStateRequiredValidation() {
$this->loadFixtures('Article');
$Article = new Article();
// no title field present
$data = array(
'Article' => array(
'body' => 'Extra Fields Body',
'published' => '1'
)
);
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'create'
)
)
);
$Article->create($data);
$this->assertFalse($Article->validates());
$Article->save(null, array('validate' => false));
$data['Article']['id'] = $Article->id;
$Article->set($data);
$this->assertTrue($Article->validates());
unset($data['Article']['id']);
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'update'
)
)
);
$Article->create($data);
$this->assertTrue($Article->validates());
$Article->save(null, array('validate' => false));
$data['Article']['id'] = $Article->id;
$Article->set($data);
$this->assertFalse($Article->validates());
}
/**
* Test that 'required' and 'on' are not conflicting
*
* @return void
*/
function testOnRequiredConflictValidation() {
$this->loadFixtures('Article');
$Article = new Article();
// no title field present
$data = array(
'Article' => array(
'body' => 'Extra Fields Body',
'published' => '1'
)
);
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'create',
'on' => 'create'
)
)
);
$Article->create($data);
$this->assertFalse($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'update',
'on' => 'create'
)
)
);
$Article->create($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'create',
'on' => 'update'
)
)
);
$Article->create($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'update',
'on' => 'update'
)
)
);
$Article->create($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'create',
'on' => 'create'
)
)
);
$Article->save(null, array('validate' => false));
$data['Article']['id'] = $Article->id;
$Article->set($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'update',
'on' => 'create'
)
)
);
$Article->set($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'create',
'on' => 'update'
)
)
);
$Article->set($data);
$this->assertTrue($Article->validates());
$Article->validate = array(
'title' => array(
'notempty' => array(
'rule' => 'notEmpty',
'required' => 'update',
'on' => 'update'
)
)
);
$Article->set($data);
$this->assertFalse($Article->validates());
}
}