To do this you will need to use bindings into the backing bean which does
the validation. You might also want to take a look at the validateCompareTo
validator and see if this does what you need. 

-----Original Message-----
From: Ali Abdel-Aziz Ali [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 18, 2006 9:10 AM
To: MyFaces Discussion
Subject: Custom Validator problem.

Hello All,

I have Custom Validator problem.
I have two input calenders and I want to make some validation on their
values any validation.

so this the UI code:

<h:outputText value="From Date:" style="font-weight:bold"/> <t:inputCalendar
id="FromDate" monthYearRowClass="yearMonthHeader" weekRowClass="weekHeader" 
                            currentDayCellClass="currentDayCell"
value="#{positionPopup.fromDate}" renderAsPopup="true"
 
popupTodayString="#{example_messages['popup_today_string']}"
popupWeekString="#{example_messages['popup_week_string']}" 
                            renderPopupButtonAsImage="true" required="true"
styleClass="required">                                
            <f:validator validatorId=" validators.datesOverlapValidator"/>
</t:inputCalendar>

<h:outputText value="Thru Date:" style="font-weight:bold" />
<t:inputCalendar id="ThruDate" monthYearRowClass="yearMonthHeader"
weekRowClass="weekHeader" 
                            currentDayCellClass="currentDayCell"
value="#{positionPopup.thruDate}" renderAsPopup="true"
 
popupTodayString="#{example_messages['popup_today_string']}"
popupWeekString="#{example_messages['popup_week_string']}" 
                            renderPopupButtonAsImage="true"
required="false"> </t:inputCalendar>

and this is the validator code:

public void validate(FacesContext facesContext,    UIComponent uiComponent,
Object value)    throws ValidatorException 
{
        if (facesContext == null) throw new
NullPointerException("facesContext"); 
                if (uiComponent == null) throw new
NullPointerException("uiComponent");
                    if (value == null)
                    {
                        return;
                    } 

            UIComponent foreignComp =
uiComponent.getParent().findComponent(_for);        // _for =  "ThruDate" //
equall to the second inputCalender ID
            if(foreignComp==null)
                throw new FacesException("Unable to find component '" + _for
+ "' (calling findComponent on component '" + uiComponent.getId() + "')");
            if(false == foreignComp instanceof EditableValueHolder)
                throw new FacesException("Component '" + foreignComp.getId()
+ "' does not implement EditableValueHolder"); 
            EditableValueHolder foreignEditableValueHolder =
(EditableValueHolder) foreignComp;

            if (foreignEditableValueHolder.isRequired() &&
foreignEditableValueHolder.getValue()== null ) 
            {
                return;
            }

            Object[] args =
{value.toString(),(foreignEditableValueHolder.getValue()==null) ?
foreignComp.getId():foreignEditableValueHolder.getValue().toString()}; 

            _logger.info(" From Date =  "+ args[0].toString() +" Thrue Date
= "+foreignEditableValueHolder.getValue());
            
            
            boolean overlap = this.overlap ( new Range(new Date(1), new
Date(2)), new Range(new Date(1), new Date(2)));
            if(!overlap)
            { 
                //if(foreignEditableValueHolder.getValue()==null ||
!foreignEditableValueHolder.getValue().toString().equals( value.toString())
)
                
               FacesMessage message = new FacesMessage();
 
message.setDetail(Messages.getMessageString("RangeOverlapValidator")+" "+
args[0].toString() +" "+args[1].toString() + "."); 
 
message.setSummary(Messages.getMessageString("RangeOverlapValidator")+" "+
args[0].toString() +" "+args[1].toString() + ".");
               message.setSeverity (FacesMessage.SEVERITY_ERROR);
               throw new ValidatorException(message);
            }
    }

but it always return the ThruDate = null however it can feel with it's
properties like isRequired or not. 

Can Any buddy help?
--
Ali Abdel-Aziz
http://www.aliabdelaziz.com
http://aabdelaziz.blogspot.com 

Reply via email to