I am allowing users to register on my site. I want to have the field "full name" on the registration page which, from there I will eventually split that value to populate the first name & last name fields for each user within the CP.

I have the actual in put in place:


<h3><label for="fullName">Full Name</label></h3>
<input id="fullName" type="text" id="fullName" name="fields[fullName]"
{%- if account is defined %} value="{{ account.fullName }}"{% endif -%}>

{% if account is defined %}
    {{ errorList(account.getErrors('fullName')) }}
{% endif %}

I have created a plugin to grab the value of the fullName input. I am using the init() method to listen for the saveUser event which is working perfectly.


public function init()
    craft()->on('users.saveUser', function (Event $event) {
        error_log('-- saveUser event called --');
        error_log(print_r($event, true));

        if ($event->params['isNewUser']) {
            error_log('-- creating a new user --');


            // split the fullname into firstname & lastname

            // populate first name

            // populate last name

            // save the new user
        } else {
            error_log('-- updating an existing user --');

I can see the data come through in the logs, but I'm not sure how to get to it.


[_attributes:Craft\BaseModel:private] => Array
                                [locale] => en_us
                                [elementId] => 123
                                [fullName] => aaa bbb
                                [id] => 123

The "fullName" parameter is getting populated, but when I log the out put:



I am getting the error Undefined index: fullName. The user is getting created.

I can get to the data using this:


But then I'm not sure how to get that back into the user model so it gets saved.


Sigh, well if it isn't obvious I don't give up too easily. I am able to capture the data like this. Is this the correct way? I will have a large number of registrations, so I'm trying to do things the correct way as much as possible.


$event->params['fullName'] = craft()->request->getPost('fields.fullName');
error_log($event->params['fullName']);  // aaa bbb

Here is is in the CP: enter image description here

The trick I'm seeing now is how to inject (for lack of better words) the values from fullName into firstName and lastName.

Here is what I am using to try to do that (without much success)

$name = explode(' ', craft()->request->getPost('fields.fullName'));
$lastName = array_pop($name);
$firstName = implode(' ', $name);

$event->params['fullName'] = craft()->request->getPost('fields.fullName');
$event->params['firstName'] = $firstName;
$event->params['lastName'] = $lastName;

"Full Name" (as you can see in the attachment) is getting populated, but "First Name" and "Last Name" are not. In the logs I can see it:

 [isNewUser] => 1
 [fullName] => aaa bbb
 [firstName] => aaa
 [lastName] => bbb

But the First Name & Last Name fields in the user account tab are not getting populated.

1 Answer 1


From the onSaveUser event docs, there are two parameters that are passed to everyone listening. One is isNewUser, which you're accessing correctly: $event->params['isNewUser']. But the other is user, which is a full-blown UserModel object that you can access the properties of.

But onSaveUser isn't the event you want, since you want to make changes to the user before they've been saved. You want onBeforeSaveUser, which has the exact same parameters.

Having said that, names are tricky things around the globe. I highly recommend reading this article on falsehoods that programmers believe about names.

Here is some code that should work for the most basic use cases.

craft()->on('users.saveUser', function (Event $event) {

    // Only do this for new users.
    if ($event->params['isNewUser'])
        $fullName = craft()->request->getPost('fields.fullName');

        // Make sure this isn't a user being saved from the control panel.
        if ($fullName)
            $name = explode(' ', $fullName);

            // No spaces, it could be Madonna or Cher.
            if (count($name) == 1)
                $firstName = $name[0];
                $lastName = '';
            // It's a "normal" first and last name.
            else if ($count($name) == 2)
                $firstName = $name[0];
                $lastName = $name[1];
                // The first element is the first name. Combine the rest into a last name separated by spaces.
                $firstName = array_pop($name);
                $lastName = implode(' ', $name);

            // Grab the user.
            $user = $event->params['user'];

            $user->firstName = $firstName;
            $user->lastName = $lastName;

Note that UserModel already has a getFullName method that does some basic logic combining the firstName and lastName properties.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.