We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34103
    • 47 Posts
    I need two extended fields, "firstname" and "lastname", but I don't want to have additional the built in field "fullname". How can I combine the two separate fields in the field "fullname"? [ed. note: istvan.velsz last edited this post 12 years, 3 months ago.]
    • I've done this in two different ways. One was to use Javascript to populate a hidden field with the content of the two fields. The second was to have the form's processing combine the two fields.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 34103
        • 47 Posts
        Thanks, I solved it with some Javascript
          • 37044
          • 68 Posts
          Just to any future readers - I now understand that Mark Hamstra has added this as a feature request (http://tracker.modx.com/issues/7695) - personally I think adding a salutation would not be a bad idea either I will try and add to the request.

          Anyway here is the (jQuery) javascript I used to combine my extended fields for first and last into full name. The event is triggered on form submit.

          (NB obviously change form / field names to match yours)

          Nasty hack but works for the time being...

          <script type="text/javascript">
          $(document).ready(function() {
          	$("#reviewer_register_form").submit(function() {
          		$fullname = $("#First").val() + " " + $("#Last").val();
          		$("#fullname").val($fullname);
          		// un-coment to debug alert("done");
          		return true;
          	});
           });
          </script>
          
            • 28120
            • 380 Posts
            I'm facing this issue currently.

            With the above, if there's validation on first and last names, and the user leaves one of them blank, how do I pre-populate value=""
              • 16348
              • 64 Posts
              I have been using this code as a hook for the Register and UpdateProfile snippets for some time to do the same thing:
              $firstname = $hook->getValue('firstname');
              $lastname = $hook->getValue('lastname');
              $fullname = $firstname . ' ' . $lastname;
              $hook->setValue('fullname',$fullname);
                
              return true;
              
                • 28120
                • 380 Posts
                Thanks Kristoffer, I will try this now.

                Is the a pre or post hook?
                  • 28120
                  • 380 Posts
                  ok, it's a post hook

                  This is working now - really appreciate you pointing me in the right direction.

                  Actually

                  $hook->setValue('fullname',$fullname);


                  didn't work. The debug code showed that $fullname was set correctly but setValue wasn't saving.

                  But your solution led me to uncover http://forums.modx.com/thread/?thread=72933&page=1 which took a different approach

                  $profile = $hook->getValue('register.profile');
                  $profile->set('fullname',$fullname);
                  $profile->save();
                  


                  which worked.

                  Still be interested to know why setValue works for you though.
                    • 16348
                    • 64 Posts
                    Sorry, I should have said that my code should be used as a pre hook.
                    The pre hook is executed before the fields array are saved to the database so all edits to that array in the hook should be saved.

                    Glad that it worked anyway.