Validate settings parameters form

Validate settings parameters form

by alberto lara -
Number of replies: 2

there is any way to validate the settings parameters for a custom block?
In my custom block i'm added a settings.php file with some settings like this:

$settings->add(new admin_setting_heading( 'headerconfig', get_string('headerconfig', 'block_simplehtml'),

get_string('descconfig', 'block_simplehtml') ));

When I access to setting page for this block I want to validate some fields when i submit the form like Form Api.

/admin/settings.php?section=blocksettingemployability

I want to validate some fields when the form is submited.

This is posible in a block?. Another way?

Average of ratings: -
In reply to alberto lara

Re: Validate settings parameters form

by Brian King -

Well, I know it's a late answer for the original question, but for the next person that comes looking:

You have two options that I can see:

1) create a subclass that extends the class you want custom validation for, and override the validate method:

class admin_setting_configtext_custom extends admin_setting_configtext {
    public function validate($data) {
        // your custom validation logic here
    }
}

This probably makes sense to do if you only need custom behaviour for one or two of several settings or setting types.


2) If you need custom validation for most of your fields, look into using an external admin settings page.

In reply to Brian King

Re: Validate settings parameters form

by Christophe Demko -

You can also pass a regular expression (delimited by //) as the 5th argument of the admin_setting_configtext constructor:

$settings->add(
    new admin_setting_configtext(
        'mymodule/myparameter',
        get_string('myparameter', 'mymodule'),
        get_string('myparameter_config', 'mymodule'),
        '1',
        '/^(0\.[0-9]*[1-9])|1$/'
    )
);

This will check that the myparameter value will be between 0 (excluded) and 1