H
H
hollanditkzn2017-08-11 18:38:36
Yii
hollanditkzn, 2017-08-11 18:38:36

How to convert value in validation?

Such a question, I have a widget with a MaskedInput currency format. There you drive in, let's say 101111120, in the input 10 111 120 '. Only when you send the form to save the value field writes the value must be a number. From the integer format, I switched to doulbe, but I still didn’t solve
the problem in the view

<?= $form->field($model, 'oplata')->widget(MaskedInput::className(), [
                    'clientOptions' => [
                        'alias' => 'decimal',
                        'groupSeparator' => ' ',
                        'autoGroup' => true,
                    ],
                'options' => ['placeholder' => 'Cтоимость', 'class' => 'inputWidget-form'],
            ])->label(false) ?>

In the model
public function rules()
    {
        return [
            [[ 'oplata'], 'number'],
        ];
    }

Roughly speaking, I need to specify in the validation $model->oplata = str_replace(" ", "", $model->oplata);

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-08-11
@hollanditkzn

As soon as spaces appear in a string, it can no longer be a number, it's a string.

['oplata', 'filter', 'filter' => function ($value) {
        return str_replace(" ", "", $value);
    }],

Read the docs:
www.yiiframework.com/doc-2.0/guide-tutorial-core-v...
every hour of reading the documentation saves 100 hours spent looking for solutions, strange solutions and fixing problems associated with them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question