Skip to content

Commit

Permalink
shimming 2.x validation errors as array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed May 31, 2015
1 parent 9c5d6d4 commit 9f167b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/Model/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Table extends CoreTable {

public $modifiedField = 'modified';

public $validationDomain = 'default';

/**
* initialize()
*
Expand Down Expand Up @@ -140,7 +142,15 @@ public function validationDefault(Validator $validator) {
unset($rules[$key]['allowEmpty']);
}
if (isset($rule['message'])) {
$rules[$key]['message'] = __($rule['message']);
if (is_array($rule['message'])) {
$name = array_shift($rule['message']);
//$args = array_slice($rule['message'], 1);
$args = $this->_translateArgs($rule['message']);
$message = __d($this->validationDomain, $name, $args);
} else {
$message = __d($this->validationDomain, $rule['message']);
}
$rules[$key]['message'] = $message;
}

if (is_string($rule)) {
Expand All @@ -164,6 +174,21 @@ public function validationDefault(Validator $validator) {
return $validator;
}

/**
* Applies translations to validator arguments.
*
* @param array $args The args to translate
* @return array Translated args.
*/
protected function _translateArgs($args) {
foreach ((array)$args as $k => $arg) {
if (is_string($arg)) {
$args[$k] = __d($this->validationDomain, $arg);
}
}
return $args;
}

/**
* Shim to provide 2.x way of find('first') for easier upgrade.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Model/Table/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,21 @@ public function testBehaviorShims() {
*/
public function testValidationShims() {
$this->Wheels = TableRegistry::get('Wheels');

$wheel = $this->Wheels->newEntity(['position' => '']);
$this->assertNotSame([], $wheel->errors());
$result = $this->Wheels->save($wheel);
$this->assertFalse($result);

// i18n array validation setup
//$this->Wheels = TableRegistry::get('Wheels');
$wheel = $this->Wheels->newEntity(['position' => '12345678901234567890abc']);
$expected = [
'position' => [
'maxLength' => 'valErrMaxCharacters xyz 20'
]
];
$this->assertSame($expected, $wheel->errors());
$result = $this->Wheels->save($wheel);
$this->assertFalse($result);

Expand Down
9 changes: 8 additions & 1 deletion tests/test_app/TestApp/Model/Table/WheelsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ class WheelsTable extends Table {
'message' => 'Please insert sth',
'allowEmpty' => false,
'required' => false,
]
'last' => true,
],
'maxLength' => [
'rule' => ['maxLength', 20],
'message' => ['valErrMaxCharacters {0} {1}', 'xyz', 20], // testing i18n
'allowEmpty' => false,
'last' => true,
],
]
];

Expand Down

0 comments on commit 9f167b5

Please sign in to comment.