Create username with lowercase letters

momentis 14 Mar, 2012
I have a form that displays information submitted by users, which includes their first and last name. The site admins will open the submitted data in a different form. That form will be used to create a Joomla user for the person. The admin form has a field for 'username'. I can certainly pull in the first and last name, combined, to automatically populate the username field. However, I would like to know if there is a way to grab the first initial of the first name, plus the last name, concatenate those and then change the string to lowercase.

My thought is to use a Custom Code action, and perform the above using PHP. If I do, how do I pass what is returned back to the field on my form? Am I way off-base?
GreyHead 14 Mar, 2012
Hi Rick,

That will work well -- all of the form data is held in the $form->data array so you can just add a new variable to it:
<?php
$form->data['username'] = strtolower(substr($form->data['first_name'], 0, 1).$form->data['last_name']);
?>
I forget the requirements for usernames - you might also need to do some cleaning to remove any 'illegal' characters from the resulting name string.

Bob

PS If this was a public site you'd get freqent failures because the names are not unique e.g. John Smith, Jenny Smith and James Smith all give jsmith. That may need thinking through.
momentis 14 Mar, 2012
Well, that certainly is easy! It's not a public site, with a relatively small and varied user base. However, this framework will be adaptable to some of our bigger clients, so I will build with that in mind. To that end, I think I will have the code append a random string of three numbers to the end of the username, so that:

John Smith may become jsmith487

or something along those lines. A similar function will be used to prepopulate the password field with a series of letters and numbers. Thanks for reminding me to add a line to clean the content first!!!
GreyHead 14 Mar, 2012
Hi Rick,

That shounds good - I've used the random string before on a public site quite successfully. It's quite easy to add a check to make sure that the username is unique if you need it.

There's a Random Password option if you scroll down the Settings tab of the Joomla! Registration action.

Bob
momentis 15 Mar, 2012
Bob,

I have the following code in a aCustom Code action, following a DB Record Loader action, in the OnLoad event:

<?php
$form->data['user_username'] = strtolower(substr($form->data['acct_reg_fname'], 0, 1).$form->data['acct_reg_lname']);
?>


Nothing is populating the field 'user_usename'.

The fields 'acct_reg_fname' and 'acct_reg_lname' are the fields displayed on the form, but they are getting their data like:

{ConfirmReg.reg_fname}
{ConfirmReg.reg_lname}

I know I am missing something!!!
GreyHead 16 Mar, 2012
Hi Rick,

If in doubt add a Debugger action to the On Load event to see what data is there.

In this case the inputs themseleves won't havbe any values until the Show HTML action runs which is too late for your Custom Code action to do anything useful. I think you need to use the data from the DB Record Loader instead.
<?php
$form->data['user_username'] = strtolower(substr($form->data['ConfirmReg']['fname'], 0, 1).$form->data[ConfirmReg']['lname']);
?>

Bob
momentis 16 Mar, 2012
That makes sense. I figured I needed to use the DB Record Loader values, but I could NOT come up with the proper syntax!! Thanks so much - it is working perfectly! Once again you saved my butt. 🙂
This topic is locked and no more replies can be posted.