简体   繁体   中英

How to make one field from two required at least with JSF/Primefaces

I'm using primefaces with jsf and i want to make one of two fields required at least. that means that the error message will be displayed if this two fields are empties togheter: this is a sample of my code:

<h:outputLabel for="srcNumber" value="Originator MSISDN (EXAMPLE 32495959595)" />
<p:inputText id="srcNumber" value="#{cdrMmscRecBean.srcNumber}" label="srcNumber" />
<h:outputLabel for="destNumber" value="Destination MSISDN (EXAMPLE 32495959595)" />
<p:inputText id="destNumber" value="#{cdrMmscRecBean.destNumber} label="destNumber" />

thanks :)

You can implement it this way:

<p:inputText id="srcNumber" value="#{cdrMmscRecBean.srcNumber}" label="srcNumber" 
     required="#{empty cdrMmscRecBean.destNumber}" requiredMessage="SRC Number Required">
    <p:ajax event="change" update="destNumber" />
</p:inputText>

<p:inputText id="destNumber" value="#{cdrMmscRecBean.destNumber}" label="destNumber"
     required="#{empty cdrMmscRecBean.srcNumber}" requiredMessage="DEST Number Required">
    <p:ajax event="change" update="srcNumber" />
</p:inputText>

For more reference on how to parametrize your validation message:

If you want to show the validation error use <p:message for="srcNumber" /> and same for test number, get rid of your outputLabels, this will show the validations warnings.

You neeed to add the required="true" flag to your inputTexts as well.

This is primefaces

EDIT Purpose of the h:outputLabel and its "for" attribute this here shows the outputLabel for using non primefaces to show validatoin messages if this is all your problem was then u just need to add the required="true" validation flag indicators on your input texts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM