Skip to content
Permalink
Browse files

Add schema default checking for FormContext.

Closes #11267
  • Loading branch information...
ADmad committed Oct 4, 2017
1 parent efef498 commit ba8dfb1a9b284c335970a41ff9ff254e3189717c
Showing with 37 additions and 2 deletions.
  1. +22 −1 src/View/Form/FormContext.php
  2. +15 −1 tests/TestCase/View/Form/FormContextTest.php
@@ -94,7 +94,28 @@ public function val($field, $options = [])
return $val;
}
return $options['default'];
if ($options['default'] !== null || !$options['schemaDefault']) {
return $options['default'];
}
return $this->_schemaDefault($field);
}
/**
* Get default value from form schema for given field.
*
* @param string $field Field name.
* @return mixed
*/
protected function _schemaDefault($field)
{
$field = $this->_form->schema()->field($field);
if ($field) {
return $field['default'];
}
return null;
}
/**
@@ -111,10 +111,24 @@ public function testValMissing()
*/
public function testValDefault()
{
$context = new FormContext($this->request, ['entity' => new Form()]);
$form = new Form();
$form->schema()->addField('name', ['default' => 'schema default']);
$context = new FormContext($this->request, ['entity' => $form]);
$result = $context->val('title');
$this->assertNull($result);
$result = $context->val('title', ['default' => 'default default']);
$this->assertEquals('default default', $result);
$result = $context->val('name');
$this->assertEquals('schema default', $result);
$result = $context->val('name', ['default' => 'custom default']);
$this->assertEquals('custom default', $result);
$result = $context->val('name', ['schemaDefault' => false]);
$this->assertNull($result);
}
/**

0 comments on commit ba8dfb1

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