How to bind a checkbox on config.jelly with a Builder class?

658 views
Skip to first unread message

Code

unread,
Apr 17, 2014, 11:46:17 PM4/17/14
to jenkin...@googlegroups.com
This is the builder class which is based on HelloWorldBuilder.

public class LogInfoBuilder extends Builder {
    private final TimerSettings settings = new TimerSettings();
    
    private final List<String> infoCollection = new ArrayList<String>();

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public LogInfoBuilder(String key, boolean isStart) {
        settings.setKey(key);
        settings.setIsStart(isStart);
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getKey() {
        return settings.getKey();
    }
        
    public boolean isStart()
    {
    return settings.getIsStart();
    }

and this is config.jelly 
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
  <!--
    This jelly script is used for per-project configuration.

    See global.jelly for a general discussion about jelly script.
  -->

  <!--
    Creates a text field that shows the value of the "name" property.
    When submitted, it will be passed to the corresponding constructor parameter.
  -->
  <f:entry title="Key" field="key">
    <f:textbox />
  </f:entry>
  
  <!--
  <f:entry title="Start?" field="isstart">
    <select name="isStart">
      <option value="true" selected="${it.isstart}">Yes</option>
      <option value="false" selected="${!it.isstart}">No!</option>
    </select>
  </f:entry>
  -->
  
  <f:entry title="Starting point?" description="If checked, this will be the starting point.">
    <f:checkbox name="start" checked="${it.start}"/>
  </f:entry>
</j:jelly>


The checkbox shows up on job configuration page but I cannot set the value from it, I mean check or uncheck the checkbox on that page doesn't affect the value in builder class.

What did I do wrong in the builder and/or the jelly file?

Ulli Hafner

unread,
Apr 18, 2014, 9:37:08 AM4/18/14
to jenkin...@googlegroups.com
For the data binding the names must exactly match:

jelly: field=„isStart“
Java: Constructor: isStart
Java: Getter: getIsStart
  
--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Jesse Glick

unread,
Apr 18, 2014, 10:36:01 AM4/18/14
to jenkin...@googlegroups.com
Also you need only specify field="isStart" on the f:entry. f:checkbox
need have no attributes at all.

Code

unread,
Apr 20, 2014, 10:50:10 PM4/20/14
to jenkin...@googlegroups.com
Thanks for your suggestion! This is the updated code.

    public String getKey() {
        return settings.getKey();
    }
        
    public boolean getStart()
    {
    return settings.getIsStart();
    }
    
    @DataBoundSetter
    public void setStart(boolean value)
    {
    settings.setIsStart(value);
    }

@DataBoundSetter is required to make this works, not sure why.

This is config.jelly:

  <f:entry title="Starting point?" description="checked = yes, unchecked = no">
    <f:checkbox name="start" field="start" default="true" value="${start}"/>
  </f:entry>

Again, thanks a lot. It works now!
Reply all
Reply to author
Forward
0 new messages