Re: Validation on my own

Kieran Kelleher <[email protected]> Tue, 3 May 2011 12:27:03 -0400
Newsgroups gmane.comp.web.webobjects.woproject.devel
Message-ID <[email protected]>
There are a few different approaches. It seems everyone has to create one when they start programming WebObjects. 

In a overview nutshell, an approach involves overriding:

1)	WOComponent.validationFailedWithException(Throwable, Object, String)

2) ..... then collecting the error info into a 'holding area' (for example a collection in ERXThreadStorage (since these are usually transient messages)

3) .... and have a little reusable component in your page that checks for errors in the holding area and if there are errors, it displays them.




On May 3, 2011, at 9:40 AM, Peter Müller wrote:

> Hello,
> thank you Kieran and Martino for your fast answers.
> 
> I also thought about Martino's solution. But I only want to do this as "last way", because there are more other parts in my software, I'll need that.
> 
> I think, that I'll order the book. I looked at the preview on amazon, and there are a view things, I want to learn.
> 
> But what can I do, until the book reach me? How can I display the messages, Wonder generates for me? At the moment, I have an accessor, which generates an alertMessage-script, which will be displayed, if alertMessage is set.
> 
> public String alertMessageScript() {
>    	if(alertMessage==null) return null;
>    	String tmp = alertMessage;
>    	alertMessage=null;
>    	return "<script language=\"javascript\" type=\"text/javascript\">"+
>    			"alert('" + tmp + "');" +
>    			"</script>";
> 
>    }
> 
> How can I display the messages, if I want to use the wonder-generated Validation-Messages?
> 
> Thanks
> Peter M.
> 
> 
> -------- Original-Nachricht --------
>> Datum: Tue, 3 May 2011 08:51:53 -0400
>> Von: Kieran Kelleher <[email protected]>
>> An: [email protected], "Peter Müller" <[email protected]>
>> Betreff: Re: Validation on my own
> 
>> You can implement custom validation by simply adding validation methods to
>> any class that implements NSValidation interface.  
>> 
>> You can throw custom 'nice' message from your own Validation method.
>> 
>> Also, if you use Wonder, the default messages will be 'nice' and Wonder
>> allows for nice localization too.
>> 
>> There is a bit for you to learn, but it will be worth it.
>> 
>> So for your user EO, you can add custom validation methods.
>> 
>> For example for your password logic below, this would normally be added in
>> the EO class as sth like:
>> 
>>   public String validatePassword(Object aValue) throws
>> NSValidation.ValidationException {
>>        if (aValue == null || aValue.toString().length() < 5) {
>>            throw new NSValidation.ValidationException("Password must be
>> set and must contain at least 5 characters.");
>>        }
>>        return aValue.toString();
>>    }
>> 
>> Your validate method gets called automatically in takeValuesFromRequest
>> phase of request handling.
>> 
>> Your current approach will not work because you are validating in the
>> invokeAction phase which comes after the takeValues phase.
>> 
>> There is a book written by a bourbon-loving fairly smart Canuck entitled
>> "Practical WebObjects". Chapter 5 explains Validation in very easy to
>> understand terms. If you read that, you will then know enough to be dangerous.
>> ;-)
>> 
>> Regards, Kieran
>> 
>> 
>> On May 3, 2011, at 8:31 AM, Peter Müller wrote:
>> 
>>> Hello WO-Community,
>>> i've a coding-problem because WO does a job for me, I want to do on my
>> own.
>>> 
>>> I've a modelclass called "User" with a view fields:
>>> id, firstname, lastname, email, ...
>>> 
>>> id is primary key, firstname, lastname and a view others are "required"
>> fields.
>>> 
>>> On my component I have a Form to set this fields. The "save"-Button is
>> linked to my method:
>>> 
>>> public WOComponent save() {
>>> 		alertMessage=null;
>>> 		if(selectedUser.lastname()==null) {
>>> 			alertMessage="Lastname of user must be set!";
>>> 			return null;
>>> 		}
>>> 		if(selectedUser.firstname()==null) {
>>> 			alertMessage="Firstname of user must be set!";
>>> 			return null;
>>> 		}
>>> 		if(selectedUser.username()==null) {
>>> 			alertMessage="Username of user must be set!";
>>> 			return null;
>>> 		}
>>> 		if(selectedUser.password()==null ||
>> selectedUser.password().length()<5) {
>>> 			alertMessage="Password of user must be set and must contain at least
>> 5 signs.";
>>> 			return null;
>>> 		}
>>> 		session.defaultEditingContext().saveChanges();
>>> 		selectedUser = null;
>>> 		return null;
>>> }
>>> 
>>> As you can see, I want to do the validation for the fields on my own
>> (because I want to make a "nice" message to the user, and later also
>> multilingual).
>>> 
>>> But now the problem: If I click "save" on my component, first the form
>> will be set, and WO validates, if all required fields are set. If not, it
>> will print a message in console and "reset" the fields. My validation will be
>> ignored.
>>> 
>>> Have I any chance to validate "required" fields on my own, and ignore
>> WO's validation?
>>> 
>>> Thank you very much,
>>> All the best from germany
>>> Peter M.
>>> -- 
>>> Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
>>> belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
>> 
> 
> -- 
> NEU: FreePhone - kostenlos mobil telefonieren und surfen!			
> Jetzt informieren: http://www.gmx.net/de/go/freephone