General developer forum

 
 
Emperor Joshua Norton I, Emperor of the United States and Protector of Mexico.
Setting hidden fields in form (2.3.2)
 

I'm using Moodle 2.3.2.

I'm trying to create a custom form that will pass the user id, the course id, and a new field called "original_expiry". I've defined the form like this:

function definition() {

$mform =& $this->_form;

$mform->addElement('header','extendlogin',get_string('extendlogin'));
$mform->addElement('hidden','original_expiry','original_expiry',$original_expiration);
$mform->addElement('hidden','studentid','studentid',$student->id);
$mform->addElement('hidden','courseid','courseid',$course->id);
$mform->addElement('hidden','id','id',$student->id);
$mform->addElement('hidden','course','course',$course->id);
$mform->addElement('date_selector','enddate',get_string('newloginexpirationdate'));
$mform->addElement('text','reason',get_string('changeloginreason'),$attributes='size=20,maxlength=250');

$mform->set_data('studentid');
$mform->set_data('original_expiry');
$mform->set_data('courseid');
$mform->set_data('id');
$mform->set_data('course');

$this->add_action_buttons(false,get_string('change'));

}

 

(Ideally "course" and "id" would be passed in a query string, but the strip_querystring function in moodleform precludes this -- a topic for someone's rant, I'm sure.)

Now, unfortunately, only two of these hidden fields get set:

<input name="studentid" type="hidden" value="3" />
<input name="courseidtype="hiddenvalue="7" />  

Why is this? How can I get my form to set all of the hidden fields?

 
Average of ratings: -
Davo
Re: Setting hidden fields in form (2.3.2)
Group DevelopersGroup Particularly helpful Moodlers

There seem to be a few things wrong with the form as you currently have it.

Firstly, in the field definition you have the defaults set via uninitialised variables - no where in the function you have shown have you set the values of '$student->id' or '$course->id', yet you have attempted to use these to set the defaults in the form.

Secondly you have lots of 'set_data' function calls that don't do anything useful and should be removed completely.

If you want to pass values into your form, then the usual way of doing so is as follows:

class my_form extends moodleform {
function definition() {
...
}
}

$studentid = optional_param('studentid', 0, PARAM_INT); // One way to get the student id in the first place

$form = new my_form();
$data = new stdClass();   // or data loaded from the database, or an array, as appropriate
$data->studentid = $studentid; // Set up all the data you want to pass into the form
$data->courseid = ...;
etc.

$form->set_data($data); // This is the important bit where the data gets set.
if ($result = $form->get_data()) {
... do something with the $results from the form
}

 
Average of ratings: -
Emperor Joshua Norton I, Emperor of the United States and Protector of Mexico.
Re: Setting hidden fields in form (2.3.2)
 

Thanks for the response. Love the avatar. big grin

Here is the complete form definition, which I should probably have included last time:

 

function dlc_student_extend_login_form ($original_expiration, $student, $course) {
global $CFG, $DB;

require_once("{$CFG->libdir}/formslib.php");

class login_extension_form extends moodleform {

function definition() {

$mform =& $this->_form;

$mform->addElement('header','extendlogin',get_string('extendlogin'));
$mform->addElement('hidden','original_expiry',$original_expiration);
$mform->addElement('hidden','studentid',$student->id);
$mform->addElement('hidden','courseid',$course->id);
$mform->addElement('hidden','id',$student->id);
$mform->addElement('hidden','course',$course->id);
$mform->addElement('date_selector','enddate',get_string('newloginexpirationdate'));
$mform->addElement('text','reason',get_string('changeloginreason'),$attributes='size=20,maxlength=250');

$this->add_action_buttons(false,get_string('change'));

}
}
}


The data comes in through the call to the form, and is populated that way.

 
Average of ratings: -
Davo
Re: Setting hidden fields in form (2.3.2)
Group DevelopersGroup Particularly helpful Moodlers

No, that still won't work.

I'm not even sure you can define a class inside a function (and even if you can, it looks like a bad idea to me).

Even if you are able to define a class inside a function (which is exposing the limits of my PHP knowledge a bit here), parameters to the outer function are not valid within the score of either the class OR the methods within that class.

e.g.

function myfunction($a, $b, $c) {
class myclass {
// $a, $b, $c are not defined in here, as they belong to the scope of the outer function
function mymethod() {
// $a, $b, $c don't exist here either
}
}
}

As I've already mentioned above, if you want to pass values into a form, then you should call $form->set_data($data); on an instance of the form initialised outside of the form's code.

To have a clearer idea of what you are trying to do, it would be helpful for you to include the code where you use the form, rather than just the code you have used to define the form.

i.e. where have you called something like: $form = new login_extension_form(); ? followed by $form->get_data(); and $form->display(); ?

 
Average of ratings: -
Picture of Hubert Chathi
Re: Setting hidden fields in form (2.3.2)
 

You can define classes and functions within another function (and when you do require_once a library file inside a function, that's essentially what you're doing), but they get defined in the global scope, and behave exactly like if they were defined outside of the function.  As Davo mentioned, unlike languages such as JavaScript, they do not have access to the function's variables.

 
Average of ratings: -
Emperor Joshua Norton I, Emperor of the United States and Protector of Mexico.
Re: Setting hidden fields in form (2.3.2)
 

Okay, I admit that I am very confused at this point, and I'm not sure I've explained the issue I'm having properly.

As I described, and as I showed in the source code, I'm trying to get five hidden fields to show up in my form. When the form is rendered and I check the page source code, however, this is what I see:

 

<input name="studentid" type="hidden" value="3" />
 <input name="courseidtype="hiddenvalue="7" />  

 

As you can see, only two of the hidden form elements even exist, even though the data have been set properly in those two elements. What's happening is that the other three hidden form elements are not even rendering into the form. So regardless of the data (which seem to be getting passed properly), how can I get these hidden form elements to show up on the page I'm building?


ETA: I've figured out what I was doing wrong and it was incredibly stupid. Thanks to all who were attempting to help out.

 
Average of ratings: -
Picture of Diane Villemure
Re: Setting hidden fields in form (2.3.2)
 

Richard, please share what you were doing wrong. I'll be asked to do the same thing as you soon and I will likely stumble into "incredibly stupid" too.

 
Average of ratings: -
Emperor Joshua Norton I, Emperor of the United States and Protector of Mexico.
Re: Setting hidden fields in form (2.3.2)
 

Diane,

I doubt that my situation will be of value to you. I actually have the form defined in a separate file, rather than in core Moodle code (though I do have the include line in the Moodle code). I was simply editing the wrong file.

Richard

 
Average of ratings: -