0

I have a form which contains the following fields:

  • n_census (provides information about the total population from a town or city)
  • n_participants (provides information about the number of participants in an election process according to n_census)

I would like to validate n_participants according to n_census because it's not possible to have more n_participants than n_census.

The code I use to validate both fields (and others I have but not relevant for this issue) is:

protected function validator(array $data)
{
    $customMessages = [
        'required' => ':attribute es obligatori',
        'min' => ':attribute cal que siga, com a mínim, :min caracters',
        'max' => ':attribute cal que siga, com a màxim, :max caracters',
        'string' => ':attribute cal que siga una cadena de caracters',
        'int' => ':attribute cal que siga un enter',
        'email' => ':attribute es un email',
        'confirmed' => 'No has confirmat el correu electrònic'
    ];

    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'dni' => ['required', 'string', 'min:9', 'max:9'],
        'surname1' => ['required', 'string', 'max:50'],
        'surname2' => ['required', 'string', 'max:50'],
        'v1' => ['required', 'string', 'max: 100'],
        'v2' => ['required', 'string', 'max: 100'],
        'district' => ['required', 'int'],
        'section' => ['required', 'int'],
        'chair' => ['required', 'string'],
        'municipality' => ['required'],
        'province' => ['required'],
        'n_census' => ['required', 'int'],
        'n_participants' => ['required', 'int', 'max: 50']
    ], $customMessages);
} 

Is it possible to use Validator::make() to do that?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
VicentGN
  • 23
  • 5

1 Answers1

2

Use this

return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'dni' => ['required', 'string', 'min:9', 'max:9'],
            'surname1' => ['required', 'string', 'max:50'],
            'surname2' => ['required', 'string', 'max:50'],
            'v1' => ['required', 'string', 'max: 100'],
            'v2' => ['required', 'string', 'max: 100'],
            'district' => ['required', 'int'],
            'section' => ['required', 'int'],
            'chair' => ['required', 'string'],
            'municipality' =>  ['required'],
            'province' =>  ['required'],
            'n_participants' => ['required', 'int', 'max: 50'],
            'n_census' => ['required', 'int','gte:n_participants'],
        ], $customMessages);

added gte:n_participants in condtion https://laravel.com/docs/6.x/validation#rule-gt

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33