-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello, I try to create simple User Registration using Zkoss, and implement MVVM Concept on it.
When i try to emptying some fields, Zkoss validate it but the Submit value still passed to ViewModel.
And i get null
value.
How to prevent submitted value pass to ViewModel when found invalid data?
Here is my index.zul
<zk>
<window border="normal" width="400px" title="Create New Account"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('com.fanjavaid.mvvm.viewmodel.UserViewModel')">
<grid hflex="1">
<columns visible="false">
<column />
<column />
</columns>
<auxhead>
<auxheader colspan="2" label="Registration Form (MVVM)" style="font-size:16px" image="/images/vcard_add.png" />
</auxhead>
<rows>
<row>
User Name <textbox id="username" hflex="1" constraint="No Empty" value="@bind(vm.username)" />
</row>
<row>
Gender
<radiogroup id="genderRadio" selectedItem="@bind(vm.gender) @load(vm.gender)">
<radio label="Male" value="male" image="/images/user.png" />
<radio label="Female" value="female" image="/images/user_female.png" />
</radiogroup>
</row>
<row>
Birthday <datebox id="birthdayBox" hflex="1" value="@bind(vm.birthday)" constraint="no empty, no today, no future"/>
</row>
<row spans="2" align="center">
<hlayout>
<checkbox id="acceptTermCheckbox" checked="@bind(vm.agree)" /> Accept Term of Use
</hlayout>
</row>
<row spans="2" align="right">
<hlayout>
<button id="resetButton" label="Reset" />
<button id="submitButton" label="Submit" disabled="@load(empty vm.username) @load(empty vm.birthday) @load(!vm.agree) @save(!vm.agree)" onClick="@command('save')" />
</hlayout>
</row>
</rows>
</grid>
</window>
</zk>
And here is my ViewModel :
public class UserViewModel {
private String username;
private String gender;
private Date birthday;
private List<User> userList = new ArrayList<User>();
private boolean agree;
public UserViewModel() {
this.gender = "male";
}
@Command
@NotifyChange("userList")
public void save() {
userList.add(new User(username, gender, birthday));
System.out.println("Updated User List : ");
for (User user : userList) {
System.out.println(user.toString());
}
System.out.println("---");
}
...
Thank you...
Just use the middle object provided by ZK.
You can read it in this link :
http://books.zkoss.org/zk-mvvm-book/8.0/databinding/formbinding.html
Greetz chill.
Asked: 2015-08-18 07:10:42 +0800
Seen: 31 times
Last updated: Aug 19 '15