Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bashy's avatar
Level 65

Validation - required_without field and other field must be empty

Not sure how to do a "required_without:somefield" but require that somefield to be empty if the field is set...

I have two fields that need to be one OR the other. I'm using the required_without but it allows both fields to be set (which I don't want). Using Laravel 5.

Guessing I'll have to make my own validation rule (empty_with:somefield) but just wondering if someone spotted something I didn't think of.

Example:

0 likes
12 replies
stefanbauer's avatar

btw: nice gui :) Like it (what i see :)) On what are you working? :)

bashy's avatar
Level 65

Yeah, just using Semantic-UI for a CMS backend. Privately using it for our company's clients.

bashy's avatar
bashy
OP
Best Answer
Level 65

I wish this was in by default but not sure if Taylor would put this in since it's probably not used a lot.

Rule

empty_when:field,field2,...

The field under validation must be empty when these fields aren't. Useful for doing one field OR the other, not both.

Added a new service provider and extended validator.

$this->app['validator']->extend('empty_when', function ($attribute, $value, $parameters)
{
    foreach ($parameters as $key)
    {
        if ( ! empty(Input::get($key)))
        {
            return false;
        }
    }

    return true;
});
5 likes
shez1983's avatar

@bashy i have the same problem BUT testing your rule, it seems if i leave BOTH fields out, Validation passes.. :s

btw i did something like this (suggestions welcome)

 Validator::extendImplicit('empty_when', function($attribute, $value, $parameters, $validator) {

            $empty = 0;
            foreach ($parameters as $key)
            {
                if ( empty(Input::get($key)))
                {   
                    $empty++;
                }
            }

            if ( !$value && ( $empty == count($parameters) ) )
                return false;

            return true;
        });
bashy's avatar
Level 65

@shez1983 Sorry, I also used 'required_without:other_field' on each so at least one was required.

1 like
shez1983's avatar

thanks. @bashy it sucks that validation doesnt work STILL.. one year on.

bashy's avatar
Level 65

@shez1983 Indeed, I keep meaning to submit a MR for this rule because I think it's something that is used quite a bit in forms. I still don't see any new rules (5.3) for this.

1 like
mahesh_chinnala's avatar

In case i am having two radio buttons , what would be the scenario.

one radio button is basic and another radio button is on cllick when we click on it ,it will show the input field

I need one of the two fields are required.

tim.ogilvy@thecigroup.com.au's avatar

In case anyone wants to replicate the required_if style functionality, this works.

Usage

    'item_price' => 'empty_if:type_id,1,Line Type,Text Only',

Extenders

        Validator::extend('empty_if', function ($attribute, $value, $parameters, $validator)
        {
            $other = $parameters[0];
            return Input::get($attribute) === Input::get($other);
            return true;
        });

        Validator::replacer('empty_if', function ($message, $attribute, $rule, $parameters) {
            $other = $parameters[0];
            $otherValue = $parameters[1];
            return !(Input::get($other) == $otherValue &&! empty($attribute));
        });

Validator Lang

'empty_if' => 'The :attribute must be empty when :other is ":value"',

I've added field name and value alias options in the replacers for ease of use, however technically I should have probably just called my field invoice_request_line_type_id instead of just type_id. The values lang isn't really going to work for me in this instance which is a trap, because it won't get translated, but you could insert those values from Lang into the validator anyway.

1 like

Please or to participate in this conversation.