laravelvalidationapache-sparklaravel-spark

Add new rules to team during registration in Laravel Spark


I am currently wanting to modify some of the validation rules during registration on Laravel Spark. I have added a couple new inputs that I want to add to the model, but for some reason the validation is not being hit. Below is the code that I have in my SparkServiceProvider:

Spark::swap('CreateTeam@rules', function () {
            return [
                'name' => 'required|max:255',
                'city' => 'required|max:255',
                'state' => 'required|max:255',
            ];
        });

        Spark::swap('CreateTeam@validator', function ($user, array $data) {
            $validator = \Validator::make($data, Spark::call(static::class.'@rules'));

            $validator->sometimes('slug', 'required|alpha_dash|max:255|unique:teams,slug', function () {
                return Spark::teamsIdentifiedByPath();
            });

            $validator->after(function ($validator) use ($user) {
                $this->validateMaximumTeamsNotExceeded($validator, $user);
            });

            return $validator;
        });

I have also attempted to override the CreateTeam interface and then bind it in my AppServiceProvider but that hasn't worked either. Any help would be appreciated.


Solution

  • I figured it out. Using the Spark::swap method, I was able to replace the default CreateUser@validator method like so:

    Spark::swap('CreateUser@validator', function ($request) {
                /**
                 * @var Validator $validator
                 */
                $validator = $this->baseValidator($request);
    
                $validator->sometimes('team', 'required|max:255', function ($input) {
                    return Spark::usesTeams() &&
                        Spark::onlyTeamPlans() &&
                        ! isset($input['invitation']);
                });
    
                $validator->sometimes(['city', 'state'], 'required|max:100', function ($input) {
                    return Spark::usesTeams() &&
                        Spark::onlyTeamPlans() &&
                        ! isset($input['invitation']);
                });
    
                $validator->sometimes('team_slug', 'required|alpha_dash|max:255|unique:teams,slug', function ($input) {
                    return Spark::usesTeams() &&
                        Spark::onlyTeamPlans() &&
                        Spark::teamsIdentifiedByPath() &&
                        ! isset($input['invitation']);
                });
    
                return $validator;
            });