0

I´m using Primefaces, I have an input field whose value must be generated in relation to an input date, the problem is that the date can be selected by clicking on the calendar or typing, if done in the first way everything works correctly but if done by writing, the value of UIInput in the backing bean is null. I dont know why the binding attribute is not working right or what i´m doing wrong.

<p:calendar id="fechaContable"   
                    value="#{movimientoBean.movimientoCab.fechaContable}" pattern="dd/MM/yyyy"  binding="#{movimientoBean.fecha}" required="true"   
                    requiredMessage="Se necesita fechaContable">  
            <p:ajax event="dateSelect" oncomplete="document.getElementById('anadirForm:numeroAsiento').focus()"/>

</p:calendar>

<p:inputText id="numeroAsiento"   
                      binding="#{movimientoBean.numeroAsiento}" value="#{movimientoBean.movimientoCab.numeroAsiento}" required="false">
        <f:ajax event="focus" render=":anadirForm:numeroAsiento" listener="#{movimientoBean.numAsiento}"/>
</p:inputText>

The backing bean

@ManagedBean
@ViewScoped
public class MovimientoBean implements Serializable{

......

private transient UIInput numeroAsiento;
private transient UIInput fecha;

......

public void numAsiento(){

.....

try {

        Date date = dateFormat.parse(fecha.getValue().toString());
        fechaContable = sdf.format(date);
    }catch (ParseException e){

    }
}

When dateSelect is invoked, the value of UIInput fecha is right, but when the date is written it is null.

Sorry for possible grammar or spelling mistakes

EDIT(Solution):

Here is the code working in case someone needs it, thanks to Laabidi Raissi for his help and the information:

<p:calendar id="fechaContable"   
                    value="#{movimientoBean.movimientoCab.fechaContable}" pattern="dd/MM/yyyy" required="true"   
                    requiredMessage="Se necesita fechaContable">  

            <p:ajax event="dateSelect" update=":anadirForm:numeroAsiento" listener="#{movimientoBean.numAsiento}" oncomplete="document.getElementById('anadirForm:numeroAsiento').focus()"/>
            <p:ajax event="change" update=":anadirForm:numeroAsiento" listener="#{movimientoBean.numAsiento}" oncomplete="document.getElementById('anadirForm:numeroAsiento').focus()"/>
</p:calendar>


<p:inputText id="numeroAsiento"   
                       value="#{movimientoBean.movimientoCab.numeroAsiento}" required="false">

</p:inputText>
Uyak
  • 27
  • 8

1 Answers1

0

You should add an ajax event for change:

<p:ajax event="change" oncomplete="document.getElementById('anadirForm:numeroAsiento').focus()"/>

Unrelated to the question, I think mixing p:ajax and f:ajax is not a very idea.
And also, you shouldn't use UIComponents in managed beans unless you have a good reason.
Refer to this answer by BalusC for more explanations

Community
  • 1
  • 1
Laabidi Raissi
  • 3,263
  • 1
  • 22
  • 28
  • Using event="change" i still don´t get the value in bean, only if it is selected by clicking on the calendar but not by typing. – Uyak May 29 '14 at 11:40
  • just a question, it does not work when you click outside (or click tab) the input ? – Laabidi Raissi May 29 '14 at 11:42
  • If i use ` – Uyak May 29 '14 at 11:43
  • for the calendar binding, you can just create a Date attribute in your managed bean, and have the value of `p:calendar` pointing to it – Laabidi Raissi May 29 '14 at 11:44
  • Ajax is being executed but the value of fecha is null when it is typed. – Uyak May 29 '14 at 11:44
  • can you remove the binding part, and work with `movimientoBean.movimientoCab.fechaContable` property ? – Laabidi Raissi May 29 '14 at 11:48
  • Ok i have removed all binding part and is pointing to movimientoBean.movimientoCab.fechaContable, now it´s nearly working, i can get the value when it's typed, but not the first time numeroAsiento is focused, needed to go back and click again on fechaContable, the second time you do it, the value is changed on the bean. Sorry if i don't explain myself right. – Uyak May 29 '14 at 11:59
  • Thanks for all your help, finally it has worked and it wasn`t neccesary to bind. – Uyak May 29 '14 at 12:51
  • You are welcome, sorry for being late, I left yesterday for some work – Laabidi Raissi May 30 '14 at 08:41