1

How validate parameters in yii2?

I try, but not work validate:

I want validate in BaseData parameters - $key_active = '0', $login = '0'

class MyController extends Controller
{
    public function actionMy($key_active = '0', $login = '0')
    {

        $model = new Mymodel();
        if($model->validate()){

           return $this->render('yes');
        }

    return $this->render('no');
    }

}

class Mymodel extends Model
{
    public $login;

    public function rules()
    {
        return [
            [['login'], 'unique', 'targetClass' => '\app\models\Account', 'message'=>'Этот email уже существует.'],

        ];
    }


}

Maybe it's the wrong thing to be validated?

2 Answers 2

1

If you want to validate custom data, you need to add custom properties to model and add it rules.

public function actionMy($key_active = '0', $login = '0')
{

    $model = new Mymodel();
    $model->key_active = $key_active;
    $modle->login = $login;
    if($model->validate()){
       return $this->render('yes');
    }

    return $this->render('no');
}

then in model

class Mymodel extends Model
{
    public $login;
    public $key_active;

    public function rules()
    {
        return [
            ['login', 'unique', 'targetClass' => '\app\models\Account', 'message'=>'Этот email уже существует.'],
            ['key_active', 'YOUR_VALIDATION_RULES_HERE'],
        ];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks You. Have a nice on)
0
$model = new Mymodel();

$model->key_active = $key_active;
$model->login = $login;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.