Working with moodleform class

Working with moodleform class

by Andrew Feller -
Number of replies: 7
While developing a block that relies upon the lib/formslib.php library, I encountered some issues. Whenever I render the form after processing the form submission, the form is still set with the values from the previous form submission. This is an issue because some combination of options cause others to be ignored, so reloading the form would result in a different looking form.

Is it possible to clear the values set to a form before rendering it?
Average of ratings: -
In reply to Andrew Feller

Re: Working with moodleform class

by Andrew Feller -
Another question: how do I mark a field as having errors while processing the data?
In reply to Andrew Feller

Re: Working with moodleform class

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In the validation method, return an array of 'fieldid' => 'errormessage'. You need to validate everything in this method before you start processing anything.
In reply to Tim Hunt

Re: Working with moodleform class

by Andrew Feller -
Thanks Tim, I just started playing with validation() late yesterday evening. Coincidentally, it is 'fieldname' => 'errormessage', which is a little relief as PEAR's Quickform library automatically generates IDs.

Ok, that will work but it leads to another question: Where should the work of form submission be done at? In the page's form or in the page itself? Is the ideal that people will validate their data within the validation() method and do the actual work in the page?
In reply to Tim Hunt

Re: Working with moodleform class

by Andrew Feller -
Question: Is there a way to determine which fields already have errors within the validation method?

I see it is possible to overwrite the error message for a field by specifying the field name and new error message in the array returned from the validation method. However, I would like to know of fields with errors from the rules added to the elements within the definition() method, so I don't overwrite them.
In reply to Andrew Feller

Re: Working with moodleform class

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
If possible, do all the validation in validate, and all the processing in the page.


Start your validation with an $errors = parent::validation() call. Then you can inspect $errors to see what has already been flagged.
In reply to Tim Hunt

Re: Working with moodleform class

by Andrew Feller -
Thanks for the response Tim!

I called the form's parent::validation() method, however it returns an empty array by default. Apparently, the moodleform class doesn't do any validation when the method is called; simply provides it as a placeholder for subclasses to override.

After looking into PEAR's QuickForm code, it appears errors are stored in the QuickForm's _errors property. It is possible to get them by specifying $this->_form->_errors within the form, however I don't know if that is the recommended approach.
In reply to Andrew Feller

Re: Working with moodleform class

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hmm yes, you are right. The $errors array is for any additional validation, on top of the standard validation done by the rules. Perhaps we should think about changing that, to enable what you want to do. ...