General developer forum

Minimum source for simple form

 
Picture of Andreas Duerr
Minimum source for simple form
 

Hello!

I'm new in developing plugins for Moodle and I'm stumpling up on the most easy problem. I tried to use the simple form api. The easiest thing was just the follwing code in my view.php


$mform = new mod_subrooms_mod_form();

$mform->display();


My form definition looks like this:

class mod_subrooms_mod_form extends moodleform {

public function definition() {

  global $CFG;

  $mform = $this->_form;

  $mform->addElement('submit','submitbutton','Change');

}

}


In Moodle I can see my submit button, but when I push it I just get an error message ("Coding error detected, it must be fixed by a programmer: notlocalisederrormessage").


I filled up the source code in my view.php with the surrounding 


if ($mform->is_cancelled()) {

} else if ($mreturn=$mform->get_data()) {

} else {

$mform->display();

}


No effect, I added rules, the validation function in my form-class - again no effect.

I looked to other plugins but I can find no significant difference.


My question is, what is the minimum code not to get an error message while using the simple form api?


Thanks for helping,

Andy


 
Average of ratings: -
Picture of Robin Stark
Re: Minimum source for simple form
 
Hey Andy, 


Did you find a solution to this?  I'm running into the same problem.


Thanks, 


Rob

 
Average of ratings: -
Picture of Richard Jones
Re: Minimum source for simple form
Particularly helpful MoodlersPlugin developersTesters

I'm pretty new to programming Moodle myself but there is a function (eg):

$this->add_action_buttons($cancel=true, $submitlabel="Save");

The optional parameters determine whether there is a cancel button and what text goes in the submit button.

You add that to the end of the form definition class and it does the work for you.

https://docs.moodle.org/dev/Form_API

 
Average of ratings: -
Picture of Robin Stark
Re: Minimum source for simple form
 

Hey, 

Thanks for replying, I noticed that as well and I'm still getting the error, how are you creating the instance of the form?  ie: $mform = new SomeForm();

Thanks, 

Rob

 
Average of ratings: -
Picture of Richard Jones
Re: Minimum source for simple form
Particularly helpful MoodlersPlugin developersTesters

Here's a form (mod_form.php):

require_once("$CFG->libdir/formslib.php");
/**
 * Example form
 */
class mod_form extends moodleform{
    /**
     * Defines forms elements
     */
    public function definition() {
        $mform = $this->_form;
        // Adding header (use get_string appropriately to your module)
        $mform->addElement('header', 'general', 'lang string header');
        // Adding standard text field
        $mform->addElement('text', 'name', 'language string here', array('size'=>'64'));
       // IMPORTANT: add validation and type rules as per documentation
        $this->add_action_buttons();     
    }
}

Here's a use case (from the dev docs - Form_API) in view.php:

$mform = new mod_form();
//Form processing and displaying is done here
if ($mform->is_cancelled()) {
    //Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
  //In this case you process validated data. $mform->get_data() returns data posted in form.
} 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.
 
  //displays the form
  $mform->display();
}

I coded both these in Moodle's root and it displays a form.  Of course you need to do quite a bit more standard Moodle coding (page, context, etc) to make it usable in your plugin but hopefully this shows there's nothing particularly complicated about creating and using a simple form.


 
Average of ratings: -