Moodle Custom Form updating

903 Views Asked by At

I have created custom form in Moodle and inserted the data into moodle database. Also i used most of the field in form as autocomplete like below eg

$mform->addElement('autocomplete', 'SBName', get_string('searcharea', 'search'), $sbnames, $options);

Then, I need to update this form . I got record from db as a array, Then i need to add db values in my update form to proceed further.

Here I could not populate the values in edit form for the field of autocomplete $mform->addElement('autocomplete.

Kindly help on this to apply the values in edit form.

Please let me know Is there any way to do this in moodle

1

There are 1 best solutions below

0
On

A few things to note - in Moodle variables and database field names should all be lowercase, so you will find things easier if you call the field 'sbname' instead of 'SBName'.

You mention you have "got the record from the db as an array" - none of the Moodle database access functions return a record as an array, they all return records as objects (although they will return multiple records as an array of objects) - please double-check the code you are using to retrieve the record, if it is reaching you as an array.

Finally, to initialise values when editing a form, the usual method is to write (outside the form itself):

$form = new name_of_form();
$form->setData($recordfromdatabase);

Where $recordfromdatabase is the record object returned by one of the $DB->get_record_* functions.

After that, you can use the $form->getData() function to retrieve the submitted values and $form->display() to display the form itself.

There are lots of examples in the core Moodle code that you can follow for this.