How can I make an admin_setting_* field require a value?

How can I make an admin_setting_* field require a value?

by Joshua Johnston -
Number of replies: 4

HI,

I have a few admin settings that require an admin to enter a value. Is there a flag on the admin_setting subclasses that would make sure the field is not empty? I would hate to have to subclass each type just to enforce this requirement.

Average of ratings: -
In reply to Joshua Johnston

Re: How can I make an admin_setting_* field require a value?

by Darko Miletić -

You have to subclass each type. Settings library is major PITA. Begs for a rewrite.

In reply to Darko Miletić

Re: How can I make an admin_setting_* field require a value?

by Darko Miletić -

Be also careful with that check and make sure it does not prevent module from being installed. During module installation Moodle will detect and offer settings for a user to set of a new module. If there are no defaults and user does not enter anything Moodle will not stop showing them until some resonable value is entered by the user. This also screws CLI installations/upgrades.

Average of ratings: Useful (1)
In reply to Darko Miletić

Re: How can I make an admin_setting_* field require a value?

by Joshua Johnston -

Thanks for the insight. It looks like I'd be better off making a custom settings page and having an 'enable' checkbox that controls the settings that I want to make required.

 

Backstory: A plugin I am writing requires you to enter the URL of the remote server that it is using for an activity module and without the URL the plugin cannot function.

In reply to Joshua Johnston

Re: How can I make an admin_setting_* field require a value?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I think a more feasible option is to just have your plugin display a polite error message if it is not set up properly.

Think about this scenario: an admin installs your plugin into their Moodle, but does not currently know where the other server will be. They have to be able to click 'Save changes' on the 'Update settings' page to complete the install, so the only viable option is for them to leave that setting blank for now, and fill it in later.