zoro.cut
zoro.cut

Reputation: 31

override core rules in yii2

I wanted to override core rule for number in yii2 to convert persian numbers to english numbers, and then validate them? these code uses for converting persian numbers to english numbers in php

function convert($string) {
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
    $num = range(0, 9);
    return str_replace($persian, $num, $string);
}

How can I apply these codes for doing convert before validation?I don't want to use beforeValidate in my model I wrote a componet but I don't know what function and where should be change?

Upvotes: 1

Views: 330

Answers (1)

Bizley
Bizley

Reputation: 18021

Add filter rule in the model.

public function rules()
{
    return [
        ['pers_number', 'filter', 'filter' => function ($value) {
            return str_replace(
                ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'], 
                range(0, 9),
                $value
            );
        }],
    ];
}

Upvotes: 1

Related Questions