Laravel Validation Rules If Value Exists in Another Field Array

Related searches

I am working in Laravel 5.4 and I have a slightly specific validation rules need but I think this should be easily doable without having to extend the class. Just not sure how to make this work..

What I would like to do is to make the 'music_instrument' form field mandatory if program array contains 'Music'.

I found this thread How to set require if value is chosen in another multiple choice field in validation of laravel? but it is not a solution (because it never got resolved in the first place) and the reason it doesn't work is because the submitted array indexes aren't constant (not selected check boxes aren't considered in indexing the submission result...)

My case looks like this:

<form action="" method="post">
    <fieldset>

        <input name="program[]" value="Anthropology" type="checkbox">Anthropology
        <input name="program[]" value="Biology"      type="checkbox">Biology
        <input name="program[]" value="Chemistry"    type="checkbox">Chemistry
        <input name="program[]" value="Music"        type="checkbox">Music
        <input name="program[]" value="Philosophy"   type="checkbox">Philosophy
        <input name="program[]" value="Zombies"      type="checkbox">Zombies

        <input name="music_instrument" type="text" value"">

        <button type="submit">Submit</button>

    </fieldset>
</form>

If I select some of the options from the list of check boxes I can potentially have this result in my $request values

[program] => Array
    (
        [0] => Anthropology
        [1] => Biology
        [2] => Music
        [3] => Philosophy
    )

[music_instrument] => 'Guitar'

Looking at validation rules here: https://laravel.com/docs/5.4/validation#available-validation-rules I think something like his should work but i am literally getting nothing:

  $validator = Validator::make($request->all(),[
        'program'           => 'required',
        'music_instrument'  => 'required_if:program,in:Music'
  ]);

I was hoping this would work too but no luck:

'music_instrument'  => 'required_if:program,in_array:Music',

Thoughts? Suggestions?

Thank you!

Haven't tried that, but in general array fields you usually write like this: program.*, so maybe something like this will work:

  $validator = Validator::make($request->all(),[
        'program'           => 'required',
        'music_instrument'  => 'required_if:program.*,in:Music'
  ]);

If it won't work, obviously you can do it also in the other way for example like this:

$rules = ['program' => 'required'];

if (in_array('Music', $request->input('program', []))) {
    $rules['music_instrument'] = 'required';
}

$validator = Validator::make($request->all(), $rules);

Validation - Other field has value, Laravel Validation Rules If Value Exists in Another Field Array. I am working in Laravel 5.4 and I have a slightly specific validation rules need but I think this� Home » Php » php – Laravel Validation Rules If Value Exists in Another Field Array php – Laravel Validation Rules If Value Exists in Another Field Array Posted by: admin July 11, 2020 Leave a comment

You could create a new custom rule called required_if_array_contains like this...

In app/Providers/CustomValidatorProvider.php add a new private function:

/**
 * A version of required_if that works for groups of checkboxes and multi-selects
 */
private function required_if_array_contains(): void
{
    $this->app['validator']->extend('required_if_array_contains',
        function ($attribute, $value, $parameters, Validator $validator){

            // The first item in the array of parameters is the field that we take the value from
            $valueField = array_shift($parameters);

            $valueFieldValues = Input::get($valueField);

            if (is_null($valueFieldValues)) {
                return true;
            }

            foreach ($parameters as $parameter) {
                if (in_array($parameter, $valueFieldValues) && strlen(trim($value)) == 0) {
                    // As soon as we find one of the parameters has been selected, we reject if field is empty

                    $validator->addReplacer('required_if_array_contains', function($message) use ($parameter) {
                        return str_replace(':value', $parameter, $message);
                    });

                    return false;
                }
            }

            // If we've managed to get this far, none of the parameters were selected so it must be valid
            return true;
        });
}

And don't forget to check there is a use statement at the top of CustomValidatorProvider.php for our use of Validator as an argument in our new method:

...

use Illuminate\Validation\Validator;

Then in the boot() method of CustomValidatorProvider.php call your new private method:

public function boot()
{
    ...

    $this->required_if_array_contains();
}

Then teach Laravel to write the validation message in a human-friendly way by adding a new item to the array in resources/lang/en/validation.php:

return [
    ...

    'required_if_array_contains' => ':attribute must be provided when &quot;:value&quot; is selected.',
]

Now you can write validation rules like this:

public function rules()
{
    return [
        "animals": "required",
        "animals-other": "required_if_array_contains:animals,other-mamal,other-reptile",
    ];
}

In the above example, animals is a group of checkboxes and animals-other is a text input that is only required if the other-mamal or other-reptile value has been checked.

This would also work for a select input with multiple selection enabled or any input that results in an array of values in one of the inputs in the request.

Validation - Laravel, states that a field is required if another field exists and has a value in. Currently Laravel provides the required_with rule, but that only checks if a field but for some reason it only validates singles attributes and not arrays� The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size rule. max:value. The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule. mimetypes:text/plain,

The approach I took for a similar problem was to make a private function inside my Controller class and use a ternary expression to add the required field if it came back true.

I have roughly 20 fields that have a checkbox to enable the input fields in this case, so it may be overkill in comparison, but as your needs grow, it could prove helpful.

/**
 * Check if the parameterized value is in the submitted list of programs
 *  
 * @param Request $request
 * @param string $value
 */
private function _checkProgram(Request $request, string $value)
{
    if ($request->has('program')) {
        return in_array($value, $request->input('program'));
    }

    return false;
}

Using this function, you can apply the same logic if you have other fields for your other programs as well.

Then in the store function:

public function store(Request $request)
{
    $this->validate(request(), [
    // ... your other validation here
    'music_instrument'  => ''.($this->_checkProgram($request, 'music') ? 'required' : '').'',
    // or if you have some other validation like max value, just remember to add the |-delimiter:
    'music_instrument'  => 'max:64'.($this->_checkProgram($request, 'music') ? '|required' : '').'',
    ]);

    // rest of your store function
}

fail validation if value exists laravel Code Example, Get code examples like "fail validation if value exists laravel" instantly right from 'email.required'=>'You cant leave Email field empty', $rules = array( laravel rules validate that fields different � laravel rules validation that� Laravel Validation Check if value is not equal to a another field By Hardik Savani | July 25, 2019 | Category : Laravel In this post, we will learn how to check two value must not be same validation in laravel. we can check using laravel different validation rule. you can use different validation rule for field value should not same in form.

The given field must be different than the field under validation. digits:value. The field under validation must be numeric and must have an exact length of value. digits_between:min,max. The field under validation must have a length between the given min and max. email. The field under validation must be formatted as an e-mail address. exists:table,column

I am submitting a single dimensional array of values to process on laravel 5.6. quantity[4]:11 quantity[2]:14 I have to validate both the index and the value, index should exits as stocks,id and value must be integer and minimum 1

I would like to require the presence of the field "items" and this code does it, but the problem is that the validation fails when the "items" field is an empty array, i.e. { "fields": [] } , which is an undesired behavior. I know that's the documented behavior of the "required" parameter but I don't see any "clean" workaround. I tried also:

Comments
  • I tried the asterisk out but it is ignored. I was able to get it to work with required_if:program.34,Music,,, but for that I have to index my list and keep track of 34 which is obviously not a good idea... I will try this aproach too. Thanks. I was hoping laravel's in_array would work but no luck
  • I will accept your second solution being that that is the way I went :) Thanks! For the sake of argument, if someone else comes up with an out of the box working configuration I would still like to see it... hehe
  • This is a great idea! Thanks