Php YII2自定义验证无法使用自定义函数模型规则
我试图在模型中使用自定义函数实现它,但它不起作用。我没有发现代码中的错误。我正试着打电话给basic,稍后我会把我的情况告诉你 这是模型代码Php YII2自定义验证无法使用自定义函数模型规则,php,yii2,yii2-advanced-app,yii2-validation,Php,Yii2,Yii2 Advanced App,Yii2 Validation,我试图在模型中使用自定义函数实现它,但它不起作用。我没有发现代码中的错误。我正试着打电话给basic,稍后我会把我的情况告诉你 这是模型代码 public function rules() { return [ ['mobile_number', 'required'], ['mobile_number', 'myfunction'], ]; } public function myfunctio
public function rules()
{
return [
['mobile_number', 'required'],
['mobile_number', 'myfunction'],
];
}
public function myfunction($attribute,$params)
{
$this->addError($attribute, 'You have already submitted');
}
这是控制器代码
public function actionCreate()
{
$model = new Createuser();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
它不会将错误分配给表单字段
Thanks in advance.
您确定没有扩展模型类吗?如果是这样的话,你需要把这个:
$model->validate()
您确定没有扩展模型类吗?如果是这样的话,你需要把这个:
$model->validate()
您的型号:
public function rules()
{
return [
['mobile_number', 'required'],
['mobile_number', 'myfunction'],
];
}
public function myfunction($attribute,$params)
{
$this->addError($attribute, 'You have already submitted');
return false;
}
和您的控制器:
public function actionCreate()
{
$model = new Createuser();
if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
您的型号:
public function rules()
{
return [
['mobile_number', 'required'],
['mobile_number', 'myfunction'],
];
}
public function myfunction($attribute,$params)
{
$this->addError($attribute, 'You have already submitted');
return false;
}
和您的控制器:
public function actionCreate()
{
$model = new Createuser();
if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
试试这个
在控制器中
protected function performAjaxValidation($model = NULL) {
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validate($model));
Yii::$app->end();
}
}
public function actionCreate()
{
$model = new Createuser();
$this->performAjaxValidation($model);
if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
试试这个
在控制器中
protected function performAjaxValidation($model = NULL) {
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validate($model));
Yii::$app->end();
}
}
public function actionCreate()
{
$model = new Createuser();
$this->performAjaxValidation($model);
if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
代码很好。动态表单验证出错。代码正常。动态表单验证时出错。我猜数据中不存在名为
mobile\u number
的参数,或者该参数是空字符串,请尝试在代码中添加'skipOnEmpty'=>false
不管怎么说,这对我很有用
public function rules()
{
return [
['mobile_number', 'required', 'skipOnEmpty' => false],
['mobile_number', 'myfunction', 'skipOnEmpty' => false],
];
}
我猜名为
mobile\u number
的参数在您的数据中不存在,或者它是一个空字符串,因此请尝试在代码中添加'skipOnEmpty'=>false
不管怎么说,这对我很有用
public function rules()
{
return [
['mobile_number', 'required', 'skipOnEmpty' => false],
['mobile_number', 'myfunction', 'skipOnEmpty' => false],
];
}
请输入第一个手机号码,然后提交表单,然后您可以在提交表单时显示msgi需要显示的错误。表单中的表单字段也将重置。pjax不工作。请输入第一个手机号码,然后提交表单,然后您可以显示错误msgi需要显示,同时提交表单中的表单字段也会重置。pjax不工作。我的应用程序中也有同样的问题,你解决了吗?有什么想法吗?是的@Clyff我修复了我的应用程序中的相同问题,你解决了吗?有什么想法吗?是的@Clyff我已经解决了