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

nickclicksco's avatar

Where do I place validation & storage code when adding registration fields?

I'm looking at this documentation: https://spark.laravel.com/docs/6.0/adding-registration-fields

I've managed to modify the view and added the extra JS needed however I'm having trouble with the 'Validation & Storage' section.

Where do I add the code that is being referenced? For example this example PHP block is given:

Spark::validateUsersWith(function () {
    return [
        'name' => 'required|max:255',
        'age' => 'required|integer|min:1',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
    ];
});

I'm just wondering where the correct places are for these bit's of PHP though?

Not sure if this makes much of a difference but I installed Spark via composer as the default installer wouldn't work.

Thanks, Nick

0 likes
4 replies
bobbybouwmann's avatar

I never used spark before, but from reading the docs I think you need to do that in the SparkServiceProviderclass

bobbybouwmann's avatar
Level 88

According to the documentation you need to do this in your service provider

Spark::validateUsersWith(function () {
    return [
        'name' => 'required|max:255',
        'age' => 'required|integer|min:1',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
        'vat_id' => 'max:50|vat_id',
        'terms' => 'required|accepted',
    ];
});

Spark::createUsersWith(function ($request) {
    $user = Spark::user();

    $data = $request->all();

    $user->forceFill([
        'name' => $data['name'],
        'email' => $data['email'],
        'age' => $data['age'],
        'password' => bcrypt($data['password']),
        'last_read_announcements_at' => Carbon::now(),
        'trial_ends_at' => Carbon::now()->addDays(Spark::trialDays()),
    ])->save();

    return $user;
});

Alternatively you can use classes for this

Spark::validateUsersWith('UserValidator@validate');

Spark::createUsersWith('UserCreator@create');
nickclicksco's avatar

Thanks! Found out that the file was within the providers directory: SparkServiceProvider.php

Please or to participate in this conversation.