FormValidation: Check for unique values between fields of repeatableProperty

30 views
Skip to first unread message

Joachim Kuhnert

unread,
Feb 16, 2018, 7:05:53 AM2/16/18
to jenkin...@googlegroups.com

Hello,

 

I would like to check if the content of a text field of a repeatable property is unique (in the context of the project) when the user enters a text.

 

Let’s say I have a Plugin with a structure like that:

 

class Parent extends Builder {

   List<Child> objectsWithUniqueName;

}

 

 

class Child implements Describable<Child> {

   String name;  // has to be unique in project

 

   public static final class DescriptorImpl extends Descriptor<JenkinsConfiguration> {

 

      FormValidation doCheckName(@QueryParameter String value, @AncestorInPath AbstractProject project) {

          // TODO: check uniqueness of name

      }

 

   }

}

 

 

<f:entry>

  <f:repeatableProperty field="objectsWithUniqueName" header="Unique Child" noAddButton="false" />

</f:entry>

 

 

I am able to get the List of  Child-Objects of the Project that were saved the last time via the AbstractProject (cast to Project and use getBuilkders()-Method). Since the user is currently configuring the project he could have made cahnged (changed the name value of different Children or added/removed Children) there could be name clashes that are not yet saved or already solved.

Is it possible to get the currently not yet stored values for the siblings of  form?

 

Best Regards,

Joachim

 

---------------------------------------------------

Joachim Kuhnert

fon:  +49 30 394 09 683 39

mail: Joachim.Ku...@PikeTec.com

 

PikeTec GmbH, Waldenserstr. 2-4, 10551 Berlin

Management: Eckard Bringmann, Andreas Krämer, Jens Lüdemann

Location of the company: Berlin (Germany)

Trade register: Amtsgericht Berlin HRB 105491 B

Website: www.piketec.com

 

Markus Winter

unread,
Feb 19, 2018, 4:42:32 PM2/19/18
to jenkin...@googlegroups.com
You could write some javascript to achieve this. I once did this though the repeatable property didn't have an own jelly file.
I'm no javascript expert though there might be a better way to get the elements. The risk would be if there are other elements that have the same name, then you could get false positives.


<f:repeatable field="objectWithUniqueName" var="f" header="Test">
  <f:entry field="uniqueId">
    <f:textbox onchange="checkForDuplicates()" onkeyup="checkForDuplicates()"/>
  </f:entry>
</f:repeatable>
<div id="uniqueId-error" class="error" style="display:none"></div>

function StringSet()
{
  var setObj = {}, val = "true"

  this.add = function(str)
  {
    setObj[str] = val
  }

  this.contains = function(str)
  {
    return setObj[str] === val
  }
}
 
function checkForDuplicates()
{
  var uniqueIds = document.getElementsByName("_.uniqueId")
  var errorElement = document.getElementById("uniqueId-error")
  var uniqueIdSet = new StringSet()
  var hasError = false
  for (i=0; i<locationIds.length; i++)
  {
    if (uniqueIdSet.contains(uniqueIds[i].value)
    {
      hasError = true
      errorElement.style.display = "block"
      errorElement.innerHTML = "Duplicate ID: " + uniqueIds[i].value
      break;
    }
  }
  if (!hasError)
  {
    errorElement.style.display = "none"
--
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/4e635b05567d409485c8eb4e61e695f7%40piketec.com.
For more options, visit https://groups.google.com/d/optout.

Jesse Glick

unread,
Feb 19, 2018, 5:08:05 PM2/19/18
to Jenkins Dev
On Fri, Feb 16, 2018 at 7:04 AM, Joachim Kuhnert
<Joachim...@piketec.com> wrote:
> Is it possible to get the currently not yet stored values for the siblings
> of form?

Not easily. Unless this is really critical, recommend just printing a
warning at runtime and skipping the duplicate child.
Reply all
Reply to author
Forward
0 new messages