jeudi 29 décembre 2016

Send Validator Instance key along with keys given in rule method

Issue

Validator Instance is not sending the InvalidAttempts key along with other keys. Instead, it send when the validation is passed for all keys given in rule method.

My request class is like below

class RegisterRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'Password'      => 'required',
            'EmailAddress'  => 'required',
            'UserName'      => 'required'

        ];
    }    
}

I am trying to add a key which will inform user that that are left with this many attempts and for that I am writing below code and my final request class becomes like below.

class RegisterRequest extends Request
{
    use ThrottlesLogins;

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'Password'     => 'required',
            'EmailAddress' => 'required',
            'UserName'     => 'required'            
        ];
    }

    protected function getValidatorInstance()
    {
        $instance = parent::getValidatorInstance();
        $instance->after(function ($validator) {
            $this->CheckTotalAttempts($validator);
        });
        return $instance;
    }

    public function CheckTotalAttempts($validator)
    {
        if ($this->hasTooManyRegisterAttempts($this)) {
            $validator->errors()->add('InvalidAttempts', $this->TotalAttemptsLeft($this) 
                                     . " attempts left.");
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire