H
H
hollanditkzn2017-09-19 16:17:16
Yii
hollanditkzn, 2017-09-19 16:17:16

How to write code validation validation?

Each employee has his own confirmation code, the question is how to implement it?
In more detail, there is a database with personnel, where each has a unique id. and a confirmation code is attached to it. Only the second question is how to do it in order to make validation about this.
I tried to do my own validation, only there the question arose, how to pass exactly the values ​​\u200b\u200bthat are needed for a particular employee to a variable?
Then the code I have is
in the controller

public function actionSetting($id)
    {
...
        $personnel = new Personnel();// модель персонала
        $formSotrud = new SotrudForm();//модель формы для подтверждение кода


        return $this->render('setting', [
...
            'personnel' => $personnel,
            'formSotrud' => $formSotrud,
        ]);

    }

in view
<?php $form = ActiveForm::begin([
        'id' => 'form-startShift',
]); ?>

<?= $form->field($formSotrud, 'sotrud')->dropDownList(ArrayHelper::map(Personnel::find()->where(['action' => 0])->all(), 'id', 'nameSotrud'),//Список сотрудников, получаем его id
    [
        'prompt' => 'Выберите сотрудника',
    ])->label(false) ?>

<?= $form->field($formSotrud, 'password')->passwordInput()->label(false) ?>//код подтверждение пишет

<?= Html::submitButton('Применить', ['class' => 'btn action']) ?>

<?php ActiveForm::end(); ?>

In SotrudForm
class SotrudForm extends Model{
    public $sotrud;
    public $password;

    private $user;

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['sotrud', 'password'], 'required'],
            ['password', 'validatePasswordSotrud'],
        ];
    }

    public function validatePasswordSotrud()
    {
        $this->user = Personnel::findOne($this->sotrud);

        if (!$this->user || $this->user->validatePasswordSotrud($this->password)){
            $this->addError('password', 'Неправильный код подтверждение');
        }
    }

}

I know it's wrong, but about how to write code while I understand it

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question