我有一个应用程序,我想要三个字段的组合:rollyear
, rollfaculty
和 rollno
在一个表中是唯一的。为此,我在迁移文件中执行了以下操作:
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']);
});
这按预期工作,但每当向指定字段提供重复值时,我都会得到一整页 违反完整性约束 错误。
如何处理此错误并向用户提供简化的错误消息,如下图所示?
最佳答案
通过jQuery添加验证
将此文件保存在您的 public/js 文件夹中
这里是 a link !
并添加另一个验证文件以供注册
// 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"
}
}
});
添加/检查用户
public function checkUser()
{
$user = User::where('email', request('email'))->exists();
if($user){
return 'false';
}
else {
return 'true';
}
}
关于php - Laravel : Handle Integrity constraint violation error and return simplified error message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53773950/