First create a model of the table using GII tool in yii2,then by using CRUD option in GII create MVC file ,which has create,update function in controller and add rules in model like
[[‘first_name’,’last_name’], ‘trim’],
[[‘first_name’,’last_name’], ‘required’],
[[‘first_name’,’last_name’], ‘string’, ‘max’ => 50],
And variable above rules like
public $first_name;
public $last_name;
As the controller render update.php in view folder,the update file render _form.php
In _form.php
I added the php code to split username into first name and last name
$parts = explode(” “, $model->name);
$lastname = array_pop($parts);
$firstname = implode(” “, $parts);$model->first_name = $firstname;
$model->last_name = $lastname;
And the form fields like
<?= $form->field($model, ‘first_name’)->textInput([‘maxlength’ => true]) ?>
<?= $form->field($model, ‘last_name’)->textInput([‘maxlength’ => true]) ?>
In model concat the firstname and lastname into $model->name to save.
$model->name = $this->first_name.’ ‘.$this->last_name;
return $model->save();
Leave a comment