Adding a field to registration form #724
-
Hi, I am trying to edit the registration forms as shown as in the docs: https://backpackforlaravel.com/docs/6.x/base-how-to#add-one-or-more-fields-to-the-register-form-1. I need to add birth_date to it. But when following the steps shown in the docs it does not work for me and gives errors. Is this part of the documentation outdated or how should I do it in version 6? I also have a second question, is it possible to show the login form on another part of the site? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @Tuoww, Thanks for posting here. I was able to get it working by following these steps (I will update the documentation soon):
<?php
namespace App\Http\Controllers\Admin\Auth;
use Backpack\CRUD\app\Http\Controllers\Auth\RegisterController as BackpackRegisterController;
use Illuminate\Support\Facades\Validator;
class RegisterController extends BackpackRegisterController
{
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$user_model_fqn = config('backpack.base.user_model_fqn');
$user = new $user_model_fqn();
$users_table = $user->getTable();
$email_validation = backpack_authentication_column() == 'email' ? 'email|' : '';
return Validator::make($data, [
'name' => 'required|max:255',
backpack_authentication_column() => 'required|' . $email_validation . 'max:255|unique:' . $users_table,
'password' => 'required|min:6|confirmed',
'birth_date' => 'required|date:dd/mm/yyyy'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return User
*/
protected function create(array $data)
{
$user_model_fqn = config('backpack.base.user_model_fqn');
$user = new $user_model_fqn();
return $user->create([
'name' => $data['name'],
backpack_authentication_column() => $data[backpack_authentication_column()],
'password' => bcrypt($data['password']),
'birth_date' => $data['birth_date']
]);
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// override register controller
$this->app->bind(
\Backpack\CRUD\app\Http\Controllers\Auth\RegisterController::class,
\App\Http\Controllers\Admin\Auth\RegisterController::class
);
}
//
}
<h2 class="card-title text-center my-4">{{ trans('backpack::base.register') }}</h2>
<form role="form" method="POST" action="{{ route('backpack.auth.register') }}">
@csrf
<div class="mb-3">
<label class="form-label" for="name">{{ trans('backpack::base.name') }}</label>
<input autofocus tabindex="1" type="text" class="form-control {{ $errors->has('name') ? 'is-invalid' : '' }}" name="name" id="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<div class="invalid-feedback">{{ $errors->first('name') }}</div>
@endif
</div>
<div class="mb-3">
<label class="form-label" for="{{ backpack_authentication_column() }}">{{ trans('backpack::base.'.strtolower(config('backpack.base.authentication_column_name'))) }}</label>
<input tabindex="2" type="{{ backpack_authentication_column()==backpack_email_column()?'email':'text'}}" class="form-control {{ $errors->has(backpack_authentication_column()) ? 'is-invalid' : '' }}" name="{{ backpack_authentication_column() }}" id="{{ backpack_authentication_column() }}" value="{{ old(backpack_authentication_column()) }}">
@if ($errors->has(backpack_authentication_column()))
<div class="invalid-feedback">{{ $errors->first(backpack_authentication_column()) }}</div>
@endif
</div>
<div class="mb-3">
<label class="form-label" for="password">{{ trans('backpack::base.password') }}</label>
<input tabindex="3" type="password" class="form-control {{ $errors->has('password') ? 'is-invalid' : '' }}" name="password" id="password" value="">
@if ($errors->has('password'))
<div class="invalid-feedback">{{ $errors->first('password') }}</div>
@endif
</div>
<div class="mb-3">
<label class="form-label" for="password_confirmation">{{ trans('backpack::base.confirm_password') }}</label>
<input tabindex="4" type="password" class="form-control {{ $errors->has('password_confirmation') ? 'is-invalid' : '' }}" name="password_confirmation" id="password_confirmation" value="">
@if ($errors->has('password_confirmation'))
<div class="invalid-feedback">{{ $errors->first('password_confirmation') }}</div>
@endif
</div>
+ <div class="mb-4">
+ <label class="form-label" for="birth_date">Birth Date</label>
+ <input tabindex="3" type="date" class="form-control {{ $errors->has('birth_date') ? 'is-invalid' : '' }}" name="birth_date" id="birth_date" value="">
+ @if ($errors->has('birth_date'))
+ <div class="invalid-feedback">{{ $errors->first('birth_date') }}</div>
+ @endif
+ </div>
<div class="form-group">
<div>
<button tabindex="5" type="submit" class="btn btn-primary w-100">
{{ trans('backpack::base.register') }}
</button>
</div>
</div>
</form>
I hope this will resolve your issue. Cheers! |
Beta Was this translation helpful? Give feedback.
Hey @Tuoww,
Thanks for posting here. I was able to get it working by following these steps (I will update the documentation soon):
RegisterController
by overriding the default one. Remember to add validation for your field and populate the value when saving to theUser
model.