dpy83214 2018-05-06 14:04
浏览 13
已采纳

为什么如果“allParticipants == 0”且有自定义问题,则显示的消息不正确?

I want to have the registration form below that:

  • if "allParticipants == 1" it should appear the message: "<p>Please fill all form fields. Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.</p>" (this is working)
  • else, if "allParticipants == 0" and there are no custom questions, that is "$selectedRtype['questions']" is empty it should appear the message "<p>Is not necessary additional info. Your tickets will be send to "<b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b></p>" (this is working)
  • if "allParticipants == 0" and there are custom questions, that is, "$selectedRtype['questions']" is not empty, it shoud appear the message "<p>Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.You only need to answer to the custom questions below.</p>" (this is not working)

But its not working for the third scenario, for when "if "allParticipants == 0" and there are custom questions," the message that appears is "<p>Is not necessary additional info. Your tickets will be send to "<b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b></p>", but the message that should appear in this case is "<p>Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.You only need to answer to the custom questions below. </p>".

Do you know where is the issue?

// form part where are the conditions:

<form method="post" id="step1formfree" action="">
    {{csrf_field()}}
    @if (!is_null($allParticipants) && is_int($allParticipants))
        @if($allParticipants == 1)
            <p>Please fill all form fields. Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.</p>
        @else
            // if there are no custom questions the message should be only "Is not necessary additional info. Your tickets will be sent to"
            @if(is_null($selectedRtype['questions']))
                <p>Is not necessary additional info. Your tickets will be sent to
                    <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b></p>
            // if the user selected in the previous page tickets that have associated custom questions the message should be "Your tickets will be send to....You only need to answer to the custom questions below."
            @else
                <p>Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.
                    You only need to answer the custom questions below.
                </p>
            @endif
        @endif
    @endif

The {{dd($selectedRtype['questions'])}} shows:

Collection {#236 ▼
  #items: []
}

// complete form

<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>
        @elseif(!is_null($selectedRtype['questions']))
           <p>Your tickets will be send to <b>{{ (\Auth::check()) ? Auth::user()->email : old('email')}}</b>.You only need to answer to the custom questions below.</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>
  • 写回答

1条回答 默认 最新

  • dongpian2559 2018-05-06 17:57
    关注

    Good job showing the dump for questions: {{dd($selectedRtype['questions'])}}.

    How about simplfying the if-check (and dd) to: @if( $selectedRtype->questions->count())? This might make it a little easier to debug/read for you as well as closer to what you are trying to say in human terms. Take a look at:

    Laravel Docs for count()

    Remember, with your dd above, you've seen that an instance of Illuminate\Support\Collection is always returned, even when there are no results. You are basically checking that $x = new stdClass; if ($x) { ... } which will always return true (or always false for a is_null check).

    It may be possible that the if check is looking at a different value than when you dd -- in theory you are 100% correct - based on the collection being returned, it should be false in the is_null check, but it's not. I think if you check for the count or ! $selectedRtype->questions->isEmpty()... or even $selectedRtype->questions->first(), it may bring to light the issue.

    ** NOTE - the complete form and the section are different. The section's code has the if-check for questions. It appears the 'Complete Form' only checks for allParticipants. Is it possible that you've just not included the code in the final form?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案