discuss.answer
-
- 571 Posts
I wanted to do exactly the same thing.
This code has worked for me.
Add a prehook to the registration form
The
fullname snippet code
<?php
$fullname = $hook->getValue('firstName').' '.$hook->getValue('lastName');
$hook->setValue('fullname',$fullname);
return true;
In the html
<input type="hidden" name="fullname" id="fullname" value="[[!+firstName]] [[!+lastName]]" />
-
- 708 Posts
Perfect. Andy, you are a star!
That's fantastic, thank you very much.
Todd
-
- 708 Posts
Andy,
Your solution works perfectly with the Register snippet but I just noticed that when using the UpdateProfile snippet to uh... update the user profile from the front-end, that editing either the "first" or "last" name fields does not update the "fullname" field on the Manager > Users page.
I added the &preHooks property and hidden text field to the UpdateProfile form but it doesn't seem to have any effect.
Todd
-
- 571 Posts
It looks like I have the same problem on my website!
I think it makes sense. It is related to the sequence of events. The new firstname and lastname have not been submitted when the prehook fetches them. On my site it appears that if you update the profile twice it puts the new firstname or lastname into fullname.
Would making the prehook a posthook work?
I not I think it might be necessary to use Javascript to capture the new firstname or lastname field contents.
There are a couple of conversations about this.
https://forums.modx.com/thread/73670/solved-combine-extended-fields-first--lastname-in-fullname
https://forums.modx.com/thread/44821/manipulating-registration-data-in-a-prehook-snippet
I can't look into it right now, but will do so later, as I need to sort it on my own site!
-
- 24,544 Posts
Since firstname and lastname are not actual fields anywhere, updating them won't have any effect. You could make them extended fields. Or you could use JavaScript to update the hidden fullname field whenever they change.
A simpler option would be to just use the fullname (no first of last input) after the user has registered and let the user update that. You could extract the first and last names with a snippet whenever you needed it. Since you got them separately in Register, you don't have to worry about middle names and the like.
You could also use ClassExtender to add those new fields, but that seems like overkill for the use case.
-
- 708 Posts
I did use ClassExtender to add "firstName" and "lastName", and while redundant it does simplify a couple things but clearly doesn't solve this current problem.
Out of curiosity how could they be extracted with a snippet?
The entire reason for using "firstName" and "lastName" is to ensure people actually enter two names. The "fullname" field is too easy to dismiss.
Honestly, I find it quite strange MODX doesn't default to "first" and "last" instead of fullname. If it did it would make all of this hoop-jumping entirely unnecessary. Maybe in MODX 3.0.
Todd
-
- 24,544 Posts
Ah, are you doing this on the update profile page?
[[!ExtUserUpdateProfile]]
[[!UpdateProfile]]
I agree that there should be firstname and lastname fields. I think almost everyone who uses MODX would agree.
-
- 571 Posts
I realised that my problem was that I had omitted the prehook from my profile update page. Now that I have it in place the update profile page works correctly and updates the Full Name using the first and last name fields.
Could you post the code for your update profile page?
It should start with
[[!ExtUserUpdateProfile]]
[[!UpdateProfile?
&useExtended=`0`
It should also contain this somewhere
-
- 708 Posts
Thanks Andy, it's working now. I was using the hidden text field and that was preventing fullname from updating.
[ed. note: todd.b last edited this post 5 years, 11 months ago.]
Todd