I've got a little problem with my profile edition form in my webapp. I use 3 selectOneMenu's (day,month,year) to modify birthday. I use ajax listener to render day values when new month has different days range. When I get into profile edition, user's birthday is loaded from DB and selectOneMenu's are filled. If I change only day - edition completes without any errors. Whenever I change month, day is changed to null and I don't know why. Here's the .xhtml code fragment:
<h2>
#{messages.birthday}:
<h:selectOneMenu id="daySelect" value="#{profileEdition.dayOfBirth}" >
<f:selectItems value="#{profileEdition.dateValues.daysList}" var="day" itemLabel="#{day.valueLabel}" itemValue="#{day.valueValue}"/>
<f:ajax listener="#{profileEdition.dayChanged}" />
</h:selectOneMenu>
<h:selectOneMenu value="#{profileEdition.monthOfBirth}">
<f:selectItems value="#{profileEdition.dateValues.monthsList}" var="month" itemLabel="#{month.valueLabel}" itemValue="#{month.valueValue}"/>
<f:ajax listener="#{profileEdition.monthChanged}" render="daySelect" />
</h:selectOneMenu>
<h:selectOneMenu value="#{profileEdition.yearOfBirth}" >
<f:selectItems value="#{profileEdition.dateValues.yearsList}" var="year" itemLabel="#{year.valueLabel}" itemValue="#{year.valueValue}"/>
<f:ajax listener="#{profileEdition.yearChanged}" render="daySelect" />
</h:selectOneMenu>
</h2>
In my @ManagedBean I got these listeners and they are changing daysList in case if there is such a need and change dayOfBirth value to null when day is out of new month's days range. I've inserted some System.out.println(); inside these listeners to check if THEY are changing dayOfBirth value, but they are not. What is strange - I already used this mechanism in my registration form and it works fine.
What's the problem?
DayChanged listener:
public void dayChanged(){
System.out.println("DAY HAS CHANGED! \n NEW DATE IS: " + dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
MonthChanged listener:
public void monthChanged(){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String day = bundle.getString("day");
String month = bundle.getString("month");
if(monthOfBirth.equals(month)){
monthOfBirth = null;
} else {
int m = Integer.parseInt(monthOfBirth);
if(m==1 || m==3 || m==5 || m==7 || m==10 || m==12) {
dateValues.daysList = new DateValues.Values[32];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<32;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
}else if(m==2) {
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}else {
dateValues.daysList = new DateValues.Values[31];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<31;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}
}
System.out.println("MONTH HAS CHANGED! \n NEW DATE IS: "
+ dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
YearChanged listener:
public void yearChanged(){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String day = bundle.getString("day");
String year = bundle.getString("year");
if(yearOfBirth.equals(year)){
yearOfBirth = null;
}
if(monthOfBirth !=null && yearOfBirth != null){
int y = Integer.parseInt(yearOfBirth);
int m = Integer.parseInt(monthOfBirth);
if(y%400==0 && m==2){
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
} else if(y%100==0 && m==2){
dateValues.daysList = new DateValues.Values[29];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<29;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
} else if(y%4==0 && m==2){
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
} else if(m==2) {
dateValues.daysList = new DateValues.Values[29];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<29;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}
}
System.out.println("YEAR HAS CHANGED! \n NEW DATE IS: "
+ dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
All these if/else statements are used to determine leap year and proper days range for specific month. What I've already noticed thanks to System.out is that dayOfBirth is changed to null but just after I submit the form.
Just in case I'm putting here my method fired by submiting the form.
public String editProfile() throws ParseException{
System.out.println("Edition! You have chosen:");
System.out.println("Day: " + dayOfBirth);
System.out.println("Month: " + monthOfBirth);
System.out.println("Year: " + yearOfBirth);
birthday = dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth;
System.out.println("NEW BIRTHDAY IS: " + birthday);
if(DateValidator.isValid(birthday) == false){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String text = bundle.getString("invalidDate");
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, text, text);
FacesContext.getCurrentInstance().addMessage("", msg);
return null;
}
Date convertedBirthday = sdf.parse(birthday);
myAccount.setUrodziny(convertedBirthday);
mokEndpoint.editAccount(myAccount);
return "editionSuccess";
}
@ManagedBean is @ViewScoped