General developer forum

Disable element if instance is being modified

 
Picture of David Poly
Disable element if instance is being modified
 

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: -
Picture of Sam Chaffee
Re: Disable element if instance is being modified
Core developersParticularly helpful Moodlers
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

 
Average of ratings: -
Picture of David Poly
Re: Disable element if instance is being modified
 

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


Thanks a lot!

 
Average of ratings: -