Search code examples
javavalidationpluginsjenkinsjelly

How to validate a form from a config.jelly on Jenkins?


I can do it from a global.jelly file, but it doesn't work from a config.jelly. Here is the procedure for a global.jelly file:

Jelly:

<f:entry title="Value" field="value">
    <f:textbox />
</f:entry>

JAVA:

public static final class Descriptor extends BuildStepDescriptor<Builder>{

    //descriptor's code

    /**
     * Performs on-the-fly validation of the form field 'value'.
     * 
     * @param value
     *            This parameter receives the value that the user has typed.
     * @return Indicates the outcome of the validation. This is sent to the
     *         browser.
     */
    public FormValidation doCheckValue(@QueryParameter String value) throws IOException, ServletException {
        if(value.isEmpty()) {
            return FormValidation.warning("You must fill this box!");
        }
        return FormValidation.ok();
    }
}

This doesn't apply anymore when the jelly code is placed in a configuration file (config.jelly), no matter whether the doCheckValue method is placed in the plugin class or in its descriptor.


Solution

  • Here is what it becomes with a config.jelly file. The textbox takes one addional attribute : checkUrl.

    Jelly:

    <f:entry title="Value" field="value">
        <f:textbox 
            checkUrl="'descriptorByName/NAME_OF_YOUR_JAVA_CLASS/checkValue?value='+escape(this.value)" />
    </f:entry>
    

    Note: this.value is specific to Javascript. It gets the value of your value variable. Don't touch it.

    The Java code remains the same.