mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-05 02:52:41 +00:00
Added support for 'on' validation key in FormHelper. Fixes #2169.
This commit is contained in:
parent
c9528b8446
commit
cabb0d00ac
2 changed files with 161 additions and 2 deletions
|
@ -104,6 +104,9 @@ class Contact extends CakeTestModel {
|
|||
'non_existing' => array(),
|
||||
'idontexist' => array(),
|
||||
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
|
||||
'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
|
||||
'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
|
||||
'imrequiredonboth' => array('required' => array('rule' => 'alphaNumeric')),
|
||||
'string_required' => 'notEmpty',
|
||||
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
|
||||
'imrequiredtoo' => array('rule' => 'notEmpty'),
|
||||
|
@ -610,7 +613,8 @@ class TestMail extends CakeTestModel {
|
|||
* FormHelperTest class
|
||||
*
|
||||
* @package cake
|
||||
* @package Cake.Test.Case.View.Helper
|
||||
* @subpackage Cake.Test.Case.View.Helper
|
||||
* @property FormHelper $Form
|
||||
*/
|
||||
class FormHelperTest extends CakeTestCase {
|
||||
|
||||
|
@ -7845,4 +7849,134 @@ class FormHelperTest extends CakeTestCase {
|
|||
$result = $this->Form->error('Thing.field', null, array('wrap' => false));
|
||||
$this->assertEquals('Badness!', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the 'on' key validates as expected on create
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRequiredOnCreate() {
|
||||
$this->Form->create('Contact');
|
||||
|
||||
$result = $this->Form->input('Contact.imrequiredonupdate');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text'),
|
||||
'label' => array('for' => 'ContactImrequiredonupdate'),
|
||||
'Imrequiredonupdate',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
|
||||
'id' => 'ContactImrequiredonupdate'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Contact.imrequiredoncreate');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequiredoncreate'),
|
||||
'Imrequiredoncreate',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
|
||||
'id' => 'ContactImrequiredoncreate'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Contact.imrequiredonboth');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequiredonboth'),
|
||||
'Imrequiredonboth',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
|
||||
'id' => 'ContactImrequiredonboth'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Contact.imrequired');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequired'),
|
||||
'Imrequired',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequired]',
|
||||
'id' => 'ContactImrequired'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the 'on' key validates as expected on update
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRequiredOnUpdate() {
|
||||
$this->Form->request->data['Contact']['id'] = 1;
|
||||
$this->Form->create('Contact');
|
||||
|
||||
$result = $this->Form->input('Contact.imrequiredonupdate');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequiredonupdate'),
|
||||
'Imrequiredonupdate',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
|
||||
'id' => 'ContactImrequiredonupdate'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
$result = $this->Form->input('Contact.imrequiredoncreate');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text'),
|
||||
'label' => array('for' => 'ContactImrequiredoncreate'),
|
||||
'Imrequiredoncreate',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
|
||||
'id' => 'ContactImrequiredoncreate'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Contact.imrequiredonboth');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequiredonboth'),
|
||||
'Imrequiredonboth',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
|
||||
'id' => 'ContactImrequiredonboth'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Contact.imrequired');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text required'),
|
||||
'label' => array('for' => 'ContactImrequired'),
|
||||
'Imrequired',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Contact][imrequired]',
|
||||
'id' => 'ContactImrequired'
|
||||
),
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -249,8 +249,11 @@ class FormHelper extends AppHelper {
|
|||
}
|
||||
|
||||
foreach ($validateProperties as $rule => $validateProp) {
|
||||
if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) {
|
||||
$parse = $this->_parseRuleRequired($validateProp);
|
||||
if ($parse === false) {
|
||||
return false;
|
||||
} elseif ($parse == null){
|
||||
continue;
|
||||
}
|
||||
$rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
|
||||
$required = $rule || empty($validateProp);
|
||||
|
@ -2584,4 +2587,26 @@ class FormHelper extends AppHelper {
|
|||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the field is required by the 'on' key in validation properties.
|
||||
* If no 'on' key is present in validation props, this method returns true.
|
||||
*
|
||||
* @param array $validateProp
|
||||
* @return mixed. Boolean for required, null if create/update does not match
|
||||
*/
|
||||
protected function _parseRuleRequired($validateProp) {
|
||||
if (isset($validateProp['on'])) {
|
||||
if (
|
||||
($validateProp['on'] == 'create' && $this->requestType != 'post') ||
|
||||
($validateProp['on'] == 'update' && $this->requestType != 'put')
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue