Skip to content

Commit

Permalink
Added support for numeric nested fields type, isRequired, val and att…
Browse files Browse the repository at this point in the history
…ributes

    Updated tests too
  • Loading branch information
lilHermit committed Aug 6, 2017
1 parent 086dc61 commit ebad0f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/View/Form/ArrayContext.php
Expand Up @@ -177,7 +177,12 @@ public function val($field, $options = [])
return null;
}

return Hash::get($this->_context['defaults'], $field);
// 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));
}
}

/**
Expand All @@ -194,6 +199,9 @@ public function isRequired($field)
return false;
}
$required = Hash::get($this->_context['required'], $field);
if ($required === null) {
$required = Hash::get($this->_context['required'], $this->stripNesting($field));
}

return (bool)$required;
}
Expand Down Expand Up @@ -221,7 +229,11 @@ public function type($field)
if (!is_array($this->_context['schema'])) {
return null;
}

$schema = Hash::get($this->_context['schema'], $field);
if ($schema === null) {
$schema = Hash::get($this->_context['schema'], $this->stripNesting($field));
}

return isset($schema['type']) ? $schema['type'] : null;
}
Expand All @@ -237,10 +249,13 @@ public function attributes($field)
if (!is_array($this->_context['schema'])) {
return [];
}
$schema = (array)Hash::get($this->_context['schema'], $field);
$schema = Hash::get($this->_context['schema'], $field);
if ($schema === null) {
$schema = Hash::get($this->_context['schema'], $this->stripNesting($field));
}
$whitelist = ['length' => null, 'precision' => null];

return array_intersect_key($schema, $whitelist);
return array_intersect_key((array)$schema, $whitelist);
}

/**
Expand Down Expand Up @@ -273,4 +288,17 @@ public function error($field)

return Hash::get($this->_context['errors'], $field);
}

/**
* Strips out any numeric nesting like users.0.age
*
* @param string $field A dot separated path to check errors on
* @return string A string with stripped numeric nesting
*/
protected function stripNesting($field)
{
return implode('.', array_filter(explode('.', $field), function ($val) {
return !is_numeric($val);
}));
}
}
10 changes: 9 additions & 1 deletion tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -176,10 +176,12 @@ public function testValDefault()
$context = new ArrayContext($this->request, [
'defaults' => [
'title' => 'Default value',
'users' => ['tags' => 'common']
]
]);

$this->assertEquals('Default value', $context->val('title'));
$this->assertEquals('common', $context->val('users.0.tags'));
$result = $context->val('title', ['default' => 'explicit default']);
$this->assertEquals('explicit default', $result);
}
Expand All @@ -195,12 +197,14 @@ public function testIsRequired()
'required' => [
'Comments' => [
'required' => true,
'nope' => false
'nope' => false,
'tags' => true
]
]
]);
$this->assertTrue($context->isRequired('Comments.required'));
$this->assertFalse($context->isRequired('Comments.nope'));
$this->assertTrue($context->isRequired('Comments.0.tags'));
$this->assertFalse($context->isRequired('Articles.id'));
}

Expand All @@ -226,12 +230,14 @@ public function testType()
'schema' => [
'Comments' => [
'id' => ['type' => 'integer'],
'tags' => ['type' => 'string'],
'comment' => ['length' => 255]
]
]
]);
$this->assertNull($context->type('Comments.undefined'));
$this->assertEquals('integer', $context->type('Comments.id'));
$this->assertEquals('string', $context->type('Comments.0.tags'));
$this->assertNull($context->type('Comments.comment'));
}

Expand Down Expand Up @@ -260,10 +266,12 @@ public function testAttributes()
'comment' => ['type' => 'string', 'length' => 255],
'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
'tags' => ['type' => 'string', 'length' => 25],
]
]
]);
$this->assertEquals([], $context->attributes('Comments.id'));
$this->assertEquals(['length' => 25], $context->attributes('Comments.0.tags'));
$this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
$this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
$this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
Expand Down

0 comments on commit ebad0f2

Please sign in to comment.