jeudi 3 décembre 2015

Laravel 5 Validation - validating the sum of 2 fields

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire