0

I have a App where I want the combination of three fields: rollyear, rollfaculty and rollno in a table to be unique. For that, I did the following in the migration file:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('rollyear');
            $table->string('rollfaculty');
            $table->string('rollno');
            $table->unique(['rollyear','rollfaculty','rollno']);
        });

This is working as expected but whenever duplicate values are supplied to the specified fields, I get a full page Integrity constraint violation error.
How can I handle this error and give a simplified error message to the user as in following image?Error Message

Xitish
  • 298
  • 5
  • 18
  • I think the only way to get a verified error is by checking the email address against your database. Although duplicate error returns error code 1062. – Rotimi Dec 14 '18 at 05:41
  • 1
    You can handle unique validation gracefully by using Laravel's validator. To validate uniqueness on the three columns use `Rule::unique`, see https://stackoverflow.com/a/50350824/2797224 – Adam Rodriguez Dec 14 '18 at 05:58

2 Answers2

0

add validate through jQuery

save this file on your public/js folder

Here's a link!

and ad another validation file for register

// validate signup form on keyup and submit
$("#addUserForm").validate({
            rules: {
                name:{
                    required:true
                },
                email: {
                    required: true,
                    email: true,
                    remote: {
                        url: "/checkuser",
                        type: "get"
                    }
                },
                password: {
                    required: true,
                    minlength: 5
                },
                image: {
                    required:true,
                    accept: "jpg|jpeg|png|JPG|JPEG|PNG"
                }
            },
            messages: {
                name: {
                    required:"Please enter a  name"
                },
                email: {
                    required:"Please enter a  email address",
                    email:"Please enter a valid email address",
                    remote:"Email Already Exist"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                image: {
                    required: "Please provide a profile image",
                    accept: "Please upload only jpg,jpeg,png"
                }
            }
});

add /checkuser

public function checkUser()
{
    $user = User::where('email', request('email'))->exists();
     if($user){
        return 'false';
     }
     else {
        return 'true';
     }
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jay Suchak
  • 172
  • 1
  • 10
0

Please put below code in your controller where you store users details.

$chkUniqueEmailId = DB::table('users')->where('email', '=', $request->email)->first();
    if(!empty($chkUniqueEmailId)){
      return redirect()->route('auth.register')->withFlashDanger("This email address already exists.");
    }