Help in moodle form manipulation

Help in moodle form manipulation

by linh ly -
Number of replies: 6

I'm really new to Moodle and im not sure if im posting this in the right place. I want to create just a simple form, display it in the activity and get the action buttons to work. So here's my code:

simple_form.php

<?php
require_once("$CFG->libdir/formslib.php");

class simplehtml_form extends moodleform {
//Add elements to form
function definition() {
global $CFG;

$mform =& $this->_form; // Don't forget the underscore!

$mform->addElement('header', 'tieudecapnhat', 'Tiêu đề cập nhật');
$mform->addElement('text', 'email', 'Thư điện tử:'); // Add elements to your form
$mform->setType('email', PARAM_NOTAGS); //Set type of element
$mform->setDefault('email', 'Please enter email'); //Default value


// $sql = "select * from mdl_donvi";
// $rs = $DB->get_records_sql($sql);
$FORUM_TYPES=array('1' => 'một','2' => 'hai');
$mform->addElement('select', 'donvi', get_string('selectdonvi', 'facebookintergration'), $FORUM_TYPES);

$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data) {
$returnarray = array();
$returnarray['email']=get_string('required');
$returnarray['donvi']=get_string('required');
return $returnarray;
}
}


And this is how i instantiate the form in view.php

//include simplehtml_form.php
require_once('simpleform.php');

//Instantiate simplehtml_form
$mform = new simplehtml_form();

//Form processing and displaying is done here
if ($mform->is_cancelled()) {

/*
* Link that should redirect to the main page of the course.
*/
die('cancelled');
//Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
die('validated');
//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.
$toform='invalid@email.com';
//Set default data (if any)
$mform->set_data($toform);
//displays the form
$mform->display();
}


No matter what i put in the handle cancel and validate operation section, everytime i hit the "Save changes" or "Cancel" button, seems like it always tries to redirect me back to the View.php, and display the error:

"error/You must specify a course_module ID or an instance ID"

I know it is asking for a course module ID but it's redirecting itself and i don't know where to put the course module ID.

I'm not really good at PHP too. Any help would be appreciated. Sorry for my bad english.

Average of ratings: -
In reply to linh ly

Re: Help in moodle form manipulation

by Adrian Greeve -
Picture of Core developers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers

Perhaps you could try looking at this page - http://docs.moodle.org/dev/Blocks_Advanced

It gives a fairly detailed explanation of creating a form and processing submitted and edited data, as well as some other useful information regarding development in the moodle environment.

Good luck with your future development.

Average of ratings: Useful (1)
In reply to Adrian Greeve

Re: Help in moodle form manipulation

by linh ly -

I have read and done exactly the same as the instruction in that page. Maybe im not good enough to see where my mistake is. Maybe you can take a look in it? I would be very appriciated.

In reply to linh ly

Re: Help in moodle form manipulation

by Nora Wester -

Hallo linh ly,


have you solved the problem?

I have the same problem and the documentation linked above couldn't help me as well.
I would be appreciate if you (or anyone else) could give me a hint.

In reply to linh ly

Re: Help in moodle form manipulation

by Darko Miletić -

You need to add course or activity id to the form data. You can do it like this:

// Inside definition method
$activity_id
= optional_param('id', false, PARAM_INT);
if ($activity_id) {
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', $activity_id);
}



Average of ratings: Useful (2)