Author - StudySection Post Views - 1,213 views
moodle

Adding/Removing Form Elements In Moodle/Totara

How to add/remove form elements before form rendering in moodle/totara?

To work with Moodle forms, sometimes it’s easier and in some circumstances, it’s difficult to deal with. When you have no custom requirements or design changes in the form, then it is easier to use moodle form. When you have some custom requirements that do not fit with standard form elements or you have to do some specific things with data then it is hard to achieve.

In this post, we will discuss how we can modify form elements before the form is rendered on the browser. There are multiple cases in which we have to make dynamic changes to these form elements on page load. For examples:

  1. When you need to check or uncheck an element under some condition. We can do this by javascript, but if you want to use the moodle standard way(moodle PHP code) then it makes it difficult.
  2. If you want to append new element i..e. show an error message.
  3. If you want to remove an element based on some condition.

Moodle provides us with a definition_after_data method for doing all these things. This method is used for manipulating displayed data or form elements. In this method, we can also set default values for form elements.

Code:


class course_edit_form extends moodleform {

protected function definition($mform) {
$mform->addElement(‘header’, ‘general’, ‘Header’);
$mform->addElement(‘advcheckbox’, "course_checkbox", $repository->name);
$mform->setType("course_checkbox", PARAM_BOOL);
$mform->addElement(‘text’, ‘course_text’, ‘Some text input’);
} // definition

public function definition_after_data() {
parent::definition_after_data();
$mform =& $this->_form;
// Example 1
$course_text =& $mform->getElement(‘course_text’);
$course_checkbox =& $mform->getElement(course_checkbox);
if (isset($course_checkbox->_attributes[‘checked’])) {
$course_text->attributes[‘value’] = "The checkbox is checked";
} // if
// Example 2
$mform->insertElementBefore( $mform->createElement('submit', 'cancel', get_string('cancel')), ‘course_text’);
// Example 3
$mform->removeElement(‘course_text’);
} // definition_after_data
} // class

Before making the changes to form data, you have to call the parent::definition_after_data() first. This method is also known as after definition(), set_data() and data submission.You can also change the visibility of form elements and add custom css to these elements later.

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP certification exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri