php-具有动态消息的Laravel自定义验证规则
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-具有动态消息的Laravel自定义验证规则,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6203字,纯文字阅读大概需要9分钟。
内容图文
![php-具有动态消息的Laravel自定义验证规则](/upload/InfoBanner/zyjiaocheng/663/4e3d2773610042d69b1259fccaeee068.jpg)
我有一个请求,要求添加带有自定义验证方法的节目,以检查是否已存在与日期和时间冲突的节目.如果方法失败,我想说验证错误消息中显示的与之冲突的方法.
class ShowStoreRequest extends Request {
public function rules()
{
Validator::extend('time_clash_check', function($attribute, $value, $parameters)
{
list($day, $month, $year) = explode('/', $value);
$date = $year.'-'.$month.'-'.$day;
$start_time = $parameters[0];
$end_time = $parameters[1];
$show_id = $parameters[2];
$query = Show::where('date', '=', $date)->where(function($q) use ($start_time, $end_time) {
// show overlaps beginning of another show
$q->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('start_time', '<=', $end_time)
->where('end_time', '>=', $end_time);
// show is in the middle of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $end_time);
// show overlaps the end of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
// show completely overlaps another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
});
});
if(!is_null($show_id)) {
$query->where('id', '!=', $show_id);
}
$existing_show = $query->first();
if(is_null($existing_show)) {
return true;
} else {
return false;
}
});
$rules = [
'name' => 'required|max:255',
'show_code' => 'required|alpha_num|min:10|max:11',
'url_slug' => ['required', 'max:255', 'regex:/^[a-z0-9-_]+$/'],
'youtube_url' => 'url',
'name' => 'required|max:255',
'date' => ['regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', 'time_clash_check:'.$this->start_time.','.$this->end_time.','.$this->route('id')],
'start_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/'],
'end_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/']
];
}
}
因此,如果time_clash_check失败,我想在错误消息中输出$existing_show-> name的值,以告诉他们与其冲突的显示.我该怎么做?
解决方法:
将此添加到您的请求文件中:
class ShowStoreRequest extends Request {
public $exists;
public function rules()
{
Validator::extend('time_clash_check', function($attribute, $value, $parameters)
{
list($day, $month, $year) = explode('/', $value);
$date = $year.'-'.$month.'-'.$day;
$start_time = $parameters[0];
$end_time = $parameters[1];
$show_id = $parameters[2];
$query = Show::where('date', '=', $date)->where(function($q) use ($start_time, $end_time) {
// show overlaps beginning of another show
$q->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('start_time', '<=', $end_time)
->where('end_time', '>=', $end_time);
// show is in the middle of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $end_time);
// show overlaps the end of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
// show completely overlaps another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
});
});
if(!is_null($show_id)) {
$query->where('id', '!=', $show_id);
}
$existing_show = $query->first();
if(is_null($existing_show)) {
return true;
} else {
$this->exists = $existing_show->name;
return false;
}
});
$rules = [
'name' => 'required|max:255',
'show_code' => 'required|alpha_num|min:10|max:11',
'url_slug' => ['required', 'max:255', 'regex:/^[a-z0-9-_]+$/'],
'youtube_url' => 'url',
'name' => 'required|max:255',
'date' => ['regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', 'time_clash_check:'.$this->start_time.','.$this->end_time.','.$this->route('id')],
'start_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/'],
'end_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/']
];
}
}
public function messages()
{
return [
'time_clash_check ' => $this->exists . ' has clashed'
];
}
编辑:
好的,所以我找出了问题所在.解决方法如下:
class ShowStoreRequest extends Request {
public $exists;
public function rules()
{
Validator::extend('time_clash_check', function($attribute, $value, $parameters)
{
list($day, $month, $year) = explode('/', $value);
$date = $year.'-'.$month.'-'.$day;
$start_time = $parameters[0];
$end_time = $parameters[1];
$show_id = $parameters[2];
$query = Show::where('date', '=', $date)->where(function($q) use ($start_time, $end_time) {
// show overlaps beginning of another show
$q->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('start_time', '<=', $end_time)
->where('end_time', '>=', $end_time);
// show is in the middle of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $end_time);
// show overlaps the end of the another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '<=', $start_time)
->where('end_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
// show completely overlaps another show
})->orWhere(function($q) use ($start_time, $end_time) {
$q->where('start_time', '>=', $start_time)
->where('end_time', '<=', $end_time);
});
});
if(!is_null($show_id)) {
$query->where('id', '!=', $show_id);
}
$existing_show = $query->first();
if(is_null($existing_show)) {
return true;
} else {
$this->exists = $existing_show->name;
return false;
}
});
Validator::replacer('time_clash_check', function($message, $attribute, $rule, $parameters) {
return str_replace(':variable', $this->exists, $message);
});
$rules = [
'name' => 'required|max:255',
'show_code' => 'required|alpha_num|min:10|max:11',
'url_slug' => ['required', 'max:255', 'regex:/^[a-z0-9-_]+$/'],
'youtube_url' => 'url',
'name' => 'required|max:255',
'date' => ['regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', 'time_clash_check:'.$this->start_time.','.$this->end_time.','.$this->route('id')],
'start_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/'],
'end_time' => ['regex:/[0-9]{1,2}\:[0-9]{2}\:[0-9]{2}/']
];
}
}
public function messages()
{
return [
'time_clash_check ' => ':variable has clashed'
];
}
那应该解决问题.
内容总结
以上是互联网集市为您收集整理的php-具有动态消息的Laravel自定义验证规则全部内容,希望文章能够帮你解决php-具有动态消息的Laravel自定义验证规则所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。