【问题标题】:Why validation messages dont appear when required fields are not filled in?为什么未填写必填字段时不显示验证消息?
【发布时间】:2018-05-07 02:07:35
【问题描述】:

我有下面的注册表和 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 方法,当点击“Go to step 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);
    }

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

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

    例如,您可以添加自己的自定义 JSON 响应。您可以从$validator-errors() 获取所有验证器错误,如果$validator-&gt;fails() 是真实的,请确保返回它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多