Form binding for an optional JobProperty

56 views
Skip to first unread message

Marc Carter

unread,
Jan 3, 2016, 1:09:56 PM1/3/16
to Jenkins Developers
Is there a way to simplify this further?  It strikes me as a basic pattern for anybody implementing a JobProperty...

I currently have the following jelly for a JobProperty
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:optionalBlock name="useTemplate" title="${%Use another job as a template}" checked="${instance != null}" inline="true">
  <f:entry field="templateJobName" ...

Notes:
* uses existence of the property to indicate "checked".
* use of "inline" to avoid useTemplate becoming a container of the rest of the form.

But it still requires a custom Descriptor.newInstance:
@Extension
public static class DescriptorImpl extends JobPropertyDescriptor {
  @Override
  public JobProperty<?> newInstance(StaplerRequest request, JSONObject formData) throws FormException {
    return formData.has("useTemplate")?request.bindJSON(TemplateImplementationProperty.class, formData):null;
  }


Daniel Beck

unread,
Jan 3, 2016, 4:48:31 PM1/3/16
to jenkin...@googlegroups.com
Jenkins 1.637 introduced OptionalJobProperty which makes this easier.

Since this is very recent, note the pros/cons of choosing this as a compatible Jenkins release for a plugin:

https://wiki.jenkins-ci.org/display/JENKINS/Choosing+Jenkins+version+to+build+against
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/d78b27ec-3bd8-47a9-b4f9-2a20bfe2bfc5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marc Carter

unread,
Jan 3, 2016, 5:47:47 PM1/3/16
to Jenkins Developers, m...@beckweb.net
That's exactly it, thanks. I'll leave the code as is with a TODO to upgrade in future to OptionalJobProperty.
Reply all
Reply to author
Forward
0 new messages