Skip to content
Permalink
Browse files

Improved the stripNesting method

Added changes requested by ADmad
  • Loading branch information...
lilHermit committed Aug 7, 2017
1 parent ebad0f2 commit a33a03553eb69a3724953381d7cbc8357325375b
Showing with 13 additions and 9 deletions.
  1. +7 −7 src/View/Form/ArrayContext.php
  2. +6 −2 tests/TestCase/View/Form/ArrayContextTest.php
@@ -180,9 +180,9 @@ public function val($field, $options = [])
// Using Hash::check here incase the default value is actually null
if (Hash::check($this->_context['defaults'], $field)) {
return Hash::get($this->_context['defaults'], $field);
} else {
return Hash::get($this->_context['defaults'], $this->stripNesting($field));
}
return Hash::get($this->_context['defaults'], $this->stripNesting($field));
}
/**
@@ -290,15 +290,15 @@ public function error($field)
}
/**
* Strips out any numeric nesting like users.0.age
* Strips out any numeric nesting
*
* For example users.0.age will output as users.age
*
* @param string $field A dot separated path to check errors on
* @param string $field A dot separated path
* @return string A string with stripped numeric nesting
*/
protected function stripNesting($field)
{
return implode('.', array_filter(explode('.', $field), function ($val) {
return !is_numeric($val);
}));
return preg_replace('/\.\d\./', '.', $field);
}
}
@@ -169,19 +169,23 @@ public function testValMissing()
/**
* Test getting default value
*
* Tests includes making sure numeric elements are stripped but not keys beginning with numeric
* value
*
* @return void
*/
public function testValDefault()
{
$context = new ArrayContext($this->request, [
'defaults' => [
'title' => 'Default value',
'users' => ['tags' => 'common']
'users' => ['tags' => 'common1', '9tags' => 'common2']
]
]);
$this->assertEquals('Default value', $context->val('title'));
$this->assertEquals('common', $context->val('users.0.tags'));
$this->assertEquals('common1', $context->val('users.0.tags'));
$this->assertEquals('common2', $context->val('users.0.9tags'));
$result = $context->val('title', ['default' => 'explicit default']);
$this->assertEquals('explicit default', $result);
}

0 comments on commit a33a035

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