Showing 6 results.
Items per Page 50
of 1

CVP Forum

« Back to General Discussion - All Versions

Require setting A and/or B in custom element

Combination View Flat View Tree View
Threads [ Previous | Next ]
I am writing a custom element, and I need to require setting A and/or B. IE, both can be set, or one can be set, but if neither are set, the project will not validate. 
 
Is there a way to set that in a custom element?

What you are trying to do can be accomplished in CVP 9 using a custom validator (older versions do not have this capability). The following code snippet should do what you want. The way it works is that Setting A is assigned a custom validator that will fail validation if both it and Setting B are left blank.
 1public class CustomElement extends ElementBase implements ElementInterface {
 2    @Override
 3    public Setting[] getSettings() throws ElementException {
 4        Setting settingA = new Setting("settingA", "Setting A", "The first setting",
 5                Setting.OPTIONAL, Setting.SINGLE, Setting.SUBSTITUTION_ALLOWED, Setting.STRING);
 6        Setting settingB = new Setting("settingB", "Setting B", "The second setting",
 7                Setting.OPTIONAL, Setting.SINGLE, Setting.SUBSTITUTION_ALLOWED, Setting.STRING);
 8       
 9        // Validate that at least one of Setting A or Setting B has been set
10        settingA.setCustomValidator(new OrValidator(settingB));
11       
12        return new Setting[] {settingA, settingB};
13    }
14    // The rest of the class has been omitted
15}
16class OrValidator implements SettingValidator
17{
18    private Setting otherSetting;
19   
20    public OrValidator(Setting otherSetting)
21    {
22        this.otherSetting = otherSetting;
23    }
24   
25    /**
26     * Validation method for OrValidator.
27     * @param value The setting to validate.
28     * @param setting Contains information about the setting being validated.
29     * @param allSettingParams Contains information about all settings in the custom element, keyed by each setting's real name.
30     * @return A list of error messages to display.
31     */
32    @Override
33    public List<String> validate(String value, SettingParams params,
34            Map<String, SettingParams> allSettingParams)
35    {
36        List<String> errors = new ArrayList<String>();
37       
38        SettingParams otherSettingParams = allSettingParams.get(otherSetting.getRealName());
39        if (otherSettingParams != null &&
40                StringUtils.isEmpty(otherSettingParams.getCurrentValue()) &&
41                StringUtils.isEmpty(value))
42        {
43            errors.add("At least one of " + params.getDisplayName() + " or " +
44                    otherSettingParams.getDisplayName() + " must be set.");
45        }
46       
47        return errors;
48    }

Matt,
That's great. Where is this new feature documented? Is in the the CVP Programming Guide? Or the Studio Users Guide?
And are there other new features available in the custom java?
I would like to add it to my CVPD-Java training class.
Thanks, Janine

Hi Matt,
I don't see the Validator mentioned in the CVP 9 Programming Guide. Can you post the javadocs for CVP 9 on the documentation page of the CDN? The current javadocs there were posted in 2008.
Thanks, Janine
 

Hi Matt,
I just looked at the CVP 9 javadocs for VxmlServer and I don't see the SettingValidator there. Can you share more information or documentation on this?
Thanks

I'm looking into getting the javadoc on http://developer.cisco.com/web/cvp/documentation updated, but until then I've attached the latest javadoc for CVP 9.0(1). The attached javadoc includes several classes that are missing on the DVD, including com.audium.server.voiceElement.SettingValidator.
 
I misremembered when custom validation was added - it was actually in CVP 8.5(1). The javadoc related to custom validation for CVP 9.0(1) should apply equally well to 8.5(1).
Attachments:

Thanks Matt!

The CVP 9.0(1) javadoc has now been posted to the documentation page.