Skip to content

Commit

Permalink
Improved the stripNesting method
Browse files Browse the repository at this point in the history
Added changes requested by ADmad
  • Loading branch information
lilHermit committed Aug 7, 2017
1 parent ebad0f2 commit a33a035
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/View/Form/ArrayContext.php
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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);
}
}
8 changes: 6 additions & 2 deletions tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -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);
}
Expand Down

0 comments on commit a33a035

Please sign in to comment.