title: Custom Rule class in Laravel - how to get message working?
body: <p>Working on an existing project, so keeping the code structure the same as other developers is critical. </p>
  
  <p>I've created a new <code>Rule</code> and added my <code>Validator::extend</code> to the <code>boot()</code>. However I can't get the validation error to display my custom message. Instead is just displays a default "validation.ruleName"</p>
  
  <p>I'm writing a Validation rule that uses ImLiam's NHS Number validation package. The <code>validate()</code> and <code>passes()</code> work successfully. But the error message never changes.</p>
  
  <p><strong><em>How can I get the <code>messages()</code> method to display the custom output of the exception? (Instead of "validation.nhsnumber" in screenshot below?</em></strong></p>
  
  <p><a href="https://i.stack.imgur.com/o6mIU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o6mIU.png" alt="enter image description here"></a></p>
  
  <p><strong>App\Forms\MyForm:</strong> (using <a href="https://github.com/kristijanhusak/laravel-form-builder" rel="nofollow noreferrer">Kris Forms</a>)</p>
  
  <pre><code>public function buildForm()
  {
      $this-&gt;add('nhs_number', 'number', [
          'label' =&gt; 'NHS Number',
          'rules' =&gt; 'nullable|nhsnumber',
      ]);
      ...
  }
  </code></pre>
  
  <p><strong>App\Providers\AppServiceProvider:</strong></p>
  
  <pre><code>public function boot()
  {
  
          Validator::extend('nhsnumber', \App\Rules\ValidNhsNumber::class);
  }
  </code></pre>
  
  <p><strong>App\Rules\ValidNhsNumber:</strong></p>
  
  <pre><code>namespace App\Rules;
  
  use Illuminate\Contracts\Validation\Rule;
  use ImLiam\NhsNumber\InvalidNhsNumberException;
  use ImLiam\NhsNumber\NhsNumber;
  
  class ValidNhsNumber implements Rule
  {
      private $message;
  
      /**
       * Create a new rule instance.
       *
       * @return void
       */
      public function __construct()
      {
          //
      }
  
      /**
       * Determine if the validation rule passes.
       *
       * @param string $attribute
       * @param mixed $value
       * @return bool
       */
      public function passes($attribute, $value): bool
      {
          try {
              $nhsNumber = new NhsNumber($value);
              $nhsNumber-&gt;validate(); // Will throw exception here if false
              return true;
          } catch (InvalidNhsNumberException $e) {
              $this-&gt;message = $e-&gt;getMessage();
          }
      }
  
      public function validate($attribute, $value, $params): bool
      {
          return $this-&gt;passes($attribute, $value);
      }
  
      /**
       * Get the validation error message.
       *
       * @return string
       */
      public function message()
      {
          return $this-&gt;message;
      }
  }
  </code></pre>