How can I mark XForms checkboxes invalid if a "None of the above" option is selected?
Scenario:
<xf:select appearance="full">
<xf:item>
<xf:label>Vanilla</xf:label>
<xf:value>vanilla</xf:value>
</xf:item>
<xf:item>
<xf:label>Strawberry</xf:label>
<xf:value>strawberry</xf:value>
</xf:item>
<xf:item>
<xf:label>None of the above</xf:label>
<xf:value>none</xf:value>
</xf:item>
</xf:select>
When the "None of the above" 开发者_JS百科option is selected, if any other option is selected as well, the control should be marked as invalid. How can I achieve this?
You can do this with an XPath constraint that looks like this:
<xf:bind
ref="my-value"
constraint="
if (tokenize(., '\s+') = 'none') then
count(tokenize(., '\s+')) = 1
else
true()"/>
What this does is the following:
- if there is a "none" value selected, then make sure there is only that value (
count() = 1
) - if that's not the case, the control is marked as invalid
- if there is no "none" option selected, then the control is valid
Alternatively, or in addition, as Alex points out about above, you could automatically deselect the other options when the user selects the "none" option:
<xf:bind
ref="my-value"
calculate="if (tokenize(., '\s+') = 'none') 'none' else ."
readonly="false()"/>
What this does is:
- if there is a "none" value selected, then make sure the value is set to "none" only
- otherwise, keep the value as is
- also make sure that the value is not readonly (which would be the default when calculate is used)
UPDATE 2016-11-25:
The original solution could also be expressed as:
<xf:bind
ref="my-value"
constraint=". = 'none' or count(tokenize(., '\s+')) != 1"/>
contains() xpath function is best for your requirement. code will be as below.
<xforms:bind nodeset="instance('myform')/node1"
constraint="not(contains(.,'none'))" />
However as others said, you can replace the 'none' with '' if you do not want it to be added to your selection even if the user selects it.
精彩评论