Showing 1 – 1 of 1 replies
vidhyaprakash85

form validation for select2

vidhyaprakash85 PURCHASED
1 month ago
I have code for form validation on select2

```
<div class="col-md-5 mb-3" wire:ignore>
<label class="form-label" for="branches">{{__('Branch Name')}} <span
class="text-danger"> *</span> </label>
<select data-placeholder="{{__('Branch Name')}}..." name="branch"
required
id="branches"
class="select2-branch form-control @error('branch') is-invalid @enderror">
<option value="">{{__('Branch Name')}}.</option>
</select>
@if($errors->has('branch'))
<div class="invalid-feedback">
<strong>{{ $errors->first('branch') }}</strong>
</div>
@else
<div class="invalid-feedback">
{{__('Please choose a branch')}}
</div>
@endif
</div>
```
Here I a using livewire the problem is for validation expect ``` invalid-feedback ``` within the div because of this error message is not showing. Whether I am wrong in designing or any other option is available. ? please let me know
GotBootstrap
GotBootstrap SELLER
1 month ago
It seems like you’re using Livewire for form validation and Select2 for a dropdown selection. If you want to display validation errors within the same div as the Select2 dropdown, you can try this approach:

<div class="col-md-5 mb-3" wire:ignore>
<label class="form-label" for="branches">{{__('Branch Name')}} <span class="text-danger"> *</span> </label>
<select data-placeholder="{{__('Branch Name')}}..." name="branch" required id="branches" class="select2-branch form-control @error('branch') is-invalid @enderror">
<option value="">{{__('Branch Name')}}.</option>
</select>
@error('branch')
<div class="invalid-feedback" style="display: block;">
<strong>{{ $message }}</strong>
</div>
@else
<div class="invalid-feedback" style="display: block;">
{{__('Please choose a branch')}}
</div>
@enderror
</div>

In this modified version, I’ve removed the else statement for displaying the default error message, assuming you only want to display the validation error message if there is one. Additionally, I’ve added style="display: block;" to ensure the error message div is always visible, regardless of whether there’s an error or not.

Make sure that Livewire is properly configured to display validation errors. If you’re still encountering issues, double-check Livewire’s documentation or seek help from their community forums for further assistance.