livewire validation according to another input

Viewed 815

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.

1 Answers

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
Related