Disable element if instance is being modified

Disable element if instance is being modified

by David Poly -
Number of replies: 2

Hi,

I want to disable a select in my module's mod_form.php file only if the user is modifying the instance, leaving it enabled if he's creating a new instance.


I'm having a hard time figuring this one out.


Any ideas?

Average of ratings: -
In reply to David Poly

Re: Disable element if instance is being modified

by Sam Chaffee -
Picture of Core developers
Hi David,


If your form extends moodleform_mod, which it sounds like it does, you can check to see if the _instance property is set (see below).

/**
* Instance of the module that is being updated. This is the id of the {prefix}{modulename}
* record. Can be used in form definition. Will be "" if this is an 'add' form and not an
* update one.
*
* @var mixed
*/
protected $_instance;
As the comment suggests, If it is non-empty then the form is an 'add' form (new instance) rather than an update. You can then use the freeze method on the $mform (MoodleQuickForm) object or pass the disabled attribute as part of the attribute parameter to the addElement method.

Cheers,

Sam

In reply to Sam Chaffee

Re: Disable element if instance is being modified

by David Poly -

Worked like a charm Sam. I was trying to use the disableif but this is much simpler.


Thanks a lot!