Skip to content

Commit

Permalink
Allow the EntityContext to read from the request data.
Browse files Browse the repository at this point in the history
Reading from the request data is helpful when you are making forms, as
you might need to access data that has not been added to the entities
that compose the context.
  • Loading branch information
markstory committed Mar 5, 2014
1 parent 71c26e9 commit 01704a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/View/Form/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public function isCreate() {
* @return mixed The value of the field or null on a miss.
*/
public function val($field) {
$val = $this->_request->data($field);
if ($val !== null) {
return $val;
}
if (empty($this->_context['entity'])) {
return null;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,29 @@ public function testValBasic() {
$this->assertNull($result);
}

/**
* Test that val() reads from the request.
*
* @return void
*/
public function testValReadsRequest() {
$this->request->data = [
'title' => 'New title',
'notInEntity' => 'yes',
];
$row = new Entity([
'title' => 'Test entity',
'body' => 'Something new'
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
$this->assertEquals('New title', $context->val('title'));
$this->assertEquals('yes', $context->val('notInEntity'));
$this->assertEquals($row->body, $context->val('body'));
}

/**
* Test reading values from associated entities.
*
Expand Down

0 comments on commit 01704a4

Please sign in to comment.