exclude disabled fields from required laravel validation - laravel

How can i exclude disabled fields from the required fields when dealing with validation?
I don't know witch fields are going to be disabled this is happening Dynamic
<input type="text" name="modul" value="" placeholder="" disabled="disabled">
How to check in the validation for disabled="disabled" fields and exclude them from validation
$this->validate($request, [
modul => 'required' // make not required if filed is disabled
]);

You should use sometimes validation rule.
$this->validate($request, [
'modul' => 'sometimes|required'
]);
Conditionally Adding Rules

Related

Laravel validation rule based on other fields value

I have this radio button
<input class="form-check-input" type="radio" name="discount" value="Yes">
<input class="form-check-input" type="radio" name="discount" value="No" checked>
and this hidden field
<input type="hidden" name="discount_valid" value="true">
by default this hidden field is true
Now when i'm trying to validate if discount_valid is true then it should submit the form this code is working but i want to add another condition, if discount is No then it should submit the form irrespective of whether discount value is true or false. If discount is Yes and discount_valid is false then form should not submit.
$validator = Validator::make($request->all(), [
'discount_valid'=>'in:true',
]);
By "submit the form", I have to assume you mean "processing the form request".
I've constructed a validation rule from your specifications:
if discount_valid is true then it should submit the form
if discount is No then it should submit the form irrespective of whether discount value is true or false
If discount is Yes and discount_valid is false then form should not submit.
$validator = Validator::make($request->all(), [
'discount_valid'=>'exclude_if:discount,No|in:true',
'discount'=>'in:No'
]);

How can I receive a specific index of an validator array of array validation in Laravel?

I validate an array in laravel controller and receive message by using $errors->first('field_name') or $errors or $errors->has('field_name). Now the problem is, I validate an array named vaccine_certificate and I can not receive the error message. Actually I receive the message when show all erros at once. but I want to show it with it's input area. How can I solve it?
$this->validate($request, [
'name' => 'required',
'employee_no' => 'required',
'vaccine_certificate.*' => 'image|mimes:png,jpg|max:2048|dimensions:max_height=200,max_width=200',
//'expertise' => 'required',
]);
Input field looks like
<div class="col-6 form-group">
<label class="control-label " for="vaccine_certificate"><h5 class="h6">Vaccine Certificate <small>(<2mb,png,jpg) </small> </h5></label>
<input class="form-control" type="file" name="vaccine_certificate[]" id="vaccine_certificate" multiple/>
#if ($errors->has('vaccine_certificate'))
<p id="employee_no_checker_message" class="text-danger p-2" onload="changeInputBorderColor('vaccine_certificate')"> {{ $errors->first('vaccine_certificate') }}</p>
#endif
</div>
You can use and see all errors in the error bag of form.
$errors->all()
However, as far as I can see there is no "required" in vaccine_certificate, and your rule is vaccine_certificate.*, that means it should be an array. If you make it vaccine_certificate and required like you did in the name field you can see the error message.
If you need to use array, you can use:
$errors->first('vaccine_certificate.*')
Since it is not "required" it will not throw an error and you can not see it in the $errors variable.

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.

Validation on checkbox where one one checkbox must be checked in laravel

I have the following Checkboxes now a want put validation in checkbox that one checkbox must be checked . But i dont't know how to do that.
CheckBox
<div class="form-group clearfix">
<label for="" class="col-sm-2 col-form-label">Arch (es) </label>
<div class="col-sm-10">
<label class="control-label" for="inputError" style="color: red"><i
id="arch_upper_error"></i></label>
<div class="demo-checkbox">
<input id="md_checkbox_1" name="arch_upper" value="41" class="chk-col-black"
type="checkbox">
<label for="md_checkbox_1">Upper</label>
<input id="md_checkbox_2" name="arch_lower" value="41" class="chk-col-black"
type="checkbox">
<label for="md_checkbox_2">Lower</label>
</div>
</div>
</div>
I tried this in laravel validation but i know its wrong because it required for both but i want at least one checkbox is checked.
public function rules()
{
return [
'arch_lower' => 'required',
'agarch_upper' => 'required',
,
];
}
I think you could use Laravel's required-without method:
The field under validation must be present and not empty only when any
of the other specified fields are not present.
Implementation would look something like this:
'arch_upper' => 'required_without: arch_lower',
If, by any chance, you have more checkboxes, you could use required-without-all:
The field under validation must be present and not empty only when all
of the other specified fields are not present.
Implementation:
'arch_upper' => 'required_without_all: arch_lower,another_checkbox',
Note: Code is not tested, if you encounter any errors, let me know.
You can read more on Laravel's official documentantion.

Laravel validation if input is not empty required_with

I'm using laravel and i have a request form with rule like this:
'postal_code1' => 'required',
'postal_code2' => 'required_with:postal_code1|numeric',
and some html
<input type="text" name="postal_code1" />
<input type="text" name="postal_code2" />
The trouble i met that when i don't fill the postal_code1,
I receive the error
Postal_code2 must be a number
How do numeric rule run only if the input is filled?
I tried the sometimes rule:
'postal_code2' =>'sometimes|required_with:postal_code1|numeric'
But not working
I have found the solution, it's very easy. Change the rule as below:
'postal_code2' => 'nullable|required_with:postal_code1|numeric'

Resources