Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samy918's avatar

Display error with own request class?

I've created an own request class and added two rules for validating google's 2fa.

How can I display error messages?

My ValidateSecretRequest

public function __construct() {
      Validator::extend('valid_token', function ($attribute, $value, $parameters, $validator) {
         $secret = $this->user->google2fa_secret;

         return Google2FA::verifyKey($secret, $value);
      }, 'Not a valid token');


      Validator::extend('used_token', function ($attribute, $value, $parameters, $validator) {
         $key = $this->user->id . ':' . $value;
         return !Cache::has($key);
      }, 'Used Token');
   }```



0 likes
1 reply
robrogers3's avatar
Level 37

display it in a view? if so, they should be available in the blade.errors thingy.

@if($errors->has()) @foreach ($errors->all() as $error) {{ $error }} @endforeach @endif

let me know if it's an ajax thingy. I can help there too. it's simple.

1 like

Please or to participate in this conversation.