0

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

sabadow
  • 5,095
  • 3
  • 34
  • 51
gadzix90
  • 744
  • 2
  • 13
  • 28
  • What the scope of your `@ManagedBean`? Be sure you´re not using `@RequestScope` and check the bean is not being rebuilt putting a breakpoint in its constructor. – Aritz Jan 14 '13 at 11:36
  • Show the listener methods. The problem is in there. – BalusC Jan 14 '13 at 12:07
  • @BalusC I did what you asked. – gadzix90 Jan 14 '13 at 13:21
  • @AjMeen there's a lot of waterfall code in there, makes for difficult reading. Can you slim it down to just about 2-3 conditions and try again (and post here)? – kolossus Jan 14 '13 at 23:24
  • @AjMeen , just in case the `editProfile` method called from some `action` attribute of a `commandButton` : be aware , cause it will create a new instance of your `ViewScoped` bean , so instead make it a `public void editProfile()...` – Daniel Jan 15 '13 at 09:01
  • @Daniel , could you explain wider what do you mean? – gadzix90 Jan 15 '13 at 18:06
  • @AjMeen , I mean, try to change `editProfile` into `public void editProfile(` (I haven't read the entire Q' , so its a guess...) – Daniel Jan 15 '13 at 18:20

0 Answers0