Skip to content
Permalink
Browse files

add messege test to allowEmpty when set as array

small code refactor
  • Loading branch information...
Graziel committed May 12, 2016
1 parent 76ec4e0 commit d9f362e7ff5da39812a84373b7375084675411be
Showing with 42 additions and 21 deletions.
  1. +24 −19 src/Validation/Validator.php
  2. +18 −2 tests/TestCase/Validation/ValidatorTest.php
@@ -442,12 +442,17 @@ public function remove($field, $rule = null)
*/
public function requirePresence($field, $mode = true, $message = null)
{
$settingsDefault = [
'mode' => $mode,
'message' => $message
];
if (!is_array($field)) {
$field = $this->_convertValidatorToArray($field, ['mode', 'message'], [$mode, $message]);
$field = $this->_convertValidatorToArray($field, $settingsDefault);
}
foreach ($field as $fieldName => $setting) {
$settings = $this->_convertValidatorToArray($fieldName, ['mode', 'message'], [$mode, $message], $setting);
$settings = $this->_convertValidatorToArray($fieldName, $settingsDefault, $setting);
$fieldName = current(array_keys($settings));
$this->field($fieldName)->isPresenceRequired($settings[$fieldName]['mode']);
@@ -518,12 +523,17 @@ public function requirePresence($field, $mode = true, $message = null)
*/
public function allowEmpty($field, $when = true, $message = null)
{
$settingsDefault = [
'when' => $when,
'message' => $message
];
if (!is_array($field)) {
$field = $this->_convertValidatorToArray($field, ['when', 'message'], [$when, $message]);
$field = $this->_convertValidatorToArray($field, $settingsDefault);
}
foreach ($field as $fieldName => $setting) {
$settings = $this->_convertValidatorToArray($fieldName, ['when', 'message'], [$when, $message], $setting);
$settings = $this->_convertValidatorToArray($fieldName, $settingsDefault, $setting);
$fieldName = current(array_keys($settings));
$this->field($fieldName)->isEmptyAllowed($settings[$fieldName]['when']);
@@ -538,12 +548,11 @@ public function allowEmpty($field, $when = true, $message = null)
* Converts validator to fieldName => $settings array
*
* @param int|string $fieldName name of field
* @param array $settingKeys keys that will be used to create default settings
* @param array $settingsValues values that will be used to create default settings
* @param array $settingDefaults default settings
* @param string|array $settings settings from data
* @return array
*/
protected function _convertValidatorToArray($fieldName, $settingKeys = [], $settingsValues = [], $settings = [])
protected function _convertValidatorToArray($fieldName, $settingDefaults = [], $settings = [])
{
if (is_string($settings)) {
$fieldName = $settings;
@@ -554,7 +563,7 @@ protected function _convertValidatorToArray($fieldName, $settingKeys = [], $sett
sprintf('Invalid field "%s" setting, must be an array.', $fieldName)
);
}
$settings += array_combine($settingKeys, $settingsValues);
$settings += $settingDefaults;
return [$fieldName => $settings];
}
@@ -618,21 +627,17 @@ protected function _convertValidatorToArray($fieldName, $settingKeys = [], $sett
*/
public function notEmpty($field, $message = null, $when = false)
{
$settingsDefault = [
'when' => $when,
'message' => $message
];
if (!is_array($field)) {
$field = $this->_convertValidatorToArray(
$field,
['when', 'message'],
[$when, $message]
);
$field = $this->_convertValidatorToArray($field, $settingsDefault);
}
foreach ($field as $fieldName => $setting) {
$settings = $this->_convertValidatorToArray(
$fieldName,
['when', 'message'],
[$when, $message],
$setting
);
$settings = $this->_convertValidatorToArray($fieldName, $settingsDefault, $setting);
$fieldName = current(array_keys($settings));
$whenSetting = $settings[$fieldName]['when'];
@@ -562,20 +562,36 @@ public function testAllowEmptyAsArray()
'title',
'subject',
'posted_at' => [
'when' => false
'when' => false,
'message' => 'Post time cannot be empty'
],
'updated_at' => [
'when' => true
],
'show_at' => [
'when' => 'update'
]
], 'create');
], 'create', 'Cannot be empty');
$this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
$this->assertEquals('create', $validator->field('subject')->isEmptyAllowed());
$this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
$this->assertTrue($validator->field('updated_at')->isEmptyAllowed());
$this->assertEquals('update', $validator->field('show_at')->isEmptyAllowed());
$errors = $validator->errors([
'title' => '',
'subject' => null,
'posted_at' => null,
'updated_at' => null,
'show_at' => ''
], false);
$expected = [
'title' => ['_empty' => 'Cannot be empty'],
'subject' => ['_empty' => 'Cannot be empty'],
'posted_at' => ['_empty' => 'Post time cannot be empty']
];
$this->assertEquals($expected, $errors);
}
/**

0 comments on commit d9f362e

Please sign in to comment.
You can’t perform that action at this time.