I have an 'array type' dropdown field in my form, e.g:
<select name="category_id[]">
<option value="">Please Select</option>
// more options
</select>
There are 3 of these same fields (hence it being an array type) and they are all optional, but if a value is selected it checks whether it is a valid value as follows:
$rules['category_id'] = 'exists:universities,id';
The problem I'm having is that if the empty option is selected, it is still giving me a validation error, e.g "The selected category is invalid." If I select a valid value I don't get any errors (as expected).
I have tried adding both nullable and sometimes to the validation rule but they don't make any difference. Do I need to do something different with it being an array type field?
If you use "array-like" name for your select, you should use array validation like so:
$rules['category_id.*'] = ['nullable', 'exists:universities,id'];
But if it's not multi select, you can change the name to category_id to make it work
Can you try <option value="" selected disabled>Please select</option>. Also what does dumping $request->category_id give you when no selections are made?
Related
In laravel 8 I added new boolean field :
$table->boolean( 'is_opened' )->default(false)->after('active');
and having array of labels :
$htmlBlockIsOpenedValueArray = [0 => 'Is not opened', 1 => 'Is opened'];
I have select input defined :
<select name="is_opened" id="is_opened" class="form-control">
<option value=""> -Select is opened-</option>
#foreach($htmlBlockIsOpenedValueArray as $is_opened => $value)
<option value="{{ $is_opened }}" #if (old('is_opened')?old('is_opened'):$htmlBlock->is_opened) == $is_opened) selected #endif>{{ $value }}</option>
#endforeach
</select>
But it does not work correctly, if on saving 'Is not opened'/0/false is selected, as label “-Select is opened-” is visible.
My condition block is not very simple, as I try to show cuurent value in case of validation error.
Which condition can be valid here ?
Thanks!
You're using your old value as a boolean. A 0 is considered false in php. Therefore, your code will not function until you select 1.
However, you can pass a default so you don't have to perform an if statement:
old('is_opened', $htmlBlock->is_opened)
P.S., you're currently not strictly comparing types. This, in your case, is a good thing, since the old values will be of type string, even though your original key is an integer. It's just something to keep in mind.
<select multiple="multiple>
<option th:each="vName : ${variety}"
th:value="${vName}"
th:text="${vName}"
th:selected="${selectedVariety}"></option>
</select>
In the above code "selectedVariety" is a string array sent from controller.
I'm not able to bind the select tag on edit.
And this select tag is not a part of entity table so th:filed="*{selectedVariety}" is not working.
Tried th:attr="selected=${selectedVariety==vName?true:false}">
Not working
On using this "th:selected="${selectedVariety}" every option in the dropdown is getting selected.
What may be the solution??
The option is selected when the value is included to the String array. You'll need following attribute within your <option>:
th:selected="${#arrays.contains(selectedVariety, vName)}"
It returns true (selected) if selectedVariety contains given vName or false (unselected) otherwise.
I would like to select an option in a dropdown using a value in my Model (org.springframework.ui.Model), I know how to do it with th:object and th:field but what if I don't have an object and only a key/value in the model?
I'm using spring boot 2 and thymeleaf 3.
Thank you.
So if I'm understanding the question correctly you want to display values on your drop-down options first ( so that you could select one of them further) based on the selected Model.?
And May I know how you're selecting the Model , is it from UI ?
For example :--
You have selected a value in a HTML option( Values from your Model ), and based on that Selected Value you want to display the values further on another HTML-option which you can select further.
You don't need th:object and/or th:field to dynamically select a option. Just add your options to the model and use the th:selected attribute.
<select ...>
<option ...
th:each="op : ${myOptions}"
th:value="${op.myValueMember}"
th:text="${op.myTextMember}"
th:selected="${op.myTextMember == 'Chimichanga'}"
>
</option>
</select>
i have a dropdown box named f_chapter and id is also f_chapter . As it have different choices when user selects one of the choices , in model i can get the input by calling $this->input->post('f_chapter') . But i don't need this , I need the value of the input .
example:
<option value=""></option>
<option value="id1">Chapter1</option>
<option value="id2">Chapter5</option>
...................
Now if i want to grab the value in my model how should i get? as i don't need chapter1 ,chapter5......... rather i need id1,id2 ......... as these are my primary key ,i need them for database query.Please help . Advance thanks.
You will get the values by default without any change. I mean you will only get id1,id2,.. not chapter 1,2..
First of all, I have already readed this: Validating form dropdown in CodeIgniter
But it doesnt solve my problem:
As you know, <option> value can be faked for example using firebug.
My select options looks like this:
<option value="ger">Germany</option>
<option value="uk">United Kingdom</option>
<option value="usa">United States of America</option>
[...]
Now, how can I validate if the choosen and posted value is correct?
These values are builded on the large array. It looks following:
$countries = array (
'ger' => 'Germany',
'uk' => 'United Kingdom',
'usa' => 'United States of America'
);
So how can I validate, of choosen value is correct? Will I have to use the array_key_exist with function callback on form validation?
What in case if my select options looks like this?
<option value="0">Choose Something</option>
<option value="1">Select-1</option>
<option value="2">Select-2</option>
[..]
How can I validate the above using the form validator, so I could check if the posted value is correct and not faked, also how to prevent from choosing the 0 value?
The best idea for this in my opinion is creating the function callback, which will look following:
public function validate_values($input)
{
$allowed = array(1, 2, 3, 4); //[..]
if ( !in_array( $input, $allowed ) )
{
//throw error, and do the rest...
}
}
Are my ideas "ok" or there are any better solutions for these cases?
The callback is exactly what you need. I think that's why they were created. In user guide:
"The validation system supports callbacks to your own validation
functions. This permits you to extend the validation class to meet
your needs."
So if your need is to check if submitted values are from defined range, use it.
Of course it should return false or true depending on result. user guide for callbacks
For me it's the best solution. Especially because you need to check those values after form is submitted.
As for preventing from choosing 0, don't put 0 to in_array or check first if $input is !empty for example and set correct error message. If you're creating your own callback you can validate that $input for whatever you want.