You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ray Weidner <ra...@gmail.com> on 2010/07/02 07:27:25 UTC

Re: extending AbstractFormValidator as a validation adapter

Thanks for your reply, Jeremy.  I just got around to reading it.  I had been
under the impression that I'd have to call something from the #onSubmit to
trigger the feedback, but I tried it out based on your suggestion, and it
fits the bill.

This "deferred validation" is actually pretty useful in a number of
situations, so thanks.  It would be nice if Wicket had a built-in way of
turning off validation for certain buttons without turning off the
form-to-model update, i.e. not #setDefaultFormProcessing, which precludes
the latter.


On Wed, Jun 30, 2010 at 12:08 AM, Jeremy Thomerson <
jeremy@wickettraining.com> wrote:

> If you need to validate after the data has been pushed to the model, then
> just validate in the onSubmit and call error on the individual components
> for the form.  Leave the form validator out of it.
>
> On Tue, Jun 29, 2010 at 11:03 PM, Ray Weidner <
> ray.weidner.developer@gmail.com> wrote:
>
> > Igor (or anyone else who knows the answer),
> >
> > There is one problem with my implementation of the FormValidator in the
> > code
> > that I included earlier.  It performs validation using the Form's model
> > object, which is a problem because the input hasn't been mapped onto it
> at
> > this stage of validation.
> >
> > What I'd like to do now is somehow perform this validation from within
> the
> > #onSubmit call.  I don't know if this is even possible.  I am hoping that
> > perhaps I could register the errors by calling error(String) as you
> > mentioned, and then redirecting the form back to processing these
> > validation
> > errors as it normally would.
> >
> > Below is a simplification of the code I'd like to execute in this
> > situation.
> >  Note the text "// TODO: ???"  That's the part that I need to fill in,
> > ideally redirecting back down the 'invalid' pathway.
> >
> > The HTML here is just a form with some fields and a submit button, so
> I'll
> > omit.
> >
> > *public interface BusinessValidator { public ValidationResult validate
> > (Record record, Action action); } public interface
> BusinessValidationResult
> > { public boolean isValid (); public Set <String> getGeneralErrorMessages
> > ();
> > public Set <String> getFieldErrorFieldNames (); public Set <String>
> > getFieldErrorMessage (String fieldName); } public interface
> BusinessService
> > { public List <Action> getAllActions (); public BusinessValidator
> > getValidator (Action action); public void save (Record record); [lots of
> > other stuff] ... } public class RecordUpdatePage extends WebPage {
> private
> > Record theRecord = null; private Form <Record> theForm = null; private
> > BusinessService theService = null; private Action selectedAction = null;
> > public RecordEditPage (BusinessService service, Record record) {
> theService
> > = service; theRecord = record; createForm (); } public Record getRecord
> ()
> > {
> > return theRecord; } private void createForm () { theForm = new Form
> > <Record>
> > ("recordUpdateForm"); theForm.setModel (new CompoundPropertyModel
> <Record>
> > (theRecord)); theForm.add (new DropDownChoice <Action> ("action", new
> > PropertyModel <Action> (this, "selectedAction"), new
> > LoadableDetachableModel
> > <List <Action>> () { @Override protected List <Action> load () { return
> new
> > ArrayList <Action> (theService.getAllActions ()); } }, ) ); ... [add a
> > bunch
> > of fields] ... theForm.add (new Button ("save")) { @Override public void
> > onSubmit () { save (); } }; } private void save () { if (! validate ()) {
> > //
> > TODO: ??? } else { theService.save (record); setResponsePage
> > (MainPage.class); } } public boolean validate () { BusinessValidator
> > validator = theService.getValidator (selectedAction); Record record =
> > ((Form
> > <Record>) form).getModel ().getObject (); BusinessValidation result =
> > validator.validate (theRecord, selectedAction); if (result.isValid ()) {
> > return true; } for (String generalErrorMessage :
> > result.getGeneralErrorMessages ()) { theForm.error (generalErrorMessage);
> }
> > Map <String, Set <String>> fieldErrorMap = result.getErrorMessageMap ();
> > for
> > (String fieldName : fieldErrorMap.keySet ()) { for (String
> > fieldErrorMessage
> > : fieldErrorMap.get (fieldName)) { Component field = theForm.get
> > (fieldName); if (field != null) { field.error (fieldErrorMessage); } else
> {
> > theForm.error (fieldErrorMessage); } } } return false; } }*
> >
> > On Tue, Jun 29, 2010 at 10:29 AM, Ray Weidner <
> > ray.weidner.developer@gmail.com> wrote:
> >
> > > Thanks, Igor, I now see the methods you are talking about, and your
> > > explanation makes perfect sense.
> > >
> > >
> > > On Tue, Jun 29, 2010 at 1:16 AM, Igor Vaynberg <
> igor.vaynberg@gmail.com
> > >wrote:
> > >
> > >> On Mon, Jun 28, 2010 at 9:27 PM, Ray Weidner
> > >> <ra...@gmail.com> wrote:
> > >> > Hi all,
> > >> >
> > >> > The application that I'm current writing already has its own
> > validation
> > >> > logic, but I would like to integrate its results into the existing
> > >> Wicket
> > >> > form validation so as to unify the feedback.  To this end, I'm
> trying
> > to
> > >> > extend AbstractFormValidator to wrap my system's logic.
> > >> >
> > >> > The way my system's validation works is pretty simple and
> > >> self-explanatory:
> > >> >
> > >> > *public interface Validator {*
> > >> > * **public ValidationResult validate (Record record, Action
> action);*
> > >> > *}*
> > >> > *
> > >> > *
> > >> > *public interface ValidationResult {*
> > >> > * **public boolean isValid ();*
> > >> > *
> > >> > *
> > >> > * **public Set <String> getGeneralErrorMessages ();*
> > >> > * *
> > >> > * **public Set <String> getFieldErrorFieldNames ();*
> > >> > * *
> > >> > * **public Set <String> getFieldErrorMessages (String fieldName);*
> > >> > *}*
> > >> >
> > >> > The ValidationResultImpl is pretty much a simple bean for setting
> and
> > >> > getting error messages, both general, and by field.  Validator
> > >> > implementations are specific to the need, and the specifics aren't
> > >> important
> > >> > here.
> > >> >
> > >> > For my adapter, I'm able to get to a certain point, and I'm not sure
> > >> what
> > >> > call to make next:
> > >> >
> > >> > *public class RecordFormValidator extends AbstractFormValidator {*
> > >> > * **private Validator validator = null;*
> > >> > * **private Action action = null;*
> > >> > * *
> > >> > * **public RecordFormValidator (Validator validator, Action action)
> {*
> > >> > * **this.validator = validator;*
> > >> > * **this.action = action;*
> > >> > * **}*
> > >> > *
> > >> > *
> > >> > * **@Override*
> > >> > * **public FormComponent <?> [] getDependentFormComponents () {*
> > >> > * **return new FormComponent [0];*
> > >> > * **}*
> > >> > *
> > >> > *
> > >> > * **@Override*
> > >> > * **public void validate (Form <?> arg0) {*
> > >> > * **Record record = ((Form <Record>) arg0).getModel ().getObject
> ();*
> > >> > * **Validation result = validator.validate (record, action);*
> > >> > * *
> > >> > * **for (String generalErrorMessage : result.getGeneralErrorMessages
> > ())
> > >> {*
> > >> > * **// TODO: ???*
> > >> > * **}*
> > >> > * **for (String fieldName : result.getFieldErrorFieldNames ()) {*
> > >> > * **for (String fieldErrorMessage : result.getFieldErrorMessages
> > >> (fieldName))
> > >> > {*
> > >> > * **// TODO: ???*
> > >> > * **}*
> > >> > * **}*
> > >> > * **}*
> > >> > *}*
> > >> > *
> > >> > *
> > >> > In examples I see online, validation errors seem to be registered by
> > >> calls
> > >> > of the #error method.  It seems like I should be doing that here,
> too,
> > >> but
> > >> > what I don't know is:
> > >> >
> > >> >   - Which object's #error method should be called?  The validator's?
> > >>  The
> > >> >   FormComponent corresponding to the field in question?  I want
> > >> field-specific
> > >> >   errors appropriately associated with their fields, so I can use
> > >> filters for
> > >> >   field-specific feedback, etc.
> > >>
> > >> if you want formcomponent specific errors then you have to map the
> > >> errors from your business logic to the wicket components and call
> > >> error on them.
> > >>
> > >> >   - I already have my actual error strings, so how do I skip around
> > the
> > >> >   resource logic to just set the error message?
> > >>
> > >> there is error(IValidationError) but there is also error(String), call
> > >> the latter.
> > >>
> > >> >   - What should I do with my "generalErrorMessages," which aren't
> tied
> > >> to
> > >> >   any specific field?
> > >>
> > >> i usually report these kinds of errors on the form itself and have a
> > >> special feedbackpanel that filters those.
> > >>
> > >> -igor
> > >>
> > >> >
> > >> > Any help would be appreciated, thanks!
> > >> >
> > >> > Ray Weidner
> > >> >
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > >> For additional commands, e-mail: users-help@wicket.apache.org
> > >>
> > >>
> > >
> >
>
>
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>

Re: extending AbstractFormValidator as a validation adapter

Posted by Igor Vaynberg <ig...@gmail.com>.
validation is needed because you cant stuff "abc" into an Integer
field, so it cannot be turned off completely. if you dont add any
validators and dont call setrequired then you should be good to go
because the only remaining bit wicket will do before pushing into your
models is type checking and conversion.

-igor


On Thu, Jul 1, 2010 at 10:27 PM, Ray Weidner
<ra...@gmail.com> wrote:
> Thanks for your reply, Jeremy.  I just got around to reading it.  I had been
> under the impression that I'd have to call something from the #onSubmit to
> trigger the feedback, but I tried it out based on your suggestion, and it
> fits the bill.
>
> This "deferred validation" is actually pretty useful in a number of
> situations, so thanks.  It would be nice if Wicket had a built-in way of
> turning off validation for certain buttons without turning off the
> form-to-model update, i.e. not #setDefaultFormProcessing, which precludes
> the latter.
>
>
> On Wed, Jun 30, 2010 at 12:08 AM, Jeremy Thomerson <
> jeremy@wickettraining.com> wrote:
>
>> If you need to validate after the data has been pushed to the model, then
>> just validate in the onSubmit and call error on the individual components
>> for the form.  Leave the form validator out of it.
>>
>> On Tue, Jun 29, 2010 at 11:03 PM, Ray Weidner <
>> ray.weidner.developer@gmail.com> wrote:
>>
>> > Igor (or anyone else who knows the answer),
>> >
>> > There is one problem with my implementation of the FormValidator in the
>> > code
>> > that I included earlier.  It performs validation using the Form's model
>> > object, which is a problem because the input hasn't been mapped onto it
>> at
>> > this stage of validation.
>> >
>> > What I'd like to do now is somehow perform this validation from within
>> the
>> > #onSubmit call.  I don't know if this is even possible.  I am hoping that
>> > perhaps I could register the errors by calling error(String) as you
>> > mentioned, and then redirecting the form back to processing these
>> > validation
>> > errors as it normally would.
>> >
>> > Below is a simplification of the code I'd like to execute in this
>> > situation.
>> >  Note the text "// TODO: ???"  That's the part that I need to fill in,
>> > ideally redirecting back down the 'invalid' pathway.
>> >
>> > The HTML here is just a form with some fields and a submit button, so
>> I'll
>> > omit.
>> >
>> > *public interface BusinessValidator { public ValidationResult validate
>> > (Record record, Action action); } public interface
>> BusinessValidationResult
>> > { public boolean isValid (); public Set <String> getGeneralErrorMessages
>> > ();
>> > public Set <String> getFieldErrorFieldNames (); public Set <String>
>> > getFieldErrorMessage (String fieldName); } public interface
>> BusinessService
>> > { public List <Action> getAllActions (); public BusinessValidator
>> > getValidator (Action action); public void save (Record record); [lots of
>> > other stuff] ... } public class RecordUpdatePage extends WebPage {
>> private
>> > Record theRecord = null; private Form <Record> theForm = null; private
>> > BusinessService theService = null; private Action selectedAction = null;
>> > public RecordEditPage (BusinessService service, Record record) {
>> theService
>> > = service; theRecord = record; createForm (); } public Record getRecord
>> ()
>> > {
>> > return theRecord; } private void createForm () { theForm = new Form
>> > <Record>
>> > ("recordUpdateForm"); theForm.setModel (new CompoundPropertyModel
>> <Record>
>> > (theRecord)); theForm.add (new DropDownChoice <Action> ("action", new
>> > PropertyModel <Action> (this, "selectedAction"), new
>> > LoadableDetachableModel
>> > <List <Action>> () { @Override protected List <Action> load () { return
>> new
>> > ArrayList <Action> (theService.getAllActions ()); } }, ) ); ... [add a
>> > bunch
>> > of fields] ... theForm.add (new Button ("save")) { @Override public void
>> > onSubmit () { save (); } }; } private void save () { if (! validate ()) {
>> > //
>> > TODO: ??? } else { theService.save (record); setResponsePage
>> > (MainPage.class); } } public boolean validate () { BusinessValidator
>> > validator = theService.getValidator (selectedAction); Record record =
>> > ((Form
>> > <Record>) form).getModel ().getObject (); BusinessValidation result =
>> > validator.validate (theRecord, selectedAction); if (result.isValid ()) {
>> > return true; } for (String generalErrorMessage :
>> > result.getGeneralErrorMessages ()) { theForm.error (generalErrorMessage);
>> }
>> > Map <String, Set <String>> fieldErrorMap = result.getErrorMessageMap ();
>> > for
>> > (String fieldName : fieldErrorMap.keySet ()) { for (String
>> > fieldErrorMessage
>> > : fieldErrorMap.get (fieldName)) { Component field = theForm.get
>> > (fieldName); if (field != null) { field.error (fieldErrorMessage); } else
>> {
>> > theForm.error (fieldErrorMessage); } } } return false; } }*
>> >
>> > On Tue, Jun 29, 2010 at 10:29 AM, Ray Weidner <
>> > ray.weidner.developer@gmail.com> wrote:
>> >
>> > > Thanks, Igor, I now see the methods you are talking about, and your
>> > > explanation makes perfect sense.
>> > >
>> > >
>> > > On Tue, Jun 29, 2010 at 1:16 AM, Igor Vaynberg <
>> igor.vaynberg@gmail.com
>> > >wrote:
>> > >
>> > >> On Mon, Jun 28, 2010 at 9:27 PM, Ray Weidner
>> > >> <ra...@gmail.com> wrote:
>> > >> > Hi all,
>> > >> >
>> > >> > The application that I'm current writing already has its own
>> > validation
>> > >> > logic, but I would like to integrate its results into the existing
>> > >> Wicket
>> > >> > form validation so as to unify the feedback.  To this end, I'm
>> trying
>> > to
>> > >> > extend AbstractFormValidator to wrap my system's logic.
>> > >> >
>> > >> > The way my system's validation works is pretty simple and
>> > >> self-explanatory:
>> > >> >
>> > >> > *public interface Validator {*
>> > >> > * **public ValidationResult validate (Record record, Action
>> action);*
>> > >> > *}*
>> > >> > *
>> > >> > *
>> > >> > *public interface ValidationResult {*
>> > >> > * **public boolean isValid ();*
>> > >> > *
>> > >> > *
>> > >> > * **public Set <String> getGeneralErrorMessages ();*
>> > >> > * *
>> > >> > * **public Set <String> getFieldErrorFieldNames ();*
>> > >> > * *
>> > >> > * **public Set <String> getFieldErrorMessages (String fieldName);*
>> > >> > *}*
>> > >> >
>> > >> > The ValidationResultImpl is pretty much a simple bean for setting
>> and
>> > >> > getting error messages, both general, and by field.  Validator
>> > >> > implementations are specific to the need, and the specifics aren't
>> > >> important
>> > >> > here.
>> > >> >
>> > >> > For my adapter, I'm able to get to a certain point, and I'm not sure
>> > >> what
>> > >> > call to make next:
>> > >> >
>> > >> > *public class RecordFormValidator extends AbstractFormValidator {*
>> > >> > * **private Validator validator = null;*
>> > >> > * **private Action action = null;*
>> > >> > * *
>> > >> > * **public RecordFormValidator (Validator validator, Action action)
>> {*
>> > >> > * **this.validator = validator;*
>> > >> > * **this.action = action;*
>> > >> > * **}*
>> > >> > *
>> > >> > *
>> > >> > * **@Override*
>> > >> > * **public FormComponent <?> [] getDependentFormComponents () {*
>> > >> > * **return new FormComponent [0];*
>> > >> > * **}*
>> > >> > *
>> > >> > *
>> > >> > * **@Override*
>> > >> > * **public void validate (Form <?> arg0) {*
>> > >> > * **Record record = ((Form <Record>) arg0).getModel ().getObject
>> ();*
>> > >> > * **Validation result = validator.validate (record, action);*
>> > >> > * *
>> > >> > * **for (String generalErrorMessage : result.getGeneralErrorMessages
>> > ())
>> > >> {*
>> > >> > * **// TODO: ???*
>> > >> > * **}*
>> > >> > * **for (String fieldName : result.getFieldErrorFieldNames ()) {*
>> > >> > * **for (String fieldErrorMessage : result.getFieldErrorMessages
>> > >> (fieldName))
>> > >> > {*
>> > >> > * **// TODO: ???*
>> > >> > * **}*
>> > >> > * **}*
>> > >> > * **}*
>> > >> > *}*
>> > >> > *
>> > >> > *
>> > >> > In examples I see online, validation errors seem to be registered by
>> > >> calls
>> > >> > of the #error method.  It seems like I should be doing that here,
>> too,
>> > >> but
>> > >> > what I don't know is:
>> > >> >
>> > >> >   - Which object's #error method should be called?  The validator's?
>> > >>  The
>> > >> >   FormComponent corresponding to the field in question?  I want
>> > >> field-specific
>> > >> >   errors appropriately associated with their fields, so I can use
>> > >> filters for
>> > >> >   field-specific feedback, etc.
>> > >>
>> > >> if you want formcomponent specific errors then you have to map the
>> > >> errors from your business logic to the wicket components and call
>> > >> error on them.
>> > >>
>> > >> >   - I already have my actual error strings, so how do I skip around
>> > the
>> > >> >   resource logic to just set the error message?
>> > >>
>> > >> there is error(IValidationError) but there is also error(String), call
>> > >> the latter.
>> > >>
>> > >> >   - What should I do with my "generalErrorMessages," which aren't
>> tied
>> > >> to
>> > >> >   any specific field?
>> > >>
>> > >> i usually report these kinds of errors on the form itself and have a
>> > >> special feedbackpanel that filters those.
>> > >>
>> > >> -igor
>> > >>
>> > >> >
>> > >> > Any help would be appreciated, thanks!
>> > >> >
>> > >> > Ray Weidner
>> > >> >
>> > >>
>> > >> ---------------------------------------------------------------------
>> > >> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> > >> For additional commands, e-mail: users-help@wicket.apache.org
>> > >>
>> > >>
>> > >
>> >
>>
>>
>>
>> --
>> Jeremy Thomerson
>> http://www.wickettraining.com
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org