Laravel Rules Validation - Only another field has a value - laravel

<input type="checkbox" name="grievance_discipline" value="checked">
<select name="grievance_type">
<option value="_none" selected>- Select One</option>
<option value="suspension">Suspension</option>
</select>
<?php
$this->validate($request,[
'grievance_type' => 'required_if:grievance_discipline, checked|not_in:_none',
]);
?>
The trouble I am running in to, is telling Laravel to ONLY use a particular validation rule if another field has a value.
In the example, the discipline_type field (a select list), is required IF grievance_discipline is checked.
The default select option for the select list is - Select One - with the value of _none. In the validation rules, _none should be thrown out, but it should ONLY validate on it if the grievance_discipline checkbox has value 'checked'.
Could anybody help me with this, please?

How about setting the first option as disabled so the user are forced to pick a value or not submitting a value, hence gracefully fail your validation check. Also, use required_with instead of required_if to check if another value exist or not.
<input type="checkbox" name="grievance_discipline" value="checked">
<select name="grievance_type">
<option disabled selected value>- Select One</option>
<option value="suspension">Suspension</option>
</select>
Then in the PHP, do
$this->validate($request,[
'grievance_type' => 'required_with:grievance_discipline'
]);

Related

Livewire wire:model on Select Option not working properly

I have a dropdown using wire:model
<div class="form-group">
<label>Menu</label>
<select wire:model="chapter_id" class="form-control #error('chapter_id') is-invalid #enderror" placeholder="Menu">
#foreach ($chapters as $chapter)
<option value="{{ $chapter->id }}">{{ $chapter->name }}</option>
#endforeach
</select>
#error('chapter_id')
<div class="invalid-feedback">{{ $message }}</div>
#enderror
</div>
It works if I do change the option (the $chapter_id assigned successfully). But, if I leave it default (not change the option), or it has only an option, the $chapter_id will not assigned
$chapter_id not assigned
Can you guys help me to solve this issue?
I see 2 ways to work around this. As tell the other before, one have an option for advice the user to select an option
<option value="">Please select</option>
and the other have a defaul value for this property
public $chapter_id = '1';
if this value is required on submit you have to refer it in the rules
protected $rules = [
'chapter_id' => 'required'
];
wire:model is only set or updated on change. Hence if you are already on the item (or there is only one), it will not be triggered. You could work around this using wire:init, but I don't see much benefit compared to setting it directly in your mount-method of the component.
My solution is to add an "Please select" entry as the first one. This enforces to trigger a change by selecting the item in question (even if only one option is available).

How do I remove the null value in the selectbox array when nothing is selected

I am trying to validate an array of select option to check if it is empty as seen below. When value is not selected the default value of the array is null which passes the validation. How do I remove the null value from the select box when nothing is selected or if there is any other way you can suggest.
My code
<select name="test_id[]" class="form-control select-test-{{$idselect}}"
id="select-{{Illuminate\Support\Str::random(10)}}">
<option value="">Select Patient Test</option>
#foreach ($tests as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
</select>
Laravel validation code
$request->validate([
'test_id' => 'required:array'
], [
'test_id.required' => "Test is required"
]);
it goes through even when no selected
Output when not selected
When you give a value for an option it will be passed as a parameter, can you change this line:
<option value="">Select Patient Test</option>
As:
<option disabled selected>Select Patient Test</option>

Laravel required if

I have the field that appears if option VK is selected.
<select id="contact" name="contact" type="text">
<option disabled selected value="sas">sas</option>
<option value="VK">VK</option>
<option value="Viber">Viber</option>
<option value="WhatsApp">WhatsApp</option>
</select>
<input class="musician_input_div form_control"
id="contact_method_vk" name="contact_method_vk"
placeholder="id" style="display:none;" type="text" type="text"/>
The field is required and if I choose viber, this field will be hidden and the form will not send. How do I adjust validator?
$this->validate($request, [
//(if option VK is selected){
'contact' => 'required',
}
]);
if I understand your question correctly. All you need to do is like this.
$this->validate($request, [
'contact_method_vk' => 'required_if:contact,VK'
]);
This will make contact_method_vk is required when VK is selected.
Note: some explanation about required_if. required_if:anotherfield,value: The field under validation must be present and not empty if the anotherfield field is equal to value.

Laravel Collective select to output null when submitted

I am using Laravel Collective for creating my webform.
{!! Form::select('сity_from', ['London', 'Tokyo', 'Moscow'], null, ['placeholder' => 'Choose city'] ) !!}
which produces the following html:
<select id="сity_from" name="сity_from">
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
<option value="London">London</option>
<option value="Tokyo">Tokyo</option>
<option value="Moscow">Moscow</option>
when I choose no city and submit form, and then dd($request->all());in Controller
i can see nothing, I mean, there is no $request->all()['city_from'];
I would like to get ['city_from' = null] in this case.
I suppose I have to change 'value' in
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
to value="null"?
Or something else?
I would like to be using Laravel Collective when solving this problem.
I suggest you to not bother with the presence of 'city_from' in your request.
You might use $cityForm = $request->input('city_from');
And you will have $cityForm set to the actual value, or to null
public function store(Request $request)
{
$cityForm = $request->input('city_from'); //will always be actual value or null
}
remove
disabled="disabled"
and add
value="null"
hope it'll solve the problem.

Rspec + Capybara: Validating select options using xpath

I have an HTML document (there is a DIV with an ID of "countries" that wraps this code below), and in that I need to -
Validate the options in the select. I tried this but this is not valid.
expect(page).to have_xpath(('//*[#id="countries"]//select')[1],
:options => ['US CAN GER POL'])
Validate that the second select is disabled
Validate that CAN is disabled in the first select
Validate that POL is selected in the first select
Change the selected option to GER in the first select
<li>
<fieldset>
<select>
<option value="US">USA</option>
<option value="CAN" disabled>Canada</option>
<option value="GER">Germany</option>
<option value="POL" selected>Poland</option>
</select>
<fieldset>
<li>
<li>
<fieldset>
<select disabled>
<option value="US">USA</option>
<option value="CAN">Canada</option>
<option value="GER">Germany</option>
<option value="POL">Poland</option>
</select>
<fieldset>
<li>
I appreciate any help that you can provide. Thanks!
Word of warning, I personally still use the should syntax for rspec, and I also use css selectors versus xpath. I simply find them easier to read.
(1) Because you have two dropdowns without any specific IDs or class names identifying one from the other, I would use all to restrict the context of your expectations
all('#countries select')[0].should have_text('USA Canada Germany Poland')
(2) Same concept as above, restrict the scope. Second fieldset should contain a disabled dropdown.
all('#countries fieldset')[1].should have_css('select[disabled]')
(3) all('#countries select')[0].should have_css('option[disabled]', :text => 'Canada')
(4) Same answer as #3 but with different attribute and text
(5) all('#countries select')[0].find('option', :text => 'Germany').click

Resources