0

So I have fields that are to be hidden if it met certain conditions. And now I'm having problem in my validation. Here's what I have:

blade.php

<div class="{{ $room['show_checkin_out'] ? '' : 'hide-fields' }}">
    <div class="form-group check-in-dtls">
        <label for="before_checkin">@lang('before_checkin')</label>
        <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
        @if ($errors->has('before_checkin'))
            <div class="form-group">
                <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
            </div>
        @endif
    </div>
</div>

validation file

'before_checkin' => ['sometimes', 'required', 'max:255'],

css

.hide-fields{
    display: none;
}

I would like the fields to be required only if it is shown and my validation is not working well. For this one, what is the best approach to do?

1
  • you can do the trick with jquery by removing name attribute from input filed if hide-fields class is exist Commented Nov 20, 2019 at 10:36

4 Answers 4

2

I can suggest 3 ways.

  1. Just don't add this validation rule at all if $room['show_checkin_out'] is falsy.

Kind of:

$rules = [...];
if ($room['show_checkin_out']) {
    $rules['before_checkin'] = ['sometimes', 'required', 'max:255'];
}
  1. Use "Required With" rule (https://laravel.com/docs/5.8/validation#rule-required-with). Simply pass show_checkin_out as a hidden parameter when necessary and make before_checkin required only when show_checkin_out is present.

Add into your form:

@if($room['show_checkin_out'])
    <input type="hidden" name="show_checkin_out" value="1"/>
@endif

Modify the validation rule this way:

'before_checkin' => ['required_with:show_checkin_out', 'sometimes', 'max:255'],
  1. Probably the best one. Since you already have sometimes rule then basically don't output the before_checkin field when show_checkin_out is falsy:
@if($room['show_checkin_out'])
    <div class="form-group check-in-dtls">
        <label for="before_checkin">@lang('before_checkin')</label>
        <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
        @if ($errors->has('before_checkin'))
            <div class="form-group">
                <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
            </div>
        @endif
    </div>
@endif
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of setting display:none property you can do this.

@if(isset($room['show_checkin_out']))
<div class="">
  <div class="form-group check-in-dtls">
    <label for="before_checkin">@lang('before_checkin')</label>
      <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
     @if ($errors->has('before_checkin'))
        <div class="form-group">
            <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
         </div>
     @endif
   </div>
 </div>
@endif

If it's set $room['show_checkin_out'] it will create that part and will validate or else it will not create that section and your validation will work as you want it to be.

Comments

0

So finally, I have done this way to do the validation working.

blade

.... name="{{ $room['show_checkin_out']==1 ? 'before_checkin' : '' }}"

validation

$ret = [
       // other validation
       ];

if (\Request::has('before_checkin')) {
    $ret['before_checkin'] = ['required', 'sometimes', 'max:255'];
}

It's much simpler.

Comments

0

You can use:

'before_checkin' => ['required_if:show_checkin_out,true']

You can also check Laravel documentation

https://laravel.com/docs/10.x/validation#rule-required-if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.