Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有动态消息的Laravel自定义验证规则_Php_Laravel_Laravel 5_Laravel 5.1 - Fatal编程技术网

Php 带有动态消息的Laravel自定义验证规则

Php 带有动态消息的Laravel自定义验证规则,php,laravel,laravel-5,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.1,我请求添加一个带有自定义验证方法的节目,该方法检查是否已经存在与日期和时间冲突的节目。如果方法失败,我想在验证错误消息中说明它与哪个显示冲突 class ShowStoreRequest extends Request { public function rules() { Validator::extend('time_clash_check', function($attribute, $value, $parameters) {

我请求添加一个带有自定义验证方法的节目,该方法检查是否已经存在与日期和时间冲突的节目。如果方法失败,我想在验证错误消息中说明它与哪个显示冲突

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}/']
        ];
    }
}
类ShowStoreRequest扩展请求{
公共职能规则()
{
验证器::扩展('time\u clash\u check',函数($attribute,$value,$parameters)
{
列表($day、$month、$year)=分解(“/”,$value);
$date=$year.'-'.$month.'-'.$day;
$start_time=$parameters[0];
$end_time=$parameters[1];
$show_id=$parameters[2];
$query=Show::where('date','=',$date)->where(函数($q)使用($start\u time,$end\u time){
//显示与另一个显示的开始重叠
$q->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','>=',$start\u time)
->其中('start_time','=',$end_time);
/演出在另一场演出的中间。
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$end\u time);
//“显示”与另一个显示的结尾重叠
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$start\u time)
->其中('end_time'、'='、$start_time)

->其中('end_time','将其添加到您的请求文件中:

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'
    ];
}
类ShowStoreRequest扩展请求{
存在公共资金;
公共职能规则()
{
验证器::扩展('time\u clash\u check',函数($attribute,$value,$parameters)
{
列表($day、$month、$year)=分解(“/”,$value);
$date=$year.'-'.$month.'-'.$day;
$start_time=$parameters[0];
$end_time=$parameters[1];
$show_id=$parameters[2];
$query=Show::where('date','=',$date)->where(函数($q)使用($start\u time,$end\u time){
//显示与另一个显示的开始重叠
$q->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','>=',$start\u time)
->其中('start_time','=',$end_time);
/演出在另一场演出的中间。
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$end\u time);
//“显示”与另一个显示的结尾重叠
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$start\u time)
->其中('end_time'、'='、$start_time)
->where('end_time','where(function($q)use($start_time,$end_time){
//显示与另一个显示的开始重叠
$q->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','>=',$start\u time)
->其中('start_time','=',$end_time);
/演出在另一场演出的中间。
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$end\u time);
//“显示”与另一个显示的结尾重叠
})->orWhere(函数($q)使用($start\u time,$end\u time){
$q->where('start\u time','=',$start\u time)
->其中('end_time'、'='、$start_time)

->其中('end_time',将输出我正在创建的节目的名称。我想输出与之冲突的现有节目的名称。对不起,我没有理解。更新了我的帖子。我将$this->exists设置为$existing_show->name,但是如果“name”,则需要更正该值是错误的。我在猜测。我在发布问题之前尝试了这种方法,但我认为messages方法在验证规则之前运行,因此exists属性在那一点上不存在。嗯,您使用的是什么版本?刚刚测试了它,它正常工作。@geoffs3310我再次更新了我的帖子。我使用了替换程序,它修复了问题。
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'
    ];
}