lib/formslib.php no submit button pressed

Jump to: navigation, search

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('pathtoformdescription');
//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 redefine your form elements. An exemple used to be in blog/edit.php but not anymore.