← Back to Custom Fields Support
yuli.mg63391
Hi,
I added new fields to my user register but my custom fields doesn't create. Once user is created I can update, I need to save all fields on register. Do you have any idea?
Vladimir
Hi!
https://www.youtube.com/watch?v=SRTNCMlqP9Q
Adding custom fields
Adding register page - register.htm
title = "register"
url = "/register"
layout = "default"
is_hidden = 0
[account]
redirect = "home"
paramCode = "code"
forceSecure = 0
requirePassword = 0
[session]
security = "all"
==
<?php
use RainLab\User\Models\User as UserModel;
use RainLab\User\Models\Settings as UserSettings;
use Multiwebinc\Recaptcha\Validators\RecaptchaValidator;
use RainLab\Translate\Models\Message;
function isRegisterThrottled()
{
if (!UserSettings::get('use_register_throttle', false)) {
return false;
}
return UserModel::isRegisterThrottled(Request::ip());
}
function makeRedirection($intended = false)
{
$method = $intended ? 'intended' : 'to';
$property = trim((string) $this->components['account']->property('redirect'));
// No redirect
if ($property === '0') {
return;
}
// Refresh page
if ($property === '') {
return Redirect::refresh();
}
$redirectUrl = $this->components['account']->pageUrl($property) ?: $property;
if ($redirectUrl = post('redirect', $redirectUrl)) {
return Redirect::$method($redirectUrl);
}
}
function onRegister()
{
try {
if (!$this->components['account']->canRegister()) {
throw new ApplicationException(Lang::get(/*Registrations are currently disabled.*/'rainlab.user::lang.account.registration_disabled'));
}
if ($this->isRegisterThrottled()) {
throw new ApplicationException(Lang::get(/*Registration is throttled. Please try again later.*/'rainlab.user::lang.account.registration_throttled'));
}
/*
* Validate input
*/
$data = post();
//CUSTOM FIELSD VALIDATIONS
$rules = [
'fieldone' => 'required',
'fieldtwo' => 'required',
];
$customMessages = [
'fieldone.required' => 'Your message1',
'fieldtwo.required' => 'Your message2',
];
$validation = Validator::make($data, $rules, $customMessages);
if ($validation->fails()) {
throw new ValidationException($validation);
}
//END CUSTOM FIELSD VALIDATIONS
if (!array_key_exists('password_confirmation', $data)) {
$data['password_confirmation'] = post('password');
}
$rules = (new UserModel)->rules;
if ($this->components['account']->loginAttribute() !== UserSettings::LOGIN_USERNAME) {
unset($rules['username']);
}
$validation = Validator::make($data, $rules);
if ($validation->fails()) {
throw new ValidationException($validation);
}
/*
* Record IP address
*/
if ($ipAddress = Request::ip()) {
$data['created_ip_address'] = $data['last_ip_address'] = $ipAddress;
}
/*
* Register user
*/
Event::fire('rainlab.user.beforeRegister', [&$data]);
$requireActivation = UserSettings::get('require_activation', true);
$automaticActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_AUTO;
$userActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_USER;
$adminActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_ADMIN;
$user = Auth::register($data, $automaticActivation);
Event::fire('rainlab.user.register', [$user, $data]);
/*
* Activation is by the user, send the email
*/
if ($userActivation) {
$this->components['account']->sendActivationEmail($user);
Flash::success(Lang::get(/*An activation email has been sent to your email address.*/'rainlab.user::lang.account.activation_email_sent'));
}
/*
* Activation is by the admin, show message
* For automatic email on account activation RainLab.Notify plugin is needed
*/
if ($adminActivation) {
Flash::success(Lang::get(/*You have successfully registered. Your account is not yet active and must be approved by an administrator.*/'rainlab.user::lang.account.activation_by_admin'));
}
/*
* Automatically activated or not required, log the user in
*/
if ($automaticActivation || !$requireActivation) {
Auth::login($user);
}
//SAVE CUSTOM FIELDS
$user->setField('userfields', [[
"fieldone" => input('fieldone'),
"fieldtwo" => input('fieldtwo'),
]]);
$user->save();
/*
* Redirect to the intended page after successful sign in
*/
if ($redirect = $this->makeRedirection(true)) {
return $redirect;
}
}
catch (Exception $ex) {
if (Request::ajax()) throw $ex;
else Flash::error($ex->getMessage());
}
}
?>
==
<div class="container">
{% if canRegister %}
<h3>Register</h3>
{{ form_ajax('onRegister') }}
<!-- CUSTOMFIELDS -->
<div class="form-group">
<label for="registerName">Custom field 1</label>
<input
name="fieldone"
type="text"
class="form-control"
id="registerName"
placeholder="fieldone" />
</div>
<div class="form-group">
<label for="registerName">Custom field 2</label>
<input
name="fieldtwo"
type="text"
class="form-control"
id="registerName"
placeholder="fieldtwo" />
</div>
<!-- /CUSTOMFIELDS -->
<div class="form-group">
<label for="registerName">Full Name</label>
<input
name="name"
type="text"
class="form-control"
id="registerName"
placeholder="Enter your full name" />
</div>
<div class="form-group">
<label for="registerEmail">Email</label>
<input
name="email"
type="email"
class="form-control"
id="registerEmail"
placeholder="Enter your email" />
</div>
{% if loginAttribute == "username" %}
<div class="form-group">
<label for="registerUsername">Username</label>
<input
name="username"
type="text"
class="form-control"
id="registerUsername"
placeholder="Enter your username" />
</div>
{% endif %}
<div class="form-group">
<label for="registerPassword">Password</label>
<input
name="password"
type="password"
class="form-control"
id="registerPassword"
placeholder="Choose a password" />
</div>
<button type="submit" class="btn btn-default">Register</button>
{{ form_close() }}
{% else %}
<!-- Registration is disabled. -->
{% endif %}
</div>
Last updated
1-2 of 2