简体   繁体   中英

Laravel "At Least and At Most" one field is required

I have four different fields a, b, c and d. I want only one of them to be filled. How may I do this?

I found out required_without_all:foo,bar,... on Laravel documentation but this seems to allow at least one field. I can fill others as well. I want only one field to be filled and one of them should be required.

This is what I did so far, but I am allowed to send these fields together.

public function rules()
{
    return [
        "isFlat" => "required_without_all:isDetachedHouse,isTerrace,isSemiDetached",
        "isDetachedHouse" => "required_without_all:isFlat,isTerrace,isSemiDetached",
        "isTerrace" => "required_without_all:isFlat,isDetachedHouse,isSemiDetached",
        "isSemiDetached" => "required_without_all:isFlat,isDetachedHouse,isTerrace",
        "finishQuality" => [
            "required",
            Rule::in(["luxury", "standard", "economy"])
        ],
    ];
}

Try this

                $validations = $request->validate([
            'text1' => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text2') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                }
            ],
            "text2" => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text1') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                }
            ],
            "text3" => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text1') && request()->filled("text2")) {
                        return $fail('Only 1 of the three is allowed');
                    }

                    if (!request()->filled('text1') && !request()->filled("text2") && !request()->filled($attribute)) {
                        return $fail('You must fill one');
                    }
                }
            ],
        ]);

Replace text1 , text2 and text3 .

Of course you can add more validation to each text after function . You can validate if is string or number, etc... for example after function write "size:3" this will return error if you write a single character

Example:

//validation
'text1' => [
                'bail',
                function ($attribute, $value, $fail) {
                    if (request()->filled('text2') && request()->filled("text3")) {
                        return $fail('Only 1 of the three is allowed');
                    }
                },
               "size:3"
            ],
// I fill form with these values
text1[value="q"]
text2[value=null]
text3[value="hey"]

Will returns errors:

  • The text1 must be 3 characters.
  • Only 1 of the three is allowed

This looks like you have a form with four checkboxes for the four types of housing. If you require that exactly one of those options is provided, why not use a radio-button or a select?

<label>
    <input type="radio" name="housingtype" value="flat">
    flat
</label>
<label>
    <input type="radio" name="housingtype" value="detachedHouse">
    detached house
</label>
<label>
    <input type="radio" name="housingtype" value="terrace">
    terrace
</label>
<label>
    <input type="radio" name="housingtype" value="semiDetached">
    semi-detached
</label>

Or with a select:

<select name="housingtype">
    <option value="flat">flat</option>
    <option value="detachedHouse">detached house</option>
    <option value="terrace">terrace</option>
    <option value="semiDetached">semi-detached</option>
</select>

Then the validation should be like:

[
    'housingtype' => 'required|in:flat,detachedHouse,terrace,semiDetached',
    // ...
]

One extra benefit: it is now trivial to add a housing type!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM