Laravel required if - validation

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.

Related

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 multiple select array validation always give error

I am using multiple select in my form, facing problem with its form validation, i am using multiple select field name as array if i give same name for validation rule its work great, but keep giving validation error on selected options also. here is my html code and validation rule.
<select multiple="multiple" name="skills[]" class="form-control">
validation rule
'skills[]' => 'required'
if i use field name without [] or skills.* validation not working for this field, guide me where i am doing something wrong. I am using laravel 5.7 for my project.
If your select looks like this for example:
<div class="form-group row">
<label for="skills" class="col-md-4 col-form-label text-md-right">Skills</label>
<div class="col-md-6">
<select multiple name="skills[]" id="skills" class="form-control{{ $errors->has('skills') ? ' is-invalid' : '' }}" required>
<option value="ios">iOS</option>
<option value="php">PHP</option>
<option value="laravel">Laravel</option>
</select>
#if($errors->has('skills'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('skills') }}</strong>
</span>
#endif
</div>
</div>
Create a custom request:
$ php artisan make:request ExampleRequest
ExampleRequest validation would look like this:
public function authorize()
{
return true;
}
public function rules()
{
return [
'skills' => 'required|array',
];
}
Then just grab the validated data from your $request directly
public function submitForm(ExampleRequest $request)
{
// at this point, validation already passed
// if validation failed, you would be back at form with errors
$skills = request('skills');
// or
$skills = $request->skills;
dd($skills);
}
Custom requests are being validated first before even hitting your controller method.

Laravel Rules Validation - Only another field has a value

<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'
]);

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.

Laravel 5: Check for Specific Error

I have Password and Confirm Password inputs. I have coded these inputs so that in the event of an error the inputs highlight, like so:
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}>
<label>Password</label>
<input type="password" name="password" class="form-control">
</div>
<div class="form-group {{ $errors->has('password_confirmation') ? 'has-error' : '' }}>
<label>Password</label>
<input type="password" name="password_confirmation" class="form-control">
</div>
In my Controller, I validate these inputs as follows:
$this->validate($request, [
....
'password' => 'required|min:8|confirmed',
'password_confirmation' => 'required',
....
]);
When either input is null and/or less than 8 characters, the inputs highlight as expected. However, when the inputs don't match, I would logically expect the Confirm Password input to highlight. However, it is the Password field that highlights, because that is the input that the "confirmed" rule is set to.
The "$errors->has()" method simply checks if an error exists for an input. Is there a way to check if an input has a specific kind of error?
Yes, you can get the array of errors for a given input name by using:
{{ $errors->get('password') }}
So you can check if the password errors array has the confirmation error and then add the additional class to your confirmation input field:
#if (in_array(trans('validation.confirmed', ['attribute' => 'password']), $errors->get('password'))) has-error #endif
And the helper methods may vary between Laravel versions:
Laravel < 5.4: trans('validation.confirmed', ['attribute' => 'password'])
Laravel 5.4: __('validation.confirmed', ['attribute' => 'password'])

Resources