So you’re using ZK Framework to build your online forms, and your forms have fields which needs validations. You have a “Submit” button which your user will click when they’re done filling out the form. You’re using the MVVM paradigm with the data binding. You tried adding constraints to the fields, but you want the validations to happen only when your users click the “Submit” button. We have discovered this little trick that might help you.
Most likely, you are using the @bind(…) method to bind your form fields to the View Model attribute, e.g:
<textbox value="@bind(vm.someAttribute)" constraint="no empty" .../>
We noticed that when we used @bind() in conjunction with a constraint on the field, the validation was triggered immediately, for example, when the cursor left the corresponding input field. Depending on your requirement, this might be acceptable, but in one of the project that we have with our client, we went the extra mile for our client and opted for a cleaner solution, which is to trigger the validation only when the user clicks the “Submit” button.
The answer is to break the @bind() into @load() and @save(), and make sure the @save() is called before the command which handles the “Submit” button, i.e:
<textbox value="@load(vm.someAttribute) @save(vm.someAttribute, before='save')" constraint="no empty" .../> <button label="Submit" onClick="@command('save')"/>
Note that the @save() expression has before=’save’, which matches the name of the ‘save’ @command() handler. This way, the validation is invoked just before the ‘save’ command, and the invalid input pop message is only shown at that time.
That’s it for now. We hope this tip helps someone out there with the same situation.
A little bit about us, NextCoder is a team of data analysts, software architects, and developers. Using our up-to-date skills and experience, we help businesses make profitable decisions while increasing their capacity to grow. We combine Business Intelligence methodology with our vast software development experience to provide effective and unique solutions for our clients. Some of our products includes: