Answers.Body: <p>From the <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onSaveUser" rel="nofollow">onSaveUser</a> event docs, there are two parameters that are passed to everyone listening.  One is <code>isNewUser</code>, which you're accessing correctly: <code>$event-&gt;params['isNewUser']</code>.  But the other is <code>user</code>, which is a full-blown <a href="http://buildwithcraft.com/docs/templating/usermodel" rel="nofollow">UserModel</a> object that you can access the properties of.</p>
  
  <p>But <code>onSaveUser</code> isn't the event you want, since you want to make changes to the user <em>before</em> they've been saved.  You want <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onBeforeSaveUser" rel="nofollow">onBeforeSaveUser</a>, which has the exact same parameters.</p>
  
  <p>Having said that, names are tricky things around the globe.  I highly recommend reading <a href="http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/" rel="nofollow">this article</a> on falsehoods that programmers believe about names.</p>
  
  <p>Here is some code that should work for the most basic use cases.</p>
  
  <pre><code>craft()-&gt;on('users.saveUser', function (Event $event) {
  
      // Only do this for new users.
      if ($event-&gt;params['isNewUser'])
      {
          $fullName = craft()-&gt;request-&gt;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];
              }
              else
              {
                  // 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-&gt;params['user'];
  
              $user-&gt;firstName = $firstName;
              $user-&gt;lastName = $lastName;
          }
      }
  }
  </code></pre>
  
  <p>Note that UserModel already has a <a href="http://buildwithcraft.com/classreference/models/UserModel#getFullName-detail" rel="nofollow">getFullName</a> method that does some basic logic combining the <code>firstName</code> and <code>lastName</code> properties.    </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-22T00:30:13.907Z
Answers.LastActivityDate: 2015-11-22T00:30:13.907Z
Answers.UserDisplayName: Brad Bell
Body: <p>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 &amp; last name fields for each user within the CP.</p>
  
  <p>I have the actual in put in place:</p>
  
  <p><strong>twig</strong></p>
  
  <pre><code>&lt;h3&gt;&lt;label for="fullName"&gt;Full Name&lt;/label&gt;&lt;/h3&gt;
  &lt;input id="fullName" type="text" id="fullName" name="fields[fullName]"
  {%- if account is defined %} value="{{ account.fullName }}"{% endif -%}&gt;
  
  {% if account is defined %}
      {{ errorList(account.getErrors('fullName')) }}
  {% endif %}
  </code></pre>
  
  <p>I have created a plugin to grab the value of the <code>fullName</code> input. I am using the <code>init()</code> method to listen for the <code>saveUser</code> event which is working perfectly.</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('users.saveUser', function (Event $event) {
          error_log('-- saveUser event called --');
          error_log(print_r($event, true));
  
          if ($event-&gt;params['isNewUser']) {
              error_log('-- creating a new user --');
  
              error_log($event-&gt;params['fullName']);
  
  
              // split the fullname into firstname &amp; lastname
  
              // populate first name
  
              // populate last name
  
              // save the new user
          } else {
              error_log('-- updating an existing user --');
          }
      });
  }
  </code></pre>
  
  <p>I can see the data come through in the logs, but I'm not sure how to get to it.</p>
  
  <p><strong>error_log</strong></p>
  
  <pre><code>...
  [_attributes:Craft\BaseModel:private] =&gt; Array
                              (
                                  [locale] =&gt; en_us
                                  [elementId] =&gt; 123
                                  [fullName] =&gt; aaa bbb
                                  [id] =&gt; 123
                              )
  ...
  </code></pre>
  
  <p>The "fullName" parameter is getting populated, but when I log the out put:</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>error_log($event-&gt;params['fullName']);
  </code></pre>
  
  <p>I am getting the error <code>Undefined index: fullName</code>. The user <em>is</em> getting created.</p>
  
  <p>I <em>can</em> get to the data using this: </p>
  
  <pre><code>craft()-&gt;request-&gt;getPost('fields.fullName');
  </code></pre>
  
  <p>But then I'm not sure how to get <em>that</em> back into the user model so it gets saved.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>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 <em>correct</em> way as much as possible.</p>
  
  <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>$event-&gt;params['fullName'] = craft()-&gt;request-&gt;getPost('fields.fullName');
  error_log($event-&gt;params['fullName']);  // aaa bbb
  </code></pre>
  
  <p>Here is is in the CP:
  <a href="https://i.sstatic.net/oK4Dr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oK4Dr.png" alt="enter image description here"></a></p>
  
  <p>The trick I'm seeing now is how to inject (for lack of better words) the values from <code>fullName</code> into <code>firstName</code> and <code>lastName</code>.</p>
  
  <p>Here is what I am using to try to do that (without much success)</p>
  
  <pre><code>$name = explode(' ', craft()-&gt;request-&gt;getPost('fields.fullName'));
  $lastName = array_pop($name);
  $firstName = implode(' ', $name);
  
  $event-&gt;params['fullName'] = craft()-&gt;request-&gt;getPost('fields.fullName');
  $event-&gt;params['firstName'] = $firstName;
  $event-&gt;params['lastName'] = $lastName;
  </code></pre>
  
  <p>"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:</p>
  
  <pre><code> [isNewUser] =&gt; 1
   [fullName] =&gt; aaa bbb
   [firstName] =&gt; aaa
   [lastName] =&gt; bbb
  </code></pre>
  
  <p>But the First Name &amp; Last Name fields in the user account tab are not getting populated.</p>
  
Tags: <plugin-development><registration>
Title: How can I get custom user registration field parameters
ThreadText: # How can I get custom user registration field parameters
  
  (Asked by: username_0 on 2015-11-21)
  
  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:
  
  **twig**
  
  ```
  <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.
  
  **MyPlugin.php**
  
  ```
  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 --');
  
              error_log($event->params['fullName']);
  
              // 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.
  
  **error\_log**
  
  ```
  ...
  [_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:
  
  **MyPlugin.php**
  
  ```
  error_log($event->params['fullName']);
  
  ```
  
  I am getting the error `Undefined index: fullName`. The user *is* getting created.
  
  I *can* get to the data using this:
  
  ```
  craft()->request->getPost('fields.fullName');
  
  ```
  
  But then I'm not sure how to get *that* back into the user model so it gets saved.
  
  **EDIT**
  
  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.
  
  **MyPlugin.php**
  
  ```
  $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](https://i.sstatic.net/oK4Dr.png)](https://i.sstatic.net/oK4Dr.png)
  
  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.
  
  **Tags:** plugin-development, registration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-22. Score: 0 | Accepted answer
  
  From the [onSaveUser](http://buildwithcraft.com/docs/plugins/events-reference#users-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](http://buildwithcraft.com/docs/templating/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](http://buildwithcraft.com/docs/plugins/events-reference#users-onBeforeSaveUser), which has the exact same parameters.
  
  Having said that, names are tricky things around the globe. I highly recommend reading [this article](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) 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];
              }
              else
              {
                  // 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](http://buildwithcraft.com/classreference/models/UserModel#getFullName-detail) method that does some basic logic combining the `firstName` and `lastName` properties.
  
  
  ---
  
  