One of the things that I've been trying to get to work in XPages is getting validation to reject the default value of a combobox. Well, this morning I sat down and decided to get it done. I've used other methods to get around this until now, but this seems to do it.
I have a combobox with "--Select--" as the default value. If that value is still there on submission, I want a display error control to execute. To do this, I think you need to have "--Select--" as the itemValue as well as the itemLabel, but I'm not sure, as I habitually use both anyway.
Add a validateContraint, and use the RegEx "^((?!--Select--).)*$" , I got this from this StackOverflow Post. So on submission, if a value hasn't been selected (errm, sorry) no dice.
I tested on a combobox that needed the user to select the month, the entire element is below.
There may be a better way, but having this will be a relief for me.
Cheers,
Brian
<xp:comboBox
id="comboBox1"
value="#{document1.rptCover}"
style="width:125.0px"
defaultValue="--Select--">
<xp:this.validators>
<xp:validateConstraint
regex="^((?!--Select--).)*$"
message="Select a month">
</xp:validateConstraint>
</xp:this.validators>
<xp:selectItem
itemLabel="--Select--"
id="selectItem15" itemValue="--Select--"/>
<xp:selectItem
itemLabel="January"
itemValue="January"
id="selectItem1" />
<xp:selectItem
itemLabel="February"
itemValue="February"
id="selectItem2" />
<xp:selectItem
itemLabel="March"
itemValue="March"
id="selectItem3" />
<xp:selectItem
itemLabel="April"
itemValue="April"
id="selectItem4" />
<xp:selectItem
itemLabel="May"
itemValue="May"
id="selectItem5" />
<xp:selectItem
itemLabel="June"
itemValue="June"
id="selectItem6" />
<xp:selectItem
itemLabel="July"
itemValue="July"
id="selectItem7" />
<xp:selectItem
itemLabel="August"
itemValue="August"
id="selectItem8" />
<xp:selectItem
itemLabel="September"
itemValue="September"
id="selectItem9" />
<xp:selectItem
itemLabel="October"
itemValue="October"
id="selectItem10" />
<xp:selectItem
itemLabel="November"
itemValue="November"
id="selectItem11" />
<xp:selectItem
itemLabel="December"
itemValue="December"
id="selectItem12" />
</xp:comboBox>
To minify your code you could also use a formula item for everything return something like
ReplyDeletereturn ["- Select one -|", "'January|01", "February|02", ...]
and so on. Much cleaner and lesser code.
thanks for the tips~ works for me
ReplyDelete