How to split full name from model into firstname and lastname in yii2 view and concat to save in yii2 model


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();

 

 

Advertisements
About

myself pramodh kumar yet another php developer from India and have worked on oops,procedural,yii framework,codeigniter,wordpress,joomla,api and more.

Tagged with: , , , , , , , , , ,
Posted in yii2

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Follow Tutorial on WordPress.com
categories
Calendar
August 2017
M T W T F S S
« Sep   Sep »
 123456
78910111213
14151617181920
21222324252627
28293031  
Follow me on Twitter
Blog Stats
  • 79,471 hits
%d bloggers like this: