Adding Registration Form Fields in Yii2
Dan Ongudi
1 min read
Step 1: Update the Database
Add the new columns first_name
and last_name
to the users
table in your database.
Step 2: Update the Signup Form Model
In frontend/models/SignupForm.php
, add the new fields to the rules section to make them required:
public function rules()
{
return [
['first_name', 'required'],
['last_name', 'required'],
// other rules
];
}
Step 3: Update the Signup Function
Modify the signup function to include the new fields:
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->first_name = $this->first_name;
$user->last_name = $this->last_name;
// other assignments and save logic
}
}
Step 4: Update the Signup View
In frontend/views/site/signup.php
, add the new fields to the registration form:
<?= $form->field($model, 'first_name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'last_name')->textInput(['autofocus' => true]) ?>
By following these steps, you will successfully add the first_name
and last_name
fields to the registration form in your Yii2 application.
0
Subscribe to my newsletter
Read articles from Dan Ongudi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Registration FormForm FieldsYii2 GuideYii2 TipsYii2 CustomizationYii2PHP frameworkWeb DevelopmentTutorialForm Handling
Written by