Laravel 5 Validation - validating the sum of 2 fields

wyred

I'm using Laravel 5's Form Request Validation feature to validate a form input.

In my form, I have 2 fields num_adults and num_children.

I need to ensure that the sum of both fields do not exceed a certain value.

What I've tried is in the rules() function of my validation file, I used merge() to artificially add a new input value that is the sum of both num_adults and num_children.

$this->merge([
    'max_persons' => $this->input('num_adults') + $this->input('num_children') 
]);

And then in the rules array returned,

$rules = [
    'num_adults' => 'integer|max:2',
    'num_children' => 'integer|max:1',
    'max_persons' => 'integer|max:2',
];

The validation works fine for num_adults and num_children. But max_persons seems to be ignored.

Zoe Blair

I might skip the array merge and have my rules like:

$rules = [
    'num_adults' => 'integer|max:'.(2-$this->get('num_children', 0)) ,
    'num_children' => 'integer|max:'.(2-$this->get('num_adults', 0))
];

2 being the maximum permitted value.

On the other hand, the approach that you've started gives you a little more flexibility on the error messages.

The validator already has the values before you update the array, so it doesn't know about your addition. You could add a validator method to your request object to merge in your values a little earlier in the process.

public function validator(Factory $factory)
{
    $this->merge([
        'max_persons' => $this->input('num_adults') + $this->input('num_children') 
    ]);

    return $factory->make(
        $this->all(),
        $this->rules(),
        $this->messages()
    );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

two fields validation with laravel 5

From Dev

Laravel Validation: Validating OR instead of AND

From Dev

Laravel Validation: Validating OR instead of AND

From Dev

Validation plugin is not validating all fields

From Dev

Laravel 5: validation error return values to fields

From Dev

Validating unknown input fields in Laravel

From Dev

Form Validation with sum of fields

From Dev

CakePHP 2 - Validating password fields

From Dev

Laravel 5 - Validating Mime types

From Dev

Laravel 5 redirect page with dynamically added fields after validation fails

From Dev

Duplicate fields despite using unique validation in Laravel 5

From Dev

Validating fields from different tables-laravel

From Dev

Symfony2 form not validating required fields

From Dev

Having trouble validating on update (Laravel 5)

From Dev

Laravel validation service with unique fields

From Dev

Laravel 5 validation in model

From Dev

Laravel 5 array validation

From Dev

Laravel 5 Mime validation

From Dev

Laravel 5 Validation Trim

From Dev

Validation image laravel 5

From Dev

Laravel 5 custom validation

From Dev

Validation request with Laravel 5

From Dev

Laravel 5 Update Validation

From Dev

Laravel 5 validation in model

From Dev

Laravel 5 Validation in controller

From Dev

Laravel/Lumen: validating data for email already exist and general validation

From Dev

Can I appy same rule for group of fields in Laravel5 request validation?

From Dev

Play Framework 2: Best way of validating individual model fields separately

From Dev

Calculating sum of 2 fields in a row