Reputation: 10828
If use_shipping
is un-checked and user did not enter the value in the shipping_note
- validation should have passed but it has failed?
<input type="hidden" name="use_shipping" value="0">
<input type="checkbox" name="use_shipping" value="1" {{ old('use_shipping', $delivery->use_shipping) ? 'checked="checked"' : '' }}>
Text
<input type="text" name="shipping_note" value="">
In Laravel request class:
public function rules()
{
return [
'use_shipping' => 'boolean',
'shipping_note' => 'required_with:use_shipping',
];
}
Upvotes: 1
Views: 6446
Reputation: 62268
The required_with
validation states:
The field under validation must be present and not empty only if any of the other specified fields are present.
Because of your hidden input, the shipping_note
field will always be present. Since the field is present even when the checkbox is unchecked, the required_with
validation will always be triggered.
Most likely what you're looking for is the required_if
validation, which states:
required_if:anotherfield,value,...
The field under validation must be present and not empty if the anotherfield field is equal to any value.
public function rules()
{
return [
'use_shipping' => 'boolean',
'shipping_note' => 'required_if:use_shipping,1',
];
}
This should cause shipping_note
to only be required when the value of use_shipping
is 1
, which should only happen when the checkbox is checked.
Upvotes: 5