Reputation: 5644
I Want to save the value of an inplace if it was changed.
The methode employeeController.save() is called when I click on the save button. But how can I pass along the new and old value? I want to do this so I can know if the value was changed without asking the database.
<h:panelGrid id="display" columns="2" cellpadding="4"
style="width:300px;"
styleClass="ui-widget-content"
columnClasses="label, value">
<h:outputText value="ID:" />
<h:outputText id="ID" value="#{emp.id}" />
<h:outputText value="Voornaam:"/>
<p:inplace id="firstnam" editor="true">
<p:ajax event="save" onsuccess="#{employeeController.saveName()}">
</p:ajax>
<p:inputText id ="firstName" value="#{emp.firstName}"
required="true" label="text"/>
</p:inplace>
Upvotes: 1
Views: 10844
Reputation: 22867
General solution: store the original data object, provide the copy from modifications.
You will there be always able to say what fields have changed since last save. In the case of value change listeners, you'll know only if the value has changed since last submit, which not always is the same.
Remembering the old values is a good practise, because you can easily revert the changes made by user in UI, check if update to database is really necessary etc.
Upvotes: 3
Reputation: 741
In mi case I can not use event="save", so I use p:event event="valueChange". In the managedBean I have properties that it will be modified and the object owner this properties. I see many people use event="save" but in my case this event throw render view error, for this reason I use valueChange
code view
<p:column>
<p:inplace id="inplaceNombre" emptyLabel="#{registro.usuario.nombre}" editor="true">
<p:inputText id="nombre" required="true"
requiredMessage="#{msg['editar.nombre.required']}"
alt="#{msg['editar.alt.nombre']}" title="#{msg['editar.title.nombre']}"
tabindex="1" value="#{registro.nombre}"
styleClass="#{component.valid ? '' : 'invalid'}" maxlength="30">
<f:validateBean for="nombre" />
<p:ajax event="valueChange" update="@this messageNombre" />
</p:inputText>
</p:inplace>
</p:column>
<p:row>
<p:column>
<p:commandButton value="#{msg['editar.value.enviar']}"
title="#{msg['editar.title.enviar']}" alt="#{msg['editar.alt.enviar']}"
actionListener="#{registro.actualizarUsuario}" tabindex="7" />
</p:column>
</p:row>
managedBean code
//property that can be modified in the User
@Size(min=5 ,max=30 ,message="Este campo no es correcto: tamaño máximo [30]")
@Pattern(regexp="^[a-zA-Z]+[a-zA-Z ]+",message="Este campo no es correcto")
private String nombre;
//User get by database with f:event type=preRenderView in xhtml view
private Usuario usuario;
Then, I check the changes and update user.
Kind Regards.
Upvotes: 1
Reputation: 1109625
There the valueChangeListener
is for.
E.g.
<p:inputText ... valueChangeListener="#{employeeController.firstNameChanged}" />
with
public void firstNameChanged(ValueChangeEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
// ...
}
You could set a boolean
there or collect the values in some other property and handle it further in the real command action method.
The method will only be invoked when the value is really changed. Even if it's just from null
to empty string. It will not be invoked when the old value equals()
the new value.
Upvotes: 7