Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 5190 pt1 #5206

Merged
merged 3 commits into from
Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ public function allowEmpty($field, $when = true) {
* }}}
*
* It is possible to conditionally disallow emptiness on a field by passing a callback
* as a second argument. The callback will receive the validation context array as
* as the third argument. The callback will receive the validation context array as
* argument:
*
* {{{
* $validator->notEmpty('email', function ($context) {
* return $context['newRecord'] && $context['data']['role'] !== 'admin';
* $validator->notEmpty('email', 'Email is required', function ($context) {
* return $context['newRecord'] && $context['data']['role'] !== 'admin';
* });
* }}}
*
Expand Down
3 changes: 2 additions & 1 deletion src/View/Form/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ protected function _getEntity($path) {
$isLast = ($i === $last);

if (!$isLast && $next === null && $prop !== '_ids') {
return new Entity();
$table = $this->_getTable($path);
return $table->newEntity();
}

$isTraversable = (
Expand Down
42 changes: 42 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
* Test stub.
*/
class Article extends Entity {

/**
* Testing stub method.
*
* @return bool
*/
public function isRequired() {
return true;
}

}

/**
Expand Down Expand Up @@ -647,6 +657,37 @@ public function testIsRequiredAssociatedHasMany() {
$this->assertFalse($context->isRequired(''));
}

/**
* Test isRequired on associated entities with custom validators.
*
* Ensures that missing associations use the correct entity class
* so provider methods work correctly.
*
* @return void
*/
public function testIsRequiredAssociatedCustomValidator() {
$this->_setupTables();
$users = TableRegistry::get('Users');
$articles = TableRegistry::get('Articles');

$validator = $articles->validator();
$validator->notEmpty('title', 'nope', function ($context) {
return $context['providers']['entity']->isRequired();
});
$articles->validator('default', $validator);

$row = new Entity([
'username' => 'mark'
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Users',
'validator' => 'default',
]);

$this->assertTrue($context->isRequired('articles.0.title'));
}

/**
* Test isRequired on associated entities.
*
Expand Down Expand Up @@ -943,6 +984,7 @@ protected function _setupTables() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$articles->hasMany('Comments');
$articles->entityClass(__NAMESPACE__ . '\Article');

$comments = TableRegistry::get('Comments');
$users = TableRegistry::get('Users');
Expand Down