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

时间:2018-05-06 17:33:28

标签: php laravel

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

{success: true, message: "success"}
message: "success"
success:true

因此,不要显示任何验证错误,它会显示成功消息,并且任何信息都会插入db。

你知道问题出在哪里吗?

//注册表单

 <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>

// storeUserInfo方法,当&#34;转到步骤2&#34;时,代码转到此方法;点击:

 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);
    }

1 个答案:

答案 0 :(得分:0)

好像你没有捕获验证器失败实例,它将忽略$validator变量并返回成功。你应该像这样抓住它

if($validator->fails()) {
   return \Response::json([
        'data' => [
        'message'   => $validator->errors(),
        'success'   => false
        ]
    ], 404);;
}

举个例子,您可以添加自己的自定义JSON响应。并且您可以从$validator-errors()获取所有验证程序错误,如果$validator->fails()是真实的,请确保返回