> 2) Which is the right method to get the values of other components in
> the validator method, getSubmittedValue(), getLocalValue(), dependent
> on configuration issues (immediate) of the component owning the
> validator and other components asked in the validator method?
Would the following work for you?
(Taken directly from "Core Java Server Faces" book)
The trick is to attach the validator to the last of the components. By
the time its
validator is called, the preceding components passed validation and had
their
local values set. The last component has passed conversion, and the
converted
value is passed as the Object parameter of the validation method.
Of course, you need to have access to the other components. You can
easily
achieve that access by using a backing bean that contains all components
of the
current form. Simply attach the validation method to the backing bean:
public class BackingBean {
private UIInput dayInput;
private UIInput monthInput;
...
public void validateDate(FacesContext context, UIComponent
component,
Object value) {
int d = ((Integer) dayInput.getLocalValue()).intValue();
int m = ((Integer) monthInput.getLocalValue()).intValue();
int y = ((Integer) value).intValue();
if (!isValidDate(d, m, y)) {
FacesMessage message = ...;
throw new ValidatorException(message);
}
}
...
}
Note that the value lookup is a bit asymmetric. The last component does
not
yet have the local value set since it has not passed validation.
An alternative approach is to attach the validator to a hidden input
field that
comes after all other fields on the form.
<h:inputHidden id="datecheck" validator="#{bb.validateDate}"
value="needed"/>
The hidden field is rendered as a hidden HTML input field. When the
field
value is posted back, the validator kicks in. (It is essential that you
supply some
field value. Otherwise, the component value is never updated.) With this
approach, the validation function is more symmetrical since all other
form
components already have their local values set.
* ** *** ** * ** *** ** * ** *** ** *
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed.
Any views or opinions presented are solely those of the author, and do not
necessarily
represent those of ESB.
If you have received this email in error please notify the sender.
Although ESB scans e-mail and attachments for viruses, it does not guarantee
that either are virus-free and accepts no liability for any damage sustained
as a result of viruses.
* ** *** ** * ** *** ** * ** *** ** *