$mform and form elements syntax??

$mform and form elements syntax??

by Paul Caliban -
Number of replies: 5

Syntax Question:
I am new to Working on a Moodle form  $mform stuff.

The BACKGROUND

Build test Form that has two fields  'email' and 'username'

'username' is Hidden !!

The GOAL

I would like the see the user type a value for 'email'
and
then trigger (perhaps - onblue or onchange or onSubmit; byID) or some method;
push the 'email' value into>> the 'hidden username' field?

The QUESTION??

What is the proper technique and/or syntax??
GIVEN:
 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
 $mform->addElement('hidden', 'username', null);

// can I call onblur or onchange of element email; put that value into hidden username field
// or some form of:  var da_element = document.getElementById("email");

$mform->setDefault('username', da_element.value );

or????????

$mform->setDefault('username', document.getElementById("email").value );

or

$mform->setDefault('username', $this->myformname['email']);


Average of ratings: -
In reply to Paul Caliban

Re: $mform and form elements syntax??

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This seems like a lot of hard work, not to mention it would have to be written in Javascript and would stop working if that was switched off.

Would it not be easier to just copy the username from the email at the point where the submitted form is processed?
In reply to Davo Smith

Re: $mform and form elements syntax??

by Paul Caliban -

Thanks

Yep a craZy client request.

By Copy I assume you mean you want the "USER" to SELF copy   via Ctrl-C/Ctrl-V

I agree, but the client does not want to even show the PROMPT for username and wants the software to populate that field behind the curtain.

I consdered Making the LABEL for user name say "EMAIL", but then I must deal with  the other existing fields: email and email 2, which moodle validates! ARRRG!

In reply to Paul Caliban

Re: $mform and form elements syntax??

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

No, I definitely did not mean ask the user to use Ctrl-C / Ctrl-V to copy the username across, what I meant was something like this (in PHP, after the form has been submitted):

function process_the_form($formdata) {

...
$formdata->username = $formdata->email;
...

}

In reply to Davo Smith

Re: $mform and form elements syntax??

by Paul Caliban -

Perfect

Thanks much.....

The trick I find is to trace the pgm flow enought to catch the Function before the insert to the DB happens.... 

After some study of the code I get the flow and have resolvded the clients CraZy need.  I normally try to catch the data at the form level before I submit, while the user is typing. Hence onblur,onChange stuff.

Then I do not need to change down stream pgms.

Witha product as complex as Moodle I wanted to avoid changes at all cost, but I am forced to change the Singup form. sad


Thanks again, your comment actually made me think in terms of making the username act and validate like email value.  Problem solved at the form level.
smile

In reply to Davo Smith

Re: $mform and form elements syntax??

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Davo,

What about using function data_preprocessing ?

Joseph