herroffizier/yii2-translit-validator

Yii2 validator that transliterates model attribute values.

Installs: 25 461

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Type:yii2-extension

1.0.0 2016-03-29 12:35 UTC

This package is not auto-updated.

Last update: 2024-11-09 17:09:10 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Code Climate

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.

Installation

Install validator with Composer:

composer require --prefer-dist "herroffizier/yii2-translit-validator:@stable"

Usage

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 is true.
  • forUrl replaces all invalid characters with invalidReplacement value. Default is true.
  • invalidReplacement is a replacement for invalid characters. Used in conjunction with forUrl. Default is -.
  • invalidRegexp is a regular expression which matches all incorrect symbols for URL. Used in conjunction with forUrl. 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 with forUrl. Default is false which means that no characters will be trimmed.