3
votes

This is my model contacts,

public function rules()
{
    return [
        [['fname', 'status','mobile',], 'required'],
        [['user_id', 'contact_type','group_type','passport_no','trip_id','group_id','state',], 'integer'],
        [['fname', 'mname', 'lname', 'status', 'street', 'location', 'post', 'city', 'district','email','job_title','company_name','source'], 'string', 'max' => 500],
        [['department'], 'string', 'max' => 40],
        [['pincode'], 'string', 'max' => 15],
        [['mobile'],'unique'],

Here the required validation of mobile works and show error message, but unique rule works but it doesn't show any error message

Help me plz.

5
Can you show the form that you are using? - Joe Miller

5 Answers

3
votes

1) Unique validation work with database.
2) Once form was submitted then validation was checked and model not saved because of unique validation fails.
3) If you want to unique validation on form then use Ajax.

For this, You need to set 'enableAjaxValidation' => true,

$form = ActiveForm::begin([
    'id' => 'contact-form',
     'enableAjaxValidation' => true,
]);

Controller:

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) 
{
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}

Reference

2
votes

Maybe you should also set targetClass:

['email', 'unique', 'targetClass' => '\common\models\User']
2
votes

I am assuming your view page as register.php. So,

register.php (View)

1) Remove use yii\bootstrap\ActiveForm; (if present)

2) Add use yii\widgets\ActiveForm;

3) Add 'enableAjaxValidation' => true in that field (Where you are validating it. Like below)

<?= $form->field($model, 'mobile',['enableAjaxValidation' => true])->textInput(['class'=>'form-control']) ?>

Controller

See below (Add this line wherever I have written //Add This Line)

.
.
use yii\web\Response; // Add This line
use yii\widgets\ActiveForm; //Add This Line
.
.
Class SomeClass 
{
    .
    .
    .   

    public function actionRegister() {

     $model = new Candidate();

     //Add This For Ajax Mobile Exist Validation 
     if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
       Yii::$app->response->format = Response::FORMAT_JSON;
       return ActiveForm::validate($model);
     } 

     else if ($model->load(Yii::$app->request->post())) {
        .
        // Your code
        .
        .
     }
  }
}

Model

[['mobile'],'unique','message'=>'Mobile No Already Exist'],

For Reference - Click Validate unique value from database in Yii2

2
votes
if ($model->validate()) {
    //SAVE YOUR DATA
} else {
    // HERE YOU CAN PRINT THE ERRORS OF MODEL
    $data = $model->getErrors();
    Yii::$app->session->setFlash('message', $data['password'][0]);// its dislplays error msg on your form
}
0
votes

This rule in my model worked for me:

['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => '\app\models\Student', 'message' => 'This email address has already been taken.'],