But my problem is, that i want to save targets for a customer in a target database table. In these table i saved the target name and the customer id. Now i want check with a validation rule, if the target name for these customer is exists. Then i want show a error in the form.
The unique rule is not enough for me because I have to pass the customer id to the rule. Is this possible with yii2?
public function rules()
{
return [
[['target_name'], 'checkAddTarget'],
];
}
public function checkAddTarget($attribute, $params) {
if(self::find()->where(['target_name' => $this->$attribute, 'k_id'=>$this->k_id])->one()==true){
$this->addError($attribute, 'Targetname für '.$this->k_id.' bereits vorhanden');
return false;
}
}
If I validate the model in Controller, the rule is executed and a corresponding error message is generated, which I can read out with $model->errors.
But during form entry, this rule is not checked. It is only checked whether the field has an input or not. The rule is only used when saving with $model->save() in the controller.
Thanks Tommy, but i have check this and this also dosn’t work.
In the same formular i used a field for the ip-range. And i used a yii-rule for check the ip-range. This rule will be checked by the formular. But the own rule dosn’t.
If your validator works as expected in the server side, then you should be able to use it in ajaxValidation. Double check the source code in the _form.php. Does it enable the ajax validation for the ‘target_name’ field?
There are 2 kinds of validators: one that supports clientValidation and the other that doesn’t.
require, in, compare, string, number, … supports clientValidation
exist, unique, … doesn’t
When you write your own validator, you may want it to support clientValidation. You can do so if you can implement clientValidateAttribute() that delivers a javascript to validate the input value in the client side.
But when you have to access db in order to do validation, probably you can not implement client side validation. In that case, the only solution might be using ajaxValidation as @tri suggested.
Now i used ajax validation in a normal view site. All works fine.
But when i load the form with an ajax function, the ajax validation dosn’t work.
Here is my code:
The function who load the form:
public function actionAjax_userdataedit($show)
{
// if this is a ajax request
if (Yii::$app->request->getIsAjax()) {
$model = User::findOne(['id' => Yii::$app->user->identity->id]);
// render the form to add a new Target
$content=$this->renderPartial('ajax_userdataedit', [
'show' => $show,
'UserData' => User::loadUserDataArray(),
'model' => $model,
]);
// change the response format to JSON for the ajax function
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$data['content']=$content;
return $data;
} else {
// show error page
return $this->render('error');
}
}
This is an expected result when a form is rendered using renderPartial() through ajax response. The javascript for the ajax validation won’t be registered.