Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-2636

Form validator didn't get value to validate selectedItem attribute if adding new item to Combobox's ListModel

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: 7.0.4
    • Fix Version/s: 7.0.5
    • Component/s: Databind 2
    • Security Level: Jimmy
    • Labels:

      Description

      The selectedItem with form binding didn't work as expected.
      Reproducible steps:
      1. Select any item from the drop-down list.
      2. Click "Save" button.
      3. Should not see "null" in the console.

      Repeat step1 and step2 several times, the value is the same with the item you selected (correct) and sometimes the value is null (wrong).

      ZUL page

      <div apply="org.zkoss.bind.BindComposer"
      	viewModel="@id('vm') @init('org.zkoss.support.imperatives.FormBeanValidatorVM')"
      	validationMessages="@id('vmsgs')"
      	form="@id('fx') @load(vm.bean) @save(vm.bean, before='save') @validator(vm.beanValidator, prefix='p_')">
      	<combobox id="cbx" model="@load(vm.letters)" selectedItem="@bind(fx.letter)"
      		errorMessage="@load(vmsgs['p_letter'])"
      		onChange="@command('onChangeLetter')">
      		<template name="model">
      			<comboitem label="${each}" />
      		</template>
      	</combobox>
      	<button onClick="@command('save')">Save</button>
      </div>
      

      ViewModel Class

      public class FormBeanValidatorVM {
      	ListModelList<String> letters = new ListModelList<String>();
      	Bean bean = new Bean();
      	Validator validator;
      
      	@Init
      	public void init() {
      		letters.addAll(Arrays.asList(new String[] { "a", "b", "c", "d", "e", "f", "g" }));
      	}
      	
      	@Command
      	public void onChangeLetter() {
      		letters.add("h");
      	}
      	
      	@Command
      	public void save() {
      	}
      	
      	public ListModelList<String> getLetters() {
      		return letters;
      	}
      	
      	public Bean getBean() {
      		return bean;
      	}
      	
      	public Validator getBeanValidator() {
      		if (validator == null) {
      			//validator = new CascadingFormBeanValidatorService();
      			validator = new Validator() {
      				public void validate(ValidationContext ctx) {
      					String value = (String) ctx.getProperties("letter")[0].getValue();
      					System.out.println("Validate value: " + value);
      				}
      			};
      		}
      		return validator;
      	}
      
      	public class Bean {
      
      		@NotBlank
      		private String letter;
      
      		public String getLetter() {
      			return letter;
      		}
      
      		public void setLetter(String letter) {
      			this.letter = letter;
      		}
      
      	}
      }
      

        Attachments

          Activity

            People

            Assignee:
            jumperchen jumperchen
            Reporter:
            vincentjian vincentjian
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: