如何验证yii2中的参数?
我试试,但没有工作验证:
我希望在BaseData参数中进行验证 - $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 уже существует.'],
];
}
}
也许验证是错误的?
答案 0 :(得分:1)
如果要验证自定义数据,则需要向模型添加自定义属性并添加规则。
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');
}
然后在模型中
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'],
];
}
}
答案 1 :(得分:0)
$model = new Mymodel();
$model->key_active = $key_active;
$model->login = $login;