Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

lib/formslib.php no submit button pressed: Difference between revisions

From MoodleDocs
m (text 'see the extra conditional branch' added)
(changed set_defaults to set_data since method name changed and moved set_data to before data_submitted so that data loaded from db etc will be used for default for hidden and/or disabled elements)
Line 23: Line 23:
$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
//default 'action' for form is strip_querystring(qualified_me())
//default 'action' for form is strip_querystring(qualified_me())
$mform->set_data($toform);


if ($mform->is_cancelled()){
if ($mform->is_cancelled()){
Line 52: Line 53:
     //put data you want to fill out in the form into array $toform here then :
     //put data you want to fill out in the form into array $toform here then :


    $mform->set_defaults($toform);
     $mform->display();
     $mform->display();
     print_footer($course);
     print_footer($course);

Revision as of 09:55, 21 January 2007

The moodleform 'no_submit_button_pressed' method allows you to detect if a button on your form has been pressed that is a submit button but that has been defined as a button that doesn't result in a processing of all the form data but will result in some form 'sub action' and then having the form redisplayed. This is useful for example to have an 'Add' button to add some option to a select box in the form etc. You define a button as a no submit button as in the example below (in defintion()). This example adds a text box and a submit button in a group.

            $mform->registerNoSubmitButton('addotags');
            $otagsgrp = array();
            $otagsgrp[] =& $mform->createElement('text', 'otagsadd', get_string('addotags', 'blog'));
            $otagsgrp[] =& $mform->createElement('submit', 'addotags', get_string('add'));
            $mform->addGroup($otagsgrp, 'otagsgrp', get_string('addotags','blog'), array(' '), false);
            $mform->setType('otagsadd', PARAM_NOTAGS);

And see the extra conditional branch needed in your code marked by asterisks below.

require_once('pathtoformdesctiption');
//you'll process some page parameters at the top here and get the info about
//what instance of your module and what course you're in etc. Make sure you
//include hidden variable in your forms which have their defaults set in set_defaults
//which pass these variables from page to page


$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
//default 'action' for form is strip_querystring(qualified_me())
$mform->set_data($toform);

if ($mform->is_cancelled()){
    //you need this section if you have a cancel button on your form
    //here you tell php what to do if your user presses cancel
    //probably a redirect is called for!
}
//******* extra code for no_submit_button_pressed
 elseif ($mform->no_submit_button_pressed()) {
    //you need this section if you have a 'submit' button on your form
    //which performs some kind of subaction on the form and not a full
    //form submission.
//*******
} elseif ($fromform=$mform->data_submitted()){
//this branch is where you process validated data.

} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
    //setup strings for heading
    print_header_simple($streditinga, '',
     "<a href=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</a> ->
     $strnav $streditinga", $mform->focus(), "", false);
    //notice use of $mform->focus() above which puts the cursor 
    //in the first form field or the first field with an error.

    //call to print_heading_with_help or print_heading? then :
    
    //put data you want to fill out in the form into array $toform here then :

    $mform->display();
    print_footer($course);

}

Notice that the code in the elseif ($mform->no_submit_button_pressed()) branch will always be executed after your definition() function has been called because definition is called in the moodleform class constructor. So if elements or options in your form should change as a result of the code in your no_submit_button_pressed branch you may need to use methods on MoodleQuickForm to rededine your form elements. You can see an example of this in blog/edit.php