livewire validation according to another input

I have an input which requires to be validate according to another one. I want to create a lesson and need to select a classroom and participant capacity.chosen classroom has an own capacity field and my participant capacity input cannot be greater than classroom’s capacity. How to validate this?

    protected array $rules = [
        'form.class_id' => 'required|integer',
        'form.capacity' => 'required|integer',
    ];
    <select wire:model.defer="form.class_id" id="form.class_id" class="border-gray-300 py-3 px-4 rounded-md shadow-sm mt-1 block w-full">
        <option value="null" selected disabled>{{ __('Sınıf Seçin') }}</option>
        @foreach($classes as $c)
            <option value="{{ $c->id }}">{{ $c->name }} ({{ $c->location->name }})</option>
        @endforeach
    </select>
    <x-jet-input-error for="form.class_id" class="mt-2" />
    <input wire:model.defer="form.capacity" type="number" name="form.capacity" id="form.capacity" class="border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm mt-1 block w-full">
    <x-jet-input-error for="form.capacity" class="mt-2" />

for example selected clasroom only contains 5 students so I cannot select participant capacity 6. And form.class_id keeps the id of class because I need it. but I also need to use class->capacity in rules array.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can do it like this:

 protected array $rules = [
      'form.class_id' => 'required|integer',
      'form.capacity' => 'required|integer|lte:form.class_id',
 ];

You can use these validations according to your scenario.
  • gt – greater than
  • gte – greater than equal to
  • lt – less than
  • lte – less than equal to


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x