-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new validator * add tests * add tests * de language key * update docs * Text updates * German translation * add missing translation files * add changelog Co-authored-by: Roland Schaub <roland.schaub@zephir.ch>
- Loading branch information
1 parent
735dd72
commit 1d5faa8
Showing
19 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace luya\admin\validators; | ||
|
||
use luya\admin\models\Lang; | ||
use luya\admin\Module; | ||
use luya\helpers\Json; | ||
use yii\validators\Validator; | ||
|
||
/** | ||
* Validate Required i18n Attributes. | ||
* | ||
* Ensure the i18n attribute is correctly and also checks if all language keys are given and the values for each language is not empty. | ||
* | ||
* Example usage: | ||
* | ||
* ```php | ||
* [['title', 'location'], I18nRequiredValidator::class], | ||
* ``` | ||
* | ||
* In {{luya\admin\ngrest\base\NgRestModel}} scenarios it might be common to use | ||
* | ||
* ```php | ||
* [$this->i18n, I18nRequiredValidator::class], | ||
* ``` | ||
* | ||
* The validator will only validate if the attribute is available, therfore set the required validator if the attribute is required. | ||
* | ||
* @author Basil Suter <git@nadar.io> | ||
* @since 3.6.0 | ||
*/ | ||
class I18nRequiredValidator extends Validator | ||
{ | ||
/** | ||
* @var string Message if the input format is invalid. This message will be passed trough Yii::t. | ||
*/ | ||
public $invalidFormatMessage = "i18n_required_validator_invalid_format"; | ||
|
||
/** | ||
* @var string Message if an language key does not exists in the array. This message will be passed trough Yii::t. | ||
*/ | ||
public $missingKeyMessage = "i18n_required_validator_missing_key"; | ||
|
||
/** | ||
* @var string Message if a given value is empty. This message will be passed trough Yii::t. | ||
*/ | ||
public $emptyValueMessage = "i18n_required_validator_invalid_empty_value"; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validateAttribute($model, $attribute) | ||
{ | ||
$array = $model->{$attribute}; | ||
|
||
// As due to the ngrest plugin concept the value is already parsed from array to json. | ||
if (Json::isJson($array)) { | ||
$array = Json::decode($array); | ||
} | ||
|
||
if (!is_array($array)) { | ||
return $this->addError($model, $attribute, Module::t($this->invalidFormatMessage, ['attribute' => $attribute])); | ||
} | ||
|
||
/** @var $langShortCode The language short code */ | ||
foreach (Lang::find()->select(['short_code'])->asArray()->column() as $langShortCode) { | ||
if (!array_key_exists($langShortCode, $array)) { | ||
$this->addError($model, $attribute, Module::t($this->missingKeyMessage, ['lang' => $langShortCode])); | ||
} elseif (empty($array[$langShortCode])) { | ||
$this->addError($model, $attribute, Module::t($this->emptyValueMessage, ['lang' => $langShortCode])); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace luya\admin\tests\admin\validators; | ||
|
||
use admintests\AdminModelTestCase; | ||
use luya\admin\validators\I18nRequiredValidator; | ||
use luya\base\DynamicModel; | ||
|
||
class I18nRequiredValidatorTest extends AdminModelTestCase | ||
{ | ||
public function testValidatorInvalidFormat() | ||
{ | ||
$model = new DynamicModel(['i18n' => null]); | ||
$validator = new I18nRequiredValidator(); | ||
$validator->validateAttribute($model, 'i18n'); | ||
$this->assertSame('The given attribute \"i18n\" must be type of array.', $model->getFirstError('i18n')); | ||
} | ||
|
||
public function testValidatorMissingLanguage() | ||
{ | ||
$this->createAdminLangFixture([ | ||
1 => [ | ||
'id' => 1, | ||
'short_code' => 'en', | ||
'is_deleted' => 0, | ||
] | ||
]); | ||
$model = new DynamicModel(['i18n' => []]); | ||
$validator = new I18nRequiredValidator(); | ||
$validator->validateAttribute($model, 'i18n'); | ||
$this->assertSame('The language key \"en\" is missing and is required.', $model->getFirstError('i18n')); | ||
} | ||
|
||
public function testValidatorMissingLanguageAsJson() | ||
{ | ||
$this->createAdminLangFixture([ | ||
1 => [ | ||
'id' => 1, | ||
'short_code' => 'en', | ||
'is_deleted' => 0, | ||
] | ||
]); | ||
$model = new DynamicModel(['i18n' => '{}']); | ||
$validator = new I18nRequiredValidator(); | ||
$validator->validateAttribute($model, 'i18n'); | ||
$this->assertSame('The language key \"en\" is missing and is required.', $model->getFirstError('i18n')); | ||
} | ||
|
||
public function testValidatorEmptyLanguage() | ||
{ | ||
$this->createAdminLangFixture([ | ||
1 => [ | ||
'id' => 1, | ||
'short_code' => 'en', | ||
'is_deleted' => 0, | ||
] | ||
]); | ||
$model = new DynamicModel(['i18n' => ['en' => '']]); | ||
$validator = new I18nRequiredValidator(); | ||
$validator->validateAttribute($model, 'i18n'); | ||
$this->assertSame('The value for language \"en\" can not be empty.', $model->getFirstError('i18n')); | ||
} | ||
} |