为什么Laravel验证不起作用?

时间:2018-06-17 18:42:10

标签: php laravel

我在会议中有用户注册表格。表单可以有自定义问题,在这种情况下,它有自定义问题“电话”,是一个必需的问题,所以我想在Laravel中验证。还应该要求姓名和姓氏字段。

但是验证不正常,即使从源代码中删除了“required”属性并且点击了“Store Registration”按钮,代码也永远不会进入“if ($validator->passes()) {...}”。

你知道为什么验证不起作用吗?

storeRegistration()方法:

 public function storeRegistration(Request $request, $id, $slug = null)
    {

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

        $rules["participant_name.*"] = 'required|max:255|string';
        $rules["participant_surname.*"] = 'required|max:255|string';

        if (isset($request->participant_question_required)) {

            $messages = [
                'participant_question.*.required' => 'Fill all mandatory fields',
            ];

            foreach ($request->participant_question_required as $key => $value) {
                $rule = 'string|max:255';

                if ($value) {
                    $rule = 'required|' . $rule;

                }
                $rules["participant_question.{$key}"] = $rule;

            }
        }

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


        if ($validator->passes()) {

            dd('test');

            $total = Session::get('total');

            # user object
            $user = Auth::user();

            $registration = Registration::create([
                'conference_id' => $id,
                'main_participant_id' => $user->id,
                'status' => ($total > 0) ? 'I' : 'C',
            ]);

            $participants_list = $request->get('participant');

            foreach ($participants_list as $participant) {
                $name = $participant['name'];
                $surname = $participant['surname'];
                $participant_result = Participant::create([
                    'name' => $name,
                    'surname' => $surname,
                    'registration_id' => $registration->id,
                    'registration_type_id' => $participant['rtypes']
                ]);

                if (isset($participant['question_id'])) {
                    $answer = Answer::create([
                        'question_id' => $participant['question_id'],
                        'participant_id' => $participant_result->id,
                        'answer' => $participant['answer'],
                    ]);
                }
            }
            return redirect(route('user.index', ['user' => Auth::id()]) . '#myTickets');
        } else {
            dd($validator->errors());
        }
    }

用户在会议中注册的表格:                    

  <h6 > Participant - 1 - geral</h6>    

  <div class="form-group font-size-sm">
    <label for="namegeral_1" class="text-gray">Name</label>
    <input type="text" id="namegeral_1" name="participant[1][name]" required="" class="form-control" value="">
  </div>

  <div class="form-group font-size-sm">
    <label for="surnamegeral_1" class="text-gray">Surname</label>
    <input type="text" id="surnamegeral_1" required="" class="form-control" name="participant[1][surname]" value="">
  </div>

  <div class="form-group">
    <label for="participant_question">Phone?</label>
    <input type="text" name="participant[1][answer]" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant[1][question_id]">
  </div>

  <input type="hidden" name="participant[1][rtypes]" value="1">

  <h6> Participant - 2 - geral</h6>

  <div class="form-group font-size-sm">
    <label for="namegeral_2" class="text-gray">Name</label>
    <input type="text" id="namegeral_2" name="participant[2][name]" required="" class="form-control" value="">
  </div>

  <div class="form-group font-size-sm">
    <label for="surnamegeral_2" class="text-gray">Surname</label>
    <input type="text" id="surnamegeral_2" required="" class="form-control" name="participant[2][surname]" value="">
  </div>

  <div class="form-group">
    <label for="participant_question">Phone?</label>
    <input type="text" name="participant[2][answer]" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant[2][question_id]">
  </div>

  <input type="hidden" name="participant[2][rtypes]" value="1">


  <h6> Participant - 3 - plus</h6>    

  <div class="form-group font-size-sm">
    <label for="nameplus_3" class="text-gray">Name</label>
    <input type="text" id="nameplus_3" name="participant[3][name]" required="" class="form-control" value="">
  </div>
  <div class="form-group font-size-sm">
    <label for="surnameplus_3" class="text-gray">Surname</label>
    <input type="text" id="surnameplus_3" required="" class="form-control" name="participant[3][surname]" value="">
  </div>

  <input type="hidden" name="participant[3][rtypes]" value="2">
  <input type="submit" class="btn btn-primary" value="Store Registration">
</form>

“dd($ request-&gt; all(),$ rules);”根据用户在表单上填写的字段显示:

array:4 [▼
  "_token" => ""
  "participant" => array:3 [▼
    1 => array:5 [▼
      "name" => "John"
      "surname" => null
      "answer" => null
      "question_id" => "1"
      "rtypes" => "1"
    ]
    2 => array:5 [▼
      "name" => "ere"
      "surname" => "ere"
      "answer" => "003"
      "question_id" => "1"
      "rtypes" => "1"
    ]
    3 => array:3 [▼
      "name" => "i"
      "surname" => "i"
      "rtypes" => "2"
    ]
  ]
  "participant_question_required" => array:2 [▼
    0 => "1"
    1 => "1"
  ]
]
array:4 [▼
  "participant_name.*" => "required|string"
  "participant_surname.*" => "required|string"
  "participant_question.0" => "required|string|max:255"
  "participant_question.1" => "required|string|max:255"
]

dd($validator->errors());”显示:

MessageBag {#271 ▼
  #messages: array:2 [▼
    "participant_question.0" => array:1 [▼
      0 => "FIll all mandatory fields"
    ]
    "participant_question.1" => array:1 [▼
      0 => "Fill all mandatory fields"
    ]
  ]
  #format: ":message"
}

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

$validator = Validator::make($request->all(), [
        'participant.*.name' => 'required|max:255|string',
        'participant.*.surname' => 'required|max:255|string',
]);

if ($validator->passes()) {
    dd('yes');
}else
{
    dd($validator->errors());
}