This validator takes value from one attribute and puts transliterated value to another attribute. Also, validator may prepare transliterated string for usage in URL. Transliteration is made by URLify.
Install validator with Composer:
composer require --prefer-dist "herroffizier/yii2-translit-validator:@stable"
Add validator to your model's rules array before required
validator (if any) and set its sourceAttribute
property to
point source attribute which value should be transliterated.
use herroffizier\yii2tv\TranslitValidator;
...
public function rules()
{
return [
[['attribute'], 'required'],
[
['attribute_translit'],
TranslitValidator::className(),
'sourceAttribute' => 'attribute'
],
[['attribute_translit'], 'required'],
];
}
Validator has a few options to customize its behavior.
sourceAttribute
as mentioned above points to source attribute which value should be transliterated. Empty by default and required.lowercase
enforces lower case for transliterated string. Default istrue
.forUrl
replaces all invalid characters withinvalidReplacement
value. Default istrue
.invalidReplacement
is a replacement for invalid characters. Used in conjunction withforUrl
. Default is-
.invalidRegexp
is a regular expression which matches all incorrect symbols for URL. Used in conjunction withforUrl
. Default is/[^a-z0-9]+/i
which matches all non-alphanumeric symbols.trimInvalid
trims invalid characters at beginning and at end of given string. Used in conjunction withforUrl
. Default isfalse
which means that no characters will be trimmed.