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

justincone's avatar

Spark: Custom Validator for registration?

In Spark, I'm trying to swap out the default registration Validator for a custom one.

(Why? I need to do some fancy checks on the email address that go beyond the scope of simple validation rules.)

In Laravel\Spark\Interactions\Auth\CreateUser, I found a baseValidator method that defines the Validator for registration. So then I tried setting up a Spark::swap in SparkServiceProvider:

Spark::swap(
    'CreateUser@baseValidator',
    function($request){
      $validator = Validator::make(
          $request->all(),
            [
                'first_name' => 'required|max:255',
                'last_name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:user',
                'password' => 'required|confirmed|min:6',
                'country' => 'required',
                'terms' => 'required|accepted',
              ]
      );

      if ($validator->fails()) {
      
     // I'll insert code to handle special email validation cases here.
         
       return back()
            ->withErrors($validator)
            ->withInput();
      }
    }
);

But it appears that this Spark::swap call is being completely ignored. When I attempt to register, the default validation rules are run instead of the ones defined above.

Does anyone know what I'm doing wrong? Thanks in advance!

0 likes
3 replies
justincone's avatar

@CRONIX - I tried those initially, but they didn't give me the control I needed. In a non-Spark Laravel app, I built a custom validator that checks for an email's uniqueness, and then (if it's not unique), it sends an internal notification and redirects the user to a special view. I needed to do the same thing in Spark.

I ended up using an approach you suggested here:

https://laracasts.com/discuss/channels/spark/laravel-spark-swapinteract-and-private-variables

That allowed me to wholesale copy the entire CreateUser class and go crazy.

So thank you—both here in this thread and in the other one. :-)

Please or to participate in this conversation.