1

I'm building my first app with Laravel 5.2 & Laravel Spark. The front end is built using Vue.js I believe and despite adding the following to register-common-form.blade.php:

<!-- Username -->
<div class="form-group" :class="{'has-error': registerForm.errors.has('username')}">
    <label class="col-md-4 control-label">Username</label>

    <div class="col-md-6">
        <input type="name" class="form-control" name="username" v-model="registerForm.username" autofocus>

    <span class="help-block" v-show="registerForm.errors.has('username')">
        @{{ registerForm.errors.get('username') }}
    </span>
    </div>
</div>

I can't actually see a way to fully register that extra field so that it is picked up for error handling. I've got it so that the UserRepository handles the field and inserts it, but just can't get the front end errors to show properly.

Is anyone able to help with this at all?

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83

2 Answers2

0

Okay I finally stumbled across it :D

In Laravel\Spark\Interactions\Auth\CreateUser.php there is a $rules method like so:

public function rules($request)
    {
        return [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
            'vat_id' => 'max:50|vat_id',
            'terms' => 'required|accepted',
        ];
    }

All I have done is add my username field, and it works brilliantly!

public function rules($request)
    {
        return [
            'name' => 'required|max:255',
            'username' => 'required|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
            'vat_id' => 'max:50|vat_id',
            'terms' => 'required|accepted',
        ];
    }
Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • when we do `spark:update`, that file would be replaced...am I right? – Zaffar Saffee Jun 24 '16 at 19:46
  • @ZaffarSaffee Yes unfortunately, this is based off of experimentation due to lack of documentation in the beginning. Taylor has now added this as a cookbook tutorial on the Spark documentation with the proper implementation – Andy Holmes Jun 25 '16 at 09:38
0

Above answer is just for validation rules you also need to navigate to spark\src\Repositories\UserRepository.php and add 'username' => $data['username'], to the create() method like this:

public function create(array $data)
{
    $user = Spark::user();

    $user->forceFill([
        'name' => $data['name'],
        'username' => $data['username'], // ADDED THIS
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'confirmation_code' => str_random(30),
        'last_read_announcements_at' => Carbon::now(),
        'trial_ends_at' => Carbon::now()->addDays(Spark::trialDays()),
    ])->save();

    return $user;
}
Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
  • Would this also not be affected by doing an update to Spark? Very different to what Taylor has put here from what I can see - https://spark.laravel.com/docs/4.0/adding-registration-fields – Andy Holmes Sep 22 '17 at 08:41
  • I don't see any differences in the link or docs: "Next, we need to customize Spark's new user validation and storage methods to handle our new age field." – Amir Hassan Azimi Sep 26 '17 at 23:35
  • So is this the `booted` method in the SparkServiceProvider? I'm only querying because you mention `spark\src` in your answer which seems like its going to be overwritten with an update – Andy Holmes Sep 27 '17 at 08:38