如何处理此验证上下文?

时间:2018-04-27 19:00:36

标签: php laravel

我在下面有这个注册表。

// registration form in the registration page


 <form method="post" id="step1form" action="">
      {{csrf_field()}}
        <p>Is not necessary additional info.
       Your tickets will be sent to the email
     <b>
    {{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.</p>

      @if (!empty($allParticipants))
        @if($allParticipants == 1)
            @foreach($selectedTypes as $selectedType)
                @foreach(range(1,$selectedType['quantity']) as $test)
                  <p>Please enter  the following 
                  information for each participant</p>
                  <div class="form-group font-size-sm">
                    <label for="name" class="text-gray">Name</label>
                    <input type="text" required class="form-control" id="name"
                           name="name" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
                  </div>
                  <div class="form-group font-size-sm">
                    <label for="surname" class="text-gray">Surname</label>
                    <input type="text" id="surname" required class="form-control" name="surname" value="{{ (\Auth::check()) ? Auth::user()->surname : old('surname')}}">
                  </div>

                <h6>Participant - 1 - {{$test}}</h6>

              <div class="form-group font-size-sm">
                <label for="participant_name" class="text-gray">Name</label>
                <input type="text" name="participant_name[]" required class="form-control" value="">
              </div>
              <div class="form-group font-size-sm">
                <label for="participant_surname" class="text-gray">Surname</label>
                <input type="text" required class="form-control" name="participant_surname[]" value="">
              </div>
            <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>
              @foreach($selectedType['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
            @endforeach
        @endforeach
        @endif
      @endif
      <input type="submit" href="#step2" id="goToStep2" value="Go to step 2"/>
    </form> 

如果会议表中的“all_participants”列为“1”,则用户需要引入一些注册信息,以介绍每个参与者的信息(姓名和姓氏)。

如果all_participants列为“0”,则显示“<p>Is not necessary additional info.</p>”并且用户不需要引入任何信息以在会议中注册,因为它使用其身份验证信息在会议中进行注册。

但是当all_participants列为“0”时会出现问题。因为在处理注册的方法中的RegistrationController中我有一些规则:

 $rules = [
            'name' => 'required|max:255|string',
            'surname' => 'required|max:255|string',
            'email' => 'required|max:255|email',
            'participant_name.*' => 'required|max:255|string',
            'participant_surname.*' => 'required|max:255|string',
        ];

问题:因此,当all_participants列为“0”且用户单击“转到步骤2”时,会出现一些验证错误。你知道如何纠正这个问题吗?

处理注册的完整方法:

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

        $user = Auth::user();

        $rules = [
            'name' => 'required|max:255|string',
            'surname' => 'required|max:255|string',
            'email' => 'required|max:255|email',
            'participant_name.*' => 'required|max:255|string',
            'participant_surname.*' => 'required|max:255|string',
            'participant_email.*' => 'required|max:255|email',
        ];

        $messages = [
            'participant_question.*.required' => 'The participant is required'
        ];

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

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

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

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

                    $participants = [];

                    for ($i = 0; $i < count($request->participant_name); $i++)
                        $participants[] = Participant::create([
                            'name' => $request->participant_name[$i],
                            'surname' => $request->participant_surname[$i],
                            'email' => $request->participant_email[$i],
                            'registration_id' => $registration->id,
                            'ticket_type_id' => $request->ttypes[$i]
                        ]);
                    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],
                        ]);
                }
                else{
                    $participant = Participant::create([
                        'name' => $request->name,
                        'surname' => $request->surname,
                        'email' => $request->email,
                        'registration_id' => $registration->id,
                        'ticket_type_id' => '1'
                    ]);
                }
    }

// storeRegistrationInfo()就像现在一样:

public function storeRegistrationInfoF(Request $request, $id, $slug = null, Validator $validator){

        $allParticipants = Congress::where('id', $id)->first()->all_participants;

        if($allParticipants){
            $user = Auth::user();

            $rules = [
                'name' => 'required_unless:all_participants,0|max:255|string',
                'surname' => 'required_unless:all_participants,0|max:255|string',
                'email' => 'required_unless:all_participants,0|max:255|email',
                'participant_name.*' => 'required_unless:all_participants,0|max:255|string',
                'participant_surname.*' => 'required_unless:all_participants,0|max:255|string',
                'participant_email.*' => 'required_unless:all_participants,0|max:255|email',
            ];

            $messages = [
                'participant_question.*.required' => 'The participant is required'
            ];


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

                    // if this was required, ie 1, prepend "required|" to the rule
                    if ($value) {
                        $rule = 'required|' . $rule;
                    }
                    $rules["participant_question.{$key}"] = $rule;
                }
            }

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

            if($validator->passes()) {

                $registration = Registration::create([
                    'congress_id' => $id,
                    'main_user_id' => $user->id,
                    'status' => 'C',

                ]);

                if ($request->participant_name) {

                    $participants = [];

                    for ($i = 0; $i < count($request->participant_name); $i++)
                        $participants[] = Participant::create([
                            'name' => $request->participant_name[$i],
                            'surname' => $request->participant_surname[$i],
                            'email' => $request->participant_email[$i],
                            'registration_id' => $registration->id,
                            'ticket_type_id' => $request->ttypes[$i]

                        ]);
                }
            }

            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
        }
        else {
            $user = Auth::user();

            $registration = Registration::create([
                'congress_id' => $id,
                'main_user_id' => $user->id,
                'status' => 'C',

            ]);
            $participant = Participant::create([
                'name' => $user->name,
                'surname' => $user->surname,
                'email' => $user->email,
                'registration_id' => $registration->id,
                'ticket_type_id' => '1' // test
            ]);
        }
        $errors = $validator->errors();
        $errors =  json_decode($errors);

        return response()->json([
            'success' => false,
            'errors' => $errors
        ], 422);
    }

1 个答案:

答案 0 :(得分:0)

由于您的验证始终要求您拥有$allParticipants > 0是否为allParticipants的值,因此您需要确保0是否大于public function storeRegistration(Request $request, $id, $slug = null, Validator $validator){ // get your `$allParticipants` here if($allParticipants) { // put your logic here } else{ // get the auth user // and register him if there is no particpants $user = Auth::user(); $registration = Registration::create([ 'congress_id' => $id, 'main_user_id' => $user->id, 'status' => 'C', ]); } }); 才允许验证否则跳过它。

    $allParticipants = Congress::where('id', $id)->first()->all_participants;

    if($allParticipants){
        $user = Auth::user();

        $rules = [
            'name' => 'required_unless:all_participants,0|max:255|string',
            'surname' => 'required_unless:all_participants,0|max:255|string',
            'email' => 'required_unless:all_participants,0|max:255|email',
            'participant_name.*' => 'required_unless:all_participants,0|max:255|string',
            'participant_surname.*' => 'required_unless:all_participants,0|max:255|string',
            'participant_email.*' => 'required_unless:all_participants,0|max:255|email',
        ];

        $messages = [
            'participant_question.*.required' => 'The participant is required'
        ];


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

                // if this was required, ie 1, prepend "required|" to the rule
                if ($value) {
                    $rule = 'required|' . $rule;
                }
                $rules["participant_question.{$key}"] = $rule;
            }
        }

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

        if($validator->passes()) {

            $registration = Registration::create([
                'congress_id' => $id,
                'main_user_id' => $user->id,
                'status' => 'C',

            ]);

            if ($request->participant_name) {

                $participants = [];

                for ($i = 0; $i < count($request->participant_name); $i++)
                    $participants[] = Participant::create([
                        'name' => $request->participant_name[$i],
                        'surname' => $request->participant_surname[$i],
                        'email' => $request->participant_email[$i],
                        'registration_id' => $registration->id,
                        'ticket_type_id' => $request->ttypes[$i]

                    ]);
            }

        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);
      }
     if($validator->fails()) {
       $errors = $validator->errors();

       return response()->json([
        'success' => false,
        'errors' => $errors
       ], 422);
      }
    }
    else {
        $user = Auth::user();

        $registration = Registration::create([
            'congress_id' => $id,
            'main_user_id' => $user->id,
            'status' => 'C',

        ]);
        $participant = Participant::create([
            'name' => $user->name,
            'surname' => $user->surname,
            'email' => $user->email,
            'registration_id' => $registration->id,
            'ticket_type_id' => '1' // test
        ]);
    }
}

这是你的功能需要如何:

public function storeRegistrationInfoF(Request $ request,$ id,$ slug = null,Validator $ validator){

Failed: Template parse errors:
The pipe 'filter' could not be found ("
  </thead>
  <tbody>
    <tr *ngFor="let[ERROR ->] dat of result | filter:filterdata| 
 paginate: { itemsPerPage: 5, currentPage: p };let i = index ">
  "): ng:///DynamicTestModule/TransactionComponent.html@220:23