Hey all,
I'm looking for guidance on a "nice" way of doing something.
I have a page with two component on it. One is a "Select country"
drop-down and one is a "phone-number" input text. The latter has a small
text prefix that shows the international dialling code for the selected
country. Both fields are required.
The Country drop-down has onChange()="submit();" and a
ValueChangeListener method on the ViewController. When a new country is
chosen:
The form is submitted.
The ValueChangeListener looks up the international dialling code for the
chosen country and sets a value on the bean
The ValueChangeListener calls renderResponse().
The view is rendered with the dialling code displayed just before the
phone-number field.
The problem lies here. If having selected a country I now submit the
form without entering a phone-number the form validation fails because
the phone-number feild is required. That means (JSF lifecycle) that the
bean values are not initialised from the request. That means that my
dialling-code bean property is now null again and so disappears when the
view is re-rendered with the error.
To fix it, in prerender() I check for (diallingCode == null &&
isPostBack()), assume it's an error condition and look up the value of
the dialling code from the requestParameterMap like this:
public void prerender() {
String isoCountryCode =
getAddresses()[0].getCountry().getIsoCountryCode();
// If the isocode is null and this is a postback it indicates
// some validation error. Get isocode from the request
if ( isoCountryCode == null && isPostBack() ) {
Map requestParameterMap =
getFacesContext().getExternalContext().getRequestParameterMap();
Set set = requestParameterMap.keySet();
for (Object key : set) {
if ( key.toString().endsWith("country") )
isoCountryCode = (String)
requestParameterMap.get(key);
}
}
initDiallingCode(isoCountryCode);
}
That fixes my bug. But am I looking up the value in the best way? How
would others fix this problem?
Cheers,
Ian.