John Davidson

php - Laravel does not validate 'required' validation in select form

0 comments
Message:


When I choose the default (selected), the message error for validation does not appear but it redirects me to the form w/o any message and the function does not proceed any further.


View


  <select class="form-control select2bs4" name="currency" style="width: 100%;">
<option selected="selected" value="">Choose</option>
<option value="1">ALL</option>
<option value="2">Euro</option>
</select>


Controller


$rules = [
'currency' => 'required'];

$messages = array(
'currency.required' => 'Currency is required'
);

$this->validate($request, $rules, $messages);


My Answer:
The issue here is that the default option in the select element has an empty value, which is causing the validation to fail even when the user selects the default option. To fix this issue, you can remove the empty value from the default option and set it to a value that is not empty.

Here is how you can update your view code:

html



By setting the value of the default option to "0" instead of an empty string, the validation should work correctly even when the user selects the default option.

Rate this post

4 of 5 based on 1380 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us