I'm thinking that I had also problems when I used an form in a template of list or grid with multiple items. can you try to put the code outside the list for testing?
chillworld ( 2014-10-16 21:05:41 +0800 )edit-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a grid with records(name, description, editButton), for each record I have an edit-form inside a master-detail, when I press the edit button master-details opens with populated edit-form, !!! after I change the values in form and hit Save button the new values are not saved!!!
Seems that someone override the new value with the old value. Who is "someone"?
Can anybody help me? my code is listed below, please read the comments issue is there
zul:
<grid model="@bind(vm.recordsList)">
<columns>
<column label="Name" sort="auto(name)" sortAscending="${nameAsc}" sortDescending="${nameDesc}"/>
<column label="Description" sort="auto(description)" sortAscending="${descriptionAsc}" sortDescending="${descriptionDesc}"/>
</columns>
<template name="model">
<row>
<detail open="@load(each.editFormVisible)">
<div form="@id('fx') @load(vm.selectedRecord) @save(vm.selectedRecord, before='save') @validator(vm.formValidator)">
<grid>
<rows>
<row>
<textbox value="@bind(fx.name)" />
<label value="@bind(vmsgs['editFormName'])" style="color :red;" />
<textbox value="@bind(fx.description)" />
<label value="@bind(vmsgs['editFormDescription'])" style="color :red;" />
<button label="Save" onClick="@command('save')"/>
</row>
</rows>
</grid>
</div>
</detail>
<label value="${each.name}" />
<label value="${each.description}" />
<button label="Edit" onClick="@command('edit')" />
</row>
</template>
</grid>
Validator:
public Validator getFormValidator(){
return new AbstractValidator() {
@Override
public void validate(ValidationContext ctx) {
String name = (String) ctx.getProperties("name")[0].getValue();
//ctx.getProperties("name")[0] - sometimes here comes old value instead of new;
//for example in this returned list: ctx.getProperties("name"), we can have 4 items
//and the new value is on the 2nd or 3rd position(unpredictable) not on the [0]-1st as expected
String description = (String) ctx.getProperties("description")[0].getValue();
if(name == null || name.isEmpty()){
addInvalidMessage(ctx, "editFormUnitMeasure", "required");
}
if(description == null || description.isEmpty()){
addInvalidMessage(ctx, "editFormDescription", "required");
} }
}}}
ViewModel:
@Command
@NotifyChange({"selectedRecord", "recordsList"})
public void edit(@BindingParam("record") Record record){
record.setEditFormVisible(true);
selectedRecord.copy(record);
}
@Command
@NotifyChange({"selectedRecord", "recordsList"})
public void save(){
if(selectedRecord.getName() != null && !selectedRecord.getName().isEmpty()){
//here we need to get the new value from form, but we get the old value for name
}}
Hi
Assume you have 10 data in the grid, if you click edit button on each row to open detail, you will see the selectedRecord is the changed in each detail data. Which means there are 10 middle object "fx" bind to the same selectedRecord.
Also, the validation will trigger 10 times because they are all belong to the same view model.
You should separate the view model for each form binding.
I created a zkfiddle sample here: http://zkfiddle.org/sample/34vljts/3-master-detail-form-binding
In stead of using a validator,
Can't you just do :
<textbox value="@bind(fx.description)" constraint="no empty"/>
Everything remains the same, just the validator you can remove inclusif the labels for showing the error message.
Greetz chill.
Hi chillworld,
regarding to issue: "MVVM and Validator (or Constraint)" from zkoss forum, I opted for server validator. But anyway I'm curious to find where is the problem in the case listed above by me. May be here is a bug in ZK or ZK EE works improperly without an appropriate license?
I'm thinking that I had also problems when I used an form in a template of list or grid with multiple items. can you try to put the code outside the list for testing?
chillworld ( 2014-10-16 21:05:41 +0800 )editthanks for that sample :)
Asked: 2014-10-13 18:16:52 +0800
Seen: 51 times
Last updated: Oct 24 '14
Please help me in form binding
MVVM Validator: class not found ? [closed]
ZK drang and drop and databinding [closed]
How to get the absolute path of attached file
Id attribute set in zul code doesn't reflect in html code? [closed]
Conditional evaluation component in mvvm
passing present viewmodel to included page. [closed]