0

I spent some days to try to validate part form with a OneSelect like as "Terms and Conditions", for example If are pre-filled Name and Telephone and I selected "Terms and Conditions" in this moment validate the previous fields are filled and enable "Next" button.

I tried using two ways, the first I put p:attribute inside p:selectOneRadio and received in method validateSelection() always null, the second is used validateSelection(String name, String telephone), but both methods show null values,

Does anyone know why this happens?

Thanks a lot.

(this is my view)

<h:form id="frmUser" enctype="multipart/form-data"> 


  <p:outputLabel id="lblName" for="txtName" value="#{msg.lbl_name}" styleClass="lbl8"/>     
  <p:inputText id="txtName" maxlength="7" required="true"  value="#{mainBean.userDTO.name}"/>
  <p:message for="txtName" display="text"/>


  <p:outputLabel id="lblTel" for="lblTel" value="#{msg.lbl_tele}" />    
  <p:inputText id="lblTel" maxlength="7" required="true"  value="#{mainBean.userDTO.telephone}"/>
  <p:message for="lblTel" display="text"/>

  <p:outputLabel for="console" value="Accept Terms and Conditions" />
    <p:selectOneRadio id="console"  unselectable="true" value="#{mainBean.selectTerms}">
    <f:selectItem itemLabel="Yes" itemValue="true" />
    <f:selectItem itemLabel="No" itemValue="false" />
    <f:attribute name="setname" value="#{mainBean.userDTO.name}" />     
    <p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection}" />  

      <!--WAY TWO -- >
<!--
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection(mainBean.userDTO.name,mainBean.userDTO.telephone)}" /> 
-->    
        </p:selectOneRadio>



</h:form>

This is my Bean

@Named("mainBean")
@SessionScoped
public class mainBean implements Serializable{

private  UserDTO userDTO;



  public String init() {
    userDTO = new UserDTO();
   //call url to display
  }

     /*
     * Getters y Setters
     * */   

    public UserDTO getUserDTO() {
        return userDTO;
    }
    public void setUserDTO(UserDTO userDTO) {
        this.userDTO = userDTO;
    }

   public void validateSelection(AjaxBehaviorEvent event) {

        System.out.println("fill name"+ userDTO.getName());


        System.out.println("fill name in ajax event :"+event.getComponent().getAttributes().get("setname"));

        UIComponent component = event.getComponent();

        System.out.println("fill name in ajax event component:"+
          component.getAttributes().get("setname"));

      //If two values are filled, eneabled NEXT BUTTON

    }
    public void validateSelection(String name, String telephone){

     System.out.println("fill name"+name);
      System.out.println("fill telephone"+telephone);
//If two values are filled, eneabled NEXT BUTTON
    }




}

And this is the UserDTO class

public class UserDTO implements Serializable{

    private String name;
    private String telephone;


    public UserDTO(){}


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

}
Palio
  • 11
  • 1
  • 2
  • 1
    You aren't processing these fields (name and telephone), since your listener submit to server only selectTerms value. Since you have to process all data in your form, you can try adding process="@form" and update=":frmUser" (for all messages) to p:ajax. And if you need to see all value uppercase you can avoid js and use toUpperCase server side, and text-transform: uppercase, as css style. Last thing, to see why f:attribute isn't working, look at [this](https://www.tutorialspoint.com/jsf/jsf_attribute_tag.htm) for more info – WoAiNii Apr 09 '20 at 07:40
  • Does it work if you remove all panelgrid's? No, please remove them. make the code more clean for a [mcve]... (also check the enctype...) in addition to correct analysis by @MatteoZanini, read https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes but https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value is a duplicate of your problem, #9 there – Kukeltje Apr 09 '20 at 08:39
  • Hi, guys, thanks for the advices, I added process="@form" and update=":frmUser" in p:ajax works perfectly – Palio Apr 09 '20 at 21:55

0 Answers0