我如何在 laravel 中的模型中编写验证?
how do i write the validation in the model in laravel?
当我在控制器中验证 phone 数字 时,它正在工作,但它增加了代码行,我还必须编写回调函数,但是我不想写回调,而是想在模型中做,有什么办法吗??
'phone' =>['required',"regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/",function($attribute, $value, $fail) use($id) {
if (strpos($value, "-") !== false) {
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}else{
$result = User::where([["phone", str_replace("-","",$value)],["id","!=",$id]])->count();
($result) ? $fail(ucwords($attribute).' is already taken.') : "";
}
}else{
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}
}
},],
验证的最佳解决方案是提出单独且易于处理的自定义请求 =>
php artisan make:request CustomRequest
请求:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\User;
class CustomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'phone' => [
'required',
'min:10',
'max':10',
'regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/',
],
];
}
}
它将为您提供默认验证消息,但如果您想制作自定义消息,您可以通过制作 message()
https://laravel.com/docs/7.x/validation
希望对您有所帮助!
我认为您应该能够将模型中的函数定义为 returns 闭包的静态函数,这样您就可以调用它来获取闭包并将其作为回调传递。
// In the model
public static function myValidationClosure($id){
return function($attribute, $value, $fail)use($id) {
if (strpos($value, "-") !== false) {
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}else{
$result = User::where([["phone", str_replace("-","",$value)],["id","!=",$id]])->count();
($result) ? $fail(ucwords($attribute).' is already taken.') : "";
}
}else{
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}
}
};
}
然后你可以在验证中使用它作为
'phone' =>['required',"regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/", MyModelClass::myValidationClosure($id)]
当我在控制器中验证 phone 数字 时,它正在工作,但它增加了代码行,我还必须编写回调函数,但是我不想写回调,而是想在模型中做,有什么办法吗??
'phone' =>['required',"regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/",function($attribute, $value, $fail) use($id) {
if (strpos($value, "-") !== false) {
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}else{
$result = User::where([["phone", str_replace("-","",$value)],["id","!=",$id]])->count();
($result) ? $fail(ucwords($attribute).' is already taken.') : "";
}
}else{
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}
}
},],
验证的最佳解决方案是提出单独且易于处理的自定义请求 =>
php artisan make:request CustomRequest
请求:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\User;
class CustomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'phone' => [
'required',
'min:10',
'max':10',
'regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/',
],
];
}
}
它将为您提供默认验证消息,但如果您想制作自定义消息,您可以通过制作 message()
https://laravel.com/docs/7.x/validation
希望对您有所帮助!
我认为您应该能够将模型中的函数定义为 returns 闭包的静态函数,这样您就可以调用它来获取闭包并将其作为回调传递。
// In the model
public static function myValidationClosure($id){
return function($attribute, $value, $fail)use($id) {
if (strpos($value, "-") !== false) {
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}else{
$result = User::where([["phone", str_replace("-","",$value)],["id","!=",$id]])->count();
($result) ? $fail(ucwords($attribute).' is already taken.') : "";
}
}else{
$exist = User::where([["phone", $value],["id","!=",$id]])->count();
if($exist){
$fail(ucwords($attribute).' is already taken.');
}
}
};
}
然后你可以在验证中使用它作为
'phone' =>['required',"regex:/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/", MyModelClass::myValidationClosure($id)]