Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 为什么在未填写必填字段时不显示验证消息?_Php_Laravel - Fatal编程技术网

Php 为什么在未填写必填字段时不显示验证消息?

Php 为什么在未填写必填字段时不显示验证消息?,php,laravel,Php,Laravel,我有下面的注册表和storeUserInfo()来处理注册表。但是有一个问题,如果用户没有填写参与者姓名和参与者姓氏字段,这些字段是必需的,不会出现任何验证错误,则会出现: {success: true, message: "success"} message: "success" success:true 因此,不显示任何验证错误,它将显示一条成功消息,并在数据库中插入任何信息 你知道问题在哪里吗 //登记表 <form method="post" id="step1formfree

我有下面的注册表和storeUserInfo()来处理注册表。但是有一个问题,如果用户没有填写
参与者姓名
参与者姓氏
字段,这些字段是必需的,不会出现任何验证错误,则会出现:

{success: true, message: "success"}
message: "success"
success:true
因此,不显示任何验证错误,它将显示一条成功消息,并在数据库中插入任何信息

你知道问题在哪里吗

//登记表

 <form method="post" id="step1formfree" action="">
            {{csrf_field()}}
            @if (!is_null($allParticipants) && is_int($allParticipants))
                @if($allParticipants == 1)
                    <p>Please fill all fields. Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.</p>
                @else
                    <p>Is not necessary additional info. Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.</p>
                @endif

                <span id="userData" data-name="{{ auth()->user()->name }}" data-surname="{{ auth()->user()->surname }}"></span>

                @foreach($selectedRtypes as $k => $selectedRtype)
                    //{{dd($selectedRtype)}}
                    @foreach(range(1,$selectedRtype['quantity']) as $val)
                        @if($allParticipants == 1)
                            <h6>Participant - {{$val}} - {{$k}}</h6>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="fill_auth_info{{ $val }}" data-id="{{ $k }}_{{ $val }}"name="fill_with_auth_info">
                                <label class="form-check-label d-flex align-items-center" for="fill_auth_info{{ $val }}">
                                    <span class="mr-auto">Fill with auth user info.</span>
                                </label>
                            </div>
                            <div class="form-group font-size-sm">
                                <label for="name{{ $k }}_{{ $val }}" class="text-gray">Name</label>
                                <input type="text"  id="name{{ $k }}_{{ $val }}" name="participant_name[]" required class="form-control" value="">
                            </div>
                            <div class="form-group font-size-sm">
                                <label for="surname{{ $k }}_{{ $val }}" class="text-gray">Surname</label>
                                <input type="text" id="surname{{ $k }}_{{ $val }}" required class="form-control" name="participant_surname[]" value="">
                            </div>
                            @foreach($selectedRtype['questions'] as $customQuestion)
                                <div class="form-group">
                                    <label for="participant_question">{{$customQuestion->question}}</label>
                                    <input type="text"
                                           @if($customQuestion->pivot->required == "1") required @endif
                                           class="form-control" name="participant_question[]">
                                    <input type="hidden" name="participant_question_required[]"
                                           value="{{ $customQuestion->pivot->required }}">
                                    <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                                </div>
                            @endforeach
                        @else
                            <input type="hidden" value="foo" name="participant_name[]"/>
                            <input type="hidden" value="bar" name="participant_surname[]"/>
                        @endif
                        <input type="hidden" name="rtypes[]" value="{{ $selectedRtype['id'] }}"/>
                    @endforeach
                            <div class="form-group">
                    @if ($allParticipants == 0)
                        @foreach($selectedRtype['questions'] as $customQuestion)
                                <label for="participant_question">{{$customQuestion->question}}</label>
                                <input type="text"
                                       @if($customQuestion->pivot->required == "1") required @endif
                                       class="form-control" name="participant_question[]">
                                <input type="hidden" name="participant_question_required[]"
                                       value="{{ $customQuestion->pivot->required }}">
                                <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                            </div>
                        @endforeach
                    @endif
                @endforeach
            @endif
            <input type="submit" href="#step2"
                   id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
        </form>

{{csrf_field()}}
@如果(!is_null($allParticipants)&&is_int($allParticipants))
@如果($allParticipants==1)
请填写所有字段。您的票证将发送到{(\Auth::check())?Auth::user()->email:old('email')}

@否则 没有必要提供其他信息。您的票证将发送到{(\Auth::check())?Auth::user()->email:old('email')}

@恩迪夫 @foreach($SelectedType为$k=>$SelectedType) //{{dd($selectedRtype)} @foreach(范围(1,$SelectedType['quantity'])为$val) @如果($allParticipants==1) 参与者-{{$val}}-{{$k} 填写身份验证用户信息。 名称 姓 @foreach($SelectedType['questions']作为$customQuestion) {{$customQuestion->question} pivot->required==“1”)required@endif class=“form control”name=“参与者\问题[]”> @endforeach @否则 @恩迪夫 @endforeach @如果($allParticipants==0) @foreach($SelectedType['questions']作为$customQuestion) {{$customQuestion->question} pivot->required==“1”)required@endif class=“form control”name=“参与者\问题[]”> @endforeach @恩迪夫 @endforeach @恩迪夫
//storeUserInfo方法,单击“转到步骤2”时,代码转到此方法:

 public function storeUserInfo(Request $request, $id, $slug = null, Validator $validator){
        $allParticipants = Congress::where('id', $id)->first()->all_participants;
        $user = Auth::user();

        $rules = [];
        $messages = [];

        if(isset($request->participant_question_required)) {
            $messages = [
                'participant_question.*.required' => 'The participant is required'
            ];

            foreach ($request->participant_question_required as $key => $value) {
                $rule = 'string|max:255'; // I think string should come before max
                //dd($value);
                // if this was required, ie 1, prepend "required|" to the rule
                if ($value) {
                    $rule = 'required|' . $rule;
                }

                // add the individual rule for this array key to the $rules array
                $rules["participant_question.{$key}"] = $rule;
            }
        }

        if($allParticipants){
            $rules = [
                'participant_name.*' => 'required|max:255|string',
                'participant_surname.*' => 'required|max:255|string',
            ];
        }

        $validator = Validator::make($request->all(), $rules, $messages);

        if($validator->passes()) {
            $registration = Registration::create([
                'congress_id' => $id,
                'main_participant_id' => $user->id,
                'status' => 'C',
            ]);

            $participants = [];
            for ($i = 0; $i < count($request->participant_name); $i++) {
                $name = ($allParticipants) ? $request->participant_name[$i] : '';
                $surname = ($allParticipants) ? $request->participant_surname[$i] : '';
                $participants[] = Participant::create([
                    'name' => $name,
                    'surname' => $surname,
                    'registration_id' => $registration->id,
                    'registration_type_id' => $request->rtypes[$i]

                ]);
            }

            if (isset($request->participant_question))
                for ($i = 0; $i < count($request->participant_question); $i++)
                    $answer = Answer::create([
                        'question_id' => $request->participant_question_id[$i],
                        'participant_id' => $participants[$i]->id,
                        'answer' => $request->participant_question[$i],
                    ]);
        }

        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);
    }
公共函数storeUserInfo(请求$Request,$id,$slug=null,验证器$Validator){
$allParticipants=Congress::where('id',$id)->first()->all_参与者;
$user=Auth::user();
$rules=[];
$messages=[];
if(设置($request->participant\u question\u required)){
$messages=[
'参与者问题。*.required'=>'参与者是必需的'
];
foreach($request->participant\u question\u必需为$key=>$value){
$rule='string | max:255';//我认为string应该在max之前
//dd(价值);
//如果需要,即1,在规则前加上“required |”
如果(价值){
$rule='required |'。$rule;
}
//将此数组键的单个规则添加到$rules数组中
$rules[“参与者问题{$key}”]=$rule;
}
}
如果($所有参与者){
$rules=[
'参与者名称。*'=>'必填项|最大值:255 |字符串',
'参与者姓氏。*'=>'必填项|最大值:255 |字符串',
];
}
$validator=validator::make($request->all(),$rules,$messages);
如果($validator->passes()){
$registration=注册::创建([
“国会id”=>$id,
“主要参与者id”=>$user->id,
“状态”=>“C”,
]);
$参与者=[];
对于($i=0;$iparticipant_name);$i++){
$name=($allParticipants)?$request->participant_name[$i]:'';
$姓氏=($allParticipants)?$request->participant_姓氏[$i]:'';
$participants[]=参与者::创建([
“name”=>$name,
“姓氏”=>$姓氏,
“注册号”=>$registration->id,
“注册类型\u id”=>$request->rtypes[$i]
]);
}
if(isset($request->participant_question))
对于($i=0;$iparticipant_question);$i++)
$answer=answer::创建([
'question\u id'=>$request->participant\u question\u id[$i],
'participant_id'=>$participants[$i]>id,
“回答”=>$request->参与者问题[$i],
]);
}
返回响应()->json([
“成功”=>正确,
'消息'=>'成功'
], 200);
}

似乎您没有捕获validator fails实例,该实例将忽略
$validator
变量并返回成功。你应该去看有线电视
if($validator->fails()) {
   return \Response::json([
        'data' => [
        'message'   => $validator->errors(),
        'success'   => false
        ]
    ], 404);;
}