Laravel validation not allow to be false all

93 views Asked by At

I am a beginnerwith the Laravel framework. I am struggling to declare validation of request data. I have some checkboxes in the form, and at least one of the checkboxes must be selected to submit to the database.

<input type="hidden" name="has_a_type" value="0">
<input type="checkbox" name="has_a_type" value="1">
<input type="hidden" name="has_b_type" value="0">
<input type="checkbox" name="has_b_type" value="1">
<input type="hidden" name="has_c_type" value="0">
<input type="checkbox" name="has_c_type" value="1">
<input type="hidden" name="has_d_type" value="0">
<input type="checkbox" name="has_d_type" value="1">
<input type="hidden" name="has_e_type" value="0">
<input type="checkbox" name="has_e_type" value="1">
<input type="hidden" name="has_f_type" value="0">
<input type="checkbox" name="has_f_type" value="1">
public function store(Request $request) {
    $request = $request->validate([
        'form_id' => 'required',
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required',
        'has_a_type' => 'required',
        'has_b_type' => 'required',
        'has_c_type' => 'required',
        'has_d_type' => 'required',
        'has_e_type' => 'required',
        'has_f_type' => 'required',
    ]);
}

When users submit the form, they must select the form type at least once. How can I do that? I can check by adding the following code under the validate code.

if (!($request['has_a_type'] || $request['has_b_type'] || $request['has_c_type'] || $request['has_d_type'] || $request['has_e_type'] || $request['has_f_type'])) {
    return ...->withError();
}

But I wonder if there is a way to check this invalidate.

3

There are 3 answers

0
Zaid Malek On BEST ANSWER

I have two possible solutions for your problem:

One is using required_without_all

$request = $request->validate([
...
'has_a_type' => 'required_without_all','has_b_type','has_c_type' ...
'has_b_type' => ...
...
]);

Another better solution is passing data in array

<input type="checkbox" name="has_type[]" value="a">
<input type="checkbox" name="has_type[]" value="b">
...
$request = $request->validate([
...
'has_type' => 'required',
])
;

NOTE: In both solutions you will need to remove those hidden fields: <input type="hidden" name="has_a_type" value="0">

0
Tony On

Searching the docs I saw the required_without_all rule which might be what you need.

required_without_all:foo,bar,...

The field under validation must be present and not empty only when all of the other specified fields are empty or not present.

You specify the other checkboxes in the bar,... argument list, something like

    'has_a_type' => 'required_without_all','has_b_type','has_c_type' ... 

I must admit that I've not used this rule myself, it just looks like it might be a good use for it, but it will only work when you remove the hidden fields and just use the checkboxes.

0
Karl Hill On

You can create a custom validation rule to check if at least one checkbox is selected. First, generate a new rule using the following command.

php artisan make:rule AtLeastOneCheckbox

This will create a new rule class in the app/Rules directory. In the AtLeastOneCheckbox class, you can define the condition for the rule in the passes method.

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class AtLeastOneCheckbox implements Rule
{
    public function passes($attribute, $value)
    {
        // Check if at least one checkbox is selected
        return collect($value)->contains(1);
    }

    public function message()
    {
        return 'At least one checkbox must be selected.';
    }
}

Then, you can use this rule to validate the checkboxes in your controller. This will ensure that at least one checkbox is selected when submitting the form.

use App\Rules\AtLeastOneCheckbox;

public function store(Request $request) {
    $request->validate([
        'form_id' => 'required',
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required',
        'has_a_type' => ['required', new AtLeastOneCheckbox],
        'has_b_type' => ['required', new AtLeastOneCheckbox],
        'has_c_type' => ['required', new AtLeastOneCheckbox],
        'has_d_type' => ['required', new AtLeastOneCheckbox],
        'has_e_type' => ['required', new AtLeastOneCheckbox],
        'has_f_type' => ['required', new AtLeastOneCheckbox],
    ]);
}