Skip to content

Commit

Permalink
Added support for ArrayContext
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Mar 20, 2018
1 parent 7771f1f commit a5ab64d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
22 changes: 18 additions & 4 deletions src/Validation/Validator.php
Expand Up @@ -30,6 +30,20 @@
*/
class Validator implements ArrayAccess, IteratorAggregate, Countable
{
/**
* Default empty message
*
* @var string
*/
const DEFAULT_EMPTY_MESSAGE = 'This field cannot be left empty';

/**
* Default required message
*
* @var string
*/
const DEFAULT_REQUIRED_MESSAGE = 'This field is required';

/**
* Used to flag nested rules created with addNested() and addNestedMany()
*
Expand Down Expand Up @@ -1937,9 +1951,9 @@ public function regex($field, $regex, $message = null, $when = null)
*/
public function getRequiredMessage($field)
{
$defaultMessage = 'This field is required';
$defaultMessage = static::DEFAULT_REQUIRED_MESSAGE;
if ($this->_useI18n) {
$defaultMessage = __d('cake', 'This field is required');
$defaultMessage = __d('cake', $defaultMessage);
}

return isset($this->_presenceMessages[$field])
Expand All @@ -1955,9 +1969,9 @@ public function getRequiredMessage($field)
*/
public function getEmptyMessage($field)
{
$defaultMessage = 'This field cannot be left empty';
$defaultMessage = static::DEFAULT_EMPTY_MESSAGE;
if ($this->_useI18n) {
$defaultMessage = __d('cake', 'This field cannot be left empty');
$defaultMessage = __d('cake', $defaultMessage);
}

return isset($this->_allowEmptyMessages[$field])
Expand Down
41 changes: 30 additions & 11 deletions src/View/Form/ArrayContext.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Http\ServerRequest;
use Cake\Utility\Hash;
use Cake\Validation\Validator;

/**
* Provides a basic array based context provider for FormHelper.
Expand All @@ -29,7 +30,8 @@
* will be used when there is no request data set. Data should be nested following
* the dot separated paths you access your fields with.
* - `required` A nested array of fields, relationships and boolean
* flags to indicate a field is required.
* flags to indicate a field is required. The value can also be a string to be used
* as the required error message
* - `schema` An array of data that emulate the column structures that
* Cake\Database\Schema\Schema uses. This array allows you to control
* the inferred type for fields and allows auto generation of attributes
Expand All @@ -53,7 +55,12 @@
* 'defaults' => [
* 'id' => 1,
* 'title' => 'First post!',
* ]
* ],
* 'required' => [
* 'id' => true, // will use default required message
* 'title' => 'Please enter a title',
* 'body' => false,
* ],
* ];
* ```
*/
Expand Down Expand Up @@ -194,24 +201,36 @@ public function val($field, $options = [])
* @return bool
*/
public function isRequired($field)
{
return (bool)$this->getRequiredMessage($field);
}

/**
* {@inheritDoc}
*/
public function getRequiredMessage($field)
{
if (!is_array($this->_context['required'])) {
return false;
return null;
}
$required = Hash::get($this->_context['required'], $field);
if ($required === null) {
$required = Hash::get($this->_context['required'], $this->stripNesting($field));
}

return (bool)$required;
}
if ($required === false) {
return null;
}

/**
* {@inheritDoc}
*/
public function getRequiredMessage($field)
{
return null;
if ($required === true) {
$required = Validator::DEFAULT_REQUIRED_MESSAGE;

if (function_exists('__d ')) {
$required = __d('cake', $required);
}
}

return $required;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -35,6 +35,23 @@ public function setUp()
$this->request = new ServerRequest();
}

public function testGetRequiredMessage()
{
$context = new ArrayContext($this->request, [
'required' => [
'Comments' => [
'required' => 'My custom message',
'nope' => false,
'tags' => true
]
]
]);

$this->assertSame('My custom message', $context->getRequiredMessage('Comments.required'));
$this->assertSame('This field is required', $context->getRequiredMessage('Comments.tags'));
$this->assertSame(null, $context->getRequiredMessage('Comments.nope'));
}

/**
* Test getting the primary key.
*
Expand Down

0 comments on commit a5ab64d

Please sign in to comment.