0

search button using validation

asked 2013-02-28 04:48:56 +0800

progamerdotcom gravatar image progamerdotcom
117 5

How to get textbox value if a form using validator ?

I want if search button was clicked, the value of textboxs (id & nama textbox) will binder on search button.

thank in advance.

This my code ::

mstStudent.zul

<zk>
<style src="../css/mystyle.css" />  
<div
    height="100%" apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('com.myapp.viewmodel.mst.MstStudentVM')" validationMessages="@id('vmsgs')">
    <separator />       
     <groupbox form="@id('fx') @load(vm.current)  @save(vm.search, before='search') @save(vm.current, before='save') @validator(vm.formValidator) ">
     <caption label="Master Student" sclass="mycaption" />
     <separator />
            <grid sclass="mygrid">
                <columns visible="false">
                    <column width="100px" />
                    <column width="200px" />
                    <column width="200px" />
                </columns>
                <rows>
                        <row>
                            <cell><label value="*Id" sclass="mylabel" /></cell>
                            <cell><textbox value="@bind(fx.id)" type="text" focus="true" sclass="mytext" /></cell>
                            <cell><label sclass="msgerror" value="@bind(vmsgs['fkey1'])"/></cell>
                        </row>
                        <row>
                            <cell><label value="Nama" sclass="mylabel" /></cell>
                            <cell><textbox value="@bind(fx.nama)" type="text" sclass="mytext"  /></cell>
                            <cell><label sclass="msgerror" value="@bind(vmsgs['fkey2'])"/></cell>
                        </row>

                        <row>
                            <cell colspan="3">
                                <space width="100px" />
                                <button label="Save" sclass="mybutton orange bigrounded" onClick="@command('save')" image="/image/iconcrud/btn_save.gif" />
                                <space width="5px"/>
                                 <button label="Reset" sclass="mybutton orange bigrounded" onClick="@command('reset')" image="/image/iconcrud/btn_reset.gif" />
                                <space width="5px"/>
                                <button label="Search" sclass="mybutton orange bigrounded" onClick="@command('search')" image="/image/iconcrud/btn_search.gif" />
                            </cell> 
                        </row>
                    </rows>
            </grid>
        </groupbox>

    <separator />
    <listbox sclass="mylist" mold="paging" pageSize="10" pagingPosition="bottom"
        selectedItem="@bind(vm.current)"
        model="@load(vm.listModel)">
        <listhead sizable="true">
            <listheader width="100px" label="Id" sort="auto(id)" />
            <listheader width="250px" label="Nama" sort="auto(nama)" />

        </listhead>
        <template name="model" var="p1">
            <listitem>
                <listcell label="@load(p1.id)" />
                <listcell label="@load(p1.nama)" />
            </listitem>
        </template>
    </listbox>
</div>

</zk>

and this my viewmodel (MstStudentVM.java)

package com.myapp.viewmodel.mst;

import java.util.ArrayList; import java.util.List;

import org.zkoss.bind.ValidationContext; import org.zkoss.bind.Validator; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.bind.validator.AbstractValidator; import org.zkoss.zul.ListModel; import org.zkoss.zul.ListModelList;

import com.myapp.bean.Student;

/* @author Pro Gamer Feb 27, 2013 11:13:41 AM */

public class MstStudentVM {

private Student current,search;

private ListModelList<Student> listModel;

@Init
public void init(){
    List<Student> list = new ArrayList<Student>();
    list.add(new Student("1", "Student 1"));
    list.add(new Student("2", "Student 2"));
    list.add(new Student("3", "Student 3"));
    list.add(new Student("4", "Student 4"));
    list.add(new Student("5", "Student 5"));
    list.add(new Student("6", "Student 6"));
    listModel = new ListModelList<Student>();
    listModel.addAll(list);
}

public Student getSearch() {
    if (search == null) search = new Student();
    return search;
}

public void setSearch(Student search) {
    this.search = search;
}

public Student getCurrent() {
    if (current == null) current = new Student();
    return current;
}

public void setCurrent(Student current) {
    this.current = current;
}

public ListModelList<Student> getListModel() {
    if (listModel == null){
        listModel = new ListModelList<Student>();
    }
    return listModel;
}

public void setListModel(ListModelList<Student> listModel) {
    this.listModel = listModel;
}

@NotifyChange("listModel")
@Command("save")
public void save(){
    System.out.println("obj current "+current);
    listModel.add(current);

}

@NotifyChange({"listModel","search"}) 
@Command("search")
public void search(){
    // stack here, how to get object from UI -> textbox Id and textbox Nama
    if (search != null){
        System.out.println("search id "+search.getId());
        System.out.println("nama id "+search.getNama());
    }else{
        System.out.println("search is null");
    }

}

@NotifyChange("current")
@Command("reset")
public void reset(){
    current = null;
}

public Validator getFormValidator(){
    return new AbstractValidator() {

        public void validate(ValidationContext ctx) {
            // TODO , masbro
            String id = (String) ctx.getProperties("id")[0].getValue();
            if (id == null || id.equalsIgnoreCase("")){
                addInvalidMessage(ctx, "fkey1", "id is empty bro");
            }
            String nama = (String) ctx.getProperties("nama")[0].getValue();
            if (nama == null || nama.equalsIgnoreCase("")){
                addInvalidMessage(ctx, "fkey2", "nama is empty bro");
            }
        }
    };
}

}

and this is Student.java (bean)

package com.myapp.bean;

import java.io.Serializable;

/* @author Pro Gamer Feb 27, 2013 9:45:21 AM */

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private String id;

private String nama;

public Student() {
}

public Student(String id) {
    this.id = id;
}

public Student(String id, String nama) {
    this.id = id;
    this.nama = nama;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getNama() {
    return nama;
}

public void setNama(String nama) {
    this.nama = nama;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

@Override
public String toString() {
    return "Student [id=" + id + "]";
}

}

delete flag offensive retag edit

7 Answers

Sort by ยป oldest newest most voted
1

answered 2013-03-02 13:15:35 +0800

rickcr gravatar image rickcr
704 7

updated 2013-03-02 13:16:44 +0800

Ok, I found something that works, but there might be (and should be) a better way to do this. If a dev reads this, I'd like to see a way to bind the formValidator to particular submit methods (save, search) if it's not already possible?

In the meantime, you can do this (just tested this with some code)...

Change your groupbox definition back to:

 <groupbox form="@id('fx') @load(vm.current)  @save(vm.current, before={'save','search'}) @validator(vm.formValidator)">

Then you can lookup the command fired in your validation method and ignore it if it's the search command:

public void validate(ValidationContext ctx) {
    if (ctx.getCommand().equals("search")) {
        logger.debug("search, so I'm ignoring"); 
    } else {
        logger.debug("not search so validate...");
        String id = (String) ctx.getProperties("id")[0].getValue();
        if (id == null || id.equalsIgnoreCase("")){
             addInvalidMessage(ctx, "fkey1", "id is empty bro");
        }
         //.... 
    }
link publish delete flag offensive edit

Comments

Note, I entered a feature request related to this item here http://tracker.zkoss.org/browse/ZK-1651

rickcr ( 2013-03-02 13:50:03 +0800 )edit

yapz thank, your comment save me :D

progamerdotcom ( 2013-03-04 04:15:16 +0800 )edit
0

answered 2013-03-01 03:34:01 +0800

rickcr gravatar image rickcr
704 7

Are you sure you aren't seeing "validate(ValidationContext ctx) " being called? Can you debug in there to be sure it's not being called?

Or are you asking about your comment in "search" // stack here, how to get object from UI -> textbox Id and textbox Nama ???

If it's the latter, that's not a validation question. I think the issue is you seem to binding two different instances of Student "current" and "search." I think you probably just want to use "current' for both of them and I "think" you don't define two saves, I think you use a comma as part of an array (but someone will have to correct me) so instead of...

form="@id('fx') @load(vm.current)  @save(vm.search, before='search') @save(vm.current, before='save')

try..

form="@id('fx') @load(vm.current)  @save(vm.current, before={'search', 'save'})

Then in your VM use ..

if (search != null){
    System.out.println("search id "+current.getId());

I haven't tested the above, but I'm pretty sure using the search instance won't work since its not bound to the form (only "current" is bound to the form with @load(vm.current))

link publish delete flag offensive edit

Comments

>Are you sure you aren't seeing "validate(ValidationContext ctx) " being called? I seeing validation when I press search, necessary the validaion on save button not on search button.

progamerdotcom ( 2013-03-01 08:37:47 +0800 )edit

>Or are you asking about your comment in "search" yes I cannot get current obejct on search() method, I want to printout character that filled in textbox when search button was pressed

progamerdotcom ( 2013-03-01 08:38:38 +0800 )edit
0

answered 2013-03-01 07:09:15 +0800

progamerdotcom gravatar image progamerdotcom
117 5

Thank rickcr.

this is my groupbox now,

<groupbox form="@id('fx') @load(vm.current)  @save(vm.current, before={'search','save'}) @validator(vm.formValidator) ">

and in search button method as below:

@NotifyChange({"listModel","current"}) 
@Command("search")
public void search(){

    if (current != null){
        System.out.println("current id "+current.getId());
        System.out.println("current nama "+current.getNama());
    }else{
        System.out.println("current is null");
    }

}

this is screenshot ::

screenshot

on that screenshot, search() method doesn't to do because validator is called the first before search() method,,

my aim is get the current object ( Student object) to filter the listModel. how to do it ?

thanks alot.

link publish delete flag offensive edit
0

answered 2013-03-01 14:26:36 +0800

rickcr gravatar image rickcr
704 7

I think the issue is you really don't want the validator called if doing a search? (Currently what you showed in the screen shot makes sense... validation fails so search is not called.) See if this turns off the validation upon search?

 <groupbox form="@id('fx') @load(vm.current)  @save(vm.current, before='save') @validator(vm.formValidator) ">
link publish delete flag offensive edit
0

answered 2013-03-02 06:21:34 +0800

progamerdotcom gravatar image progamerdotcom
117 5

updated 2013-03-02 06:28:15 +0800

yes, you are right, I dont want the validator called in search() method, but the validation must be called when save() method.

the result if I change my groupbox form became this :

<groupbox form="@id('fx') @load(vm.current)  @save(vm.current, before='save') @validator(vm.formValidator) ">

and eclipse log shows :

--- begin log eclipse --

current id null

current nama null

--- end log eclipse --

its means the current object is null when search button was pressed. how to binding textbox ( id textbox and nama textbox ) to 'current' object on search button click ( search() method ).

thanks.

link publish delete flag offensive edit
0

answered 2013-03-02 07:09:09 +0800

progamerdotcom gravatar image progamerdotcom
117 5

this my screenshot

link publish delete flag offensive edit
0

answered 2013-03-04 04:13:47 +0800

progamerdotcom gravatar image progamerdotcom
117 5

OK its works :)

<groupbox form="@id('fx') @load(vm.current)  @save(vm.current, before={'save','search'}) @validator(vm.formValidator)">

public void validate(ValidationContext ctx) {
if (ctx.getCommand().equals("search")) {
    logger.debug("search, so I'm ignoring"); 
} else {
    logger.debug("not search so validate...");
    String id = (String) ctx.getProperties("id")[0].getValue();
    if (id == null || id.equalsIgnoreCase("")){
         addInvalidMessage(ctx, "fkey1", "id is empty bro");
    }
     //.... 
}

thank very much rickcr,, u 'r the best :)

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-02-28 04:48:56 +0800

Seen: 104 times

Last updated: Mar 04 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More