dimanche 12 janvier 2020

Can I override laravel 5.6 validation?

I am trying to add condition based validation. I have a detect function which is responsible for detecting that input is email or a number

$detectInput = $this->EmailService->detectInput($request->email);

So $detectInput always returns 0 or 1 or 2

$messages = [
    'required' => 'The :attribute field is required.',
    'unique' => 'The :attribute already exists.',
    'email' => 'The :attribute is invalid',
];

$rules = [
    'email'     => 'required|string',
];


    $detectInput = $this->EmailService->detectInput($request->email);
    if ($detectInput===1 || $detectInput===0) {
        $rules =  array_set($rules, 'email', 'email|unique:users');
    }

    if ($detectInput===2) {
        $rules =  array_set($rules, 'email', 'unique:users,login_number');
    }

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

The first if-statement is working fine, but I am not able to override when second if-statement get true. I want to override validation that check the input from users.login_number columns value, where value is duplicated or no instead of users.email.

can someone kindly guide me about it, I would appreciate it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire