Skip to content

Commit

Permalink
Merge pull request #5790 from cakephp/3.0-ruleschecker-translation
Browse files Browse the repository at this point in the history
3.0 ruleschecker translation
  • Loading branch information
lorenzo committed Jan 30, 2015
2 parents 48f2b54 + 30c834c commit 1ab551d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/ORM/RulesChecker.php
Expand Up @@ -99,6 +99,13 @@ class RulesChecker
*/
protected $_options = [];

/**
* Whether or not to use I18n functions for translating default error messages
*
* @var bool
*/
protected $_useI18n = false;

/**
* Constructor. Takes the options to be passed to all rules.
*
Expand All @@ -107,6 +114,7 @@ class RulesChecker
public function __construct(array $options)
{
$this->_options = $options;
$this->_useI18n = function_exists('__d');
}

/**
Expand Down Expand Up @@ -298,8 +306,16 @@ public function checkDelete(EntityInterface $entity, array $options = [])
* @param string $message The error message to show in case the rule does not pass.
* @return callable
*/
public function isUnique(array $fields, $message = 'This value is already in use')
public function isUnique(array $fields, $message = null)
{
if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value is already in use');
} else {
$message = 'This value is already in use';
}
}

$errorField = current($fields);
return $this->_addError(new IsUnique($fields), compact('errorField', 'message'));
}
Expand All @@ -324,8 +340,16 @@ public function isUnique(array $fields, $message = 'This value is already in use
* @param string $message The error message to show in case the rule does not pass.
* @return callable
*/
public function existsIn($field, $table, $message = 'This value does not exist')
public function existsIn($field, $table, $message = null)
{
if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value does not exist');
} else {
$message = 'This value does not exist';
}
}

$errorField = $field;
return $this->_addError(new ExistsIn($field, $table), compact('errorField', 'message'));
}
Expand Down

0 comments on commit 1ab551d

Please sign in to comment.