Answer for comparing two inputtext values in jsf
ashokmignite Jul 10, 2010 6:10 AMHI FRIENDS THIS IS USEFUL TO ALL SEAM USERS !
JSF CODE:
<s:decorate id="minScoreField" template="layout/edit.xhtml">
<ui:define name="label">Minimum Score: </ui:define>
<h:inputText id="minScore"
required="true"
styleClass="normalFont"
value="#{assesmentConfigHome.instance.minScore}"
size="12" >
<f:validateDoubleRange minimum="0" maximum="9" />
<a:support event="onblur" reRender="minScoreField" bypassUpdates="true" ajaxSingle="true"/>
</h:inputText>
</s:decorate>
<s:decorate id="maxScoreField" template="layout/edit.xhtml">
<ui:define name="label">Maximum Score: </ui:define>
<h:inputText id="maxScore"
required="true"
styleClass="normalFont"
value="#{assesmentConfigHome.instance.maxScore}"
size="12" >
<f:validator validatorId="memberValidator" />
<f:attribute name="passwordId" value="assesmentConfig:minScoreField:minScore" />
<f:validateDoubleRange minimum="0" maximum="9" />
<a:support event="onblur" reRender="maxScoreField" bypassUpdates="true" ajaxSingle="true"/>
</h:inputText>
VALIDATOR BEAN CODE:
public class MemberValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
// Obtain the client ID of the first password field from f:attribute.
String passwordId = (String) component.getAttributes().get("passwordId");
// Find the actual JSF component for the client ID.
UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId);
// Get its value, the entered password of the first field.
java.math.BigDecimal mins1 = (java.math.BigDecimal) passwordInput.getValue();
// Cast the value of the entered password of the second field back to String.
// java.math.BigDecimal maxs1=new BigDecimal(0.0);
java.math.BigDecimal maxs1 = (java.math.BigDecimal) value;
if ( (mins1!= null) && (maxs1 != null)&& (mins1.doubleValue()>maxs1.doubleValue())) {
throw new ValidatorException(new FacesMessage("Minimum score must be smaller than Maximum score" ) );
}
// You can even validate the minimum password length here and throw accordingly.
// Or, if you're smart, calculate the password strength and throw accordingly ;)
}
}
ADD FOLLOWING CODE IN FACE-CONFIG.XML
<validator-id>memberValidator</validator-id>
<validator-class>org.domain.pixel.validators.MemberValidator</validator-class>
</validator>
JSF CODE:
<s:decorate id="minScoreField" template="layout/edit.xhtml">
<ui:define name="label">Minimum Score: </ui:define>
<h:inputText id="minScore"
required="true"
styleClass="normalFont"
value="#{assesmentConfigHome.instance.minScore}"
size="12" >
<f:validateDoubleRange minimum="0" maximum="9" />
<a:support event="onblur" reRender="minScoreField" bypassUpdates="true" ajaxSingle="true"/>
</h:inputText>
</s:decorate>
<s:decorate id="maxScoreField" template="layout/edit.xhtml">
<ui:define name="label">Maximum Score: </ui:define>
<h:inputText id="maxScore"
required="true"
styleClass="normalFont"
value="#{assesmentConfigHome.instance.maxScore}"
size="12" >
<f:validator validatorId="memberValidator" />
<f:attribute name="passwordId" value="assesmentConfig:minScoreField:minScore" />
<f:validateDoubleRange minimum="0" maximum="9" />
<a:support event="onblur" reRender="maxScoreField" bypassUpdates="true" ajaxSingle="true"/>
</h:inputText>
VALIDATOR BEAN CODE:
public class MemberValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
// Obtain the client ID of the first password field from f:attribute.
String passwordId = (String) component.getAttributes().get("passwordId");
// Find the actual JSF component for the client ID.
UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId);
// Get its value, the entered password of the first field.
java.math.BigDecimal mins1 = (java.math.BigDecimal) passwordInput.getValue();
// Cast the value of the entered password of the second field back to String.
// java.math.BigDecimal maxs1=new BigDecimal(0.0);
java.math.BigDecimal maxs1 = (java.math.BigDecimal) value;
if ( (mins1!= null) && (maxs1 != null)&& (mins1.doubleValue()>maxs1.doubleValue())) {
throw new ValidatorException(new FacesMessage("Minimum score must be smaller than Maximum score" ) );
}
// You can even validate the minimum password length here and throw accordingly.
// Or, if you're smart, calculate the password strength and throw accordingly ;)
}
}
ADD FOLLOWING CODE IN FACE-CONFIG.XML
<validator-id>memberValidator</validator-id>
<validator-class>org.domain.pixel.validators.MemberValidator</validator-class>
</validator>