Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1649
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Validating form inputs? |
| Date | 2012-03-21 13:37 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <15741371.1909.1332362223206.JavaMail.geo-discussion-forums@pbij6> (permalink) |
| References | <ed7f31e9-8a19-46c7-9a7c-ad8aabfb9599@x10g2000pbi.googlegroups.com> <uA4ar.13560$fj7.13111@newsfe20.iad> <9942279.960.1332273017948.JavaMail.geo-discussion-forums@pbbpx7> <578d7f16-cbfb-4718-a639-9e7a312822fc@t2g2000pbg.googlegroups.com> |
Davej wrote:
> Lew wrote:
>> Daniel Pitts wrote:
>>> Davej wrote:
>>>> I have been thinking about the way I have been validating form
>>>> inputs in the servelet [sic] and wonder if I would be much
>>>> better off using class methods to verify these inputs?
>>
>> What exactly do you mean by "class methods"? Do you mean 'static'
>> member methods?
>>
>>>> Consider that I am almost always gathering the inputs to
>>>> instantiate one or more objects, but I gather and validate
>>>> -- and then instantiate. Maybe I should instantiate an empty
>>>> object and then use class methods to validate the inputs?
>>
>> Maybe. It depends on what you mean by "class methods".
>>
>
> Why? I think it could be done either way. If it was a static method is
> would be made generic. If it was non-static it could be an alternate
> set method...
You asked if you "should instantiate an empty object". That would not be needed if using static methods only. That's why I said "it depends on what you mean by 'class methods'".
> public String ValidateAndSetQuiz1(String q1) //returns error message
Please follow the Java naming conventions.
> {
> double score;
You could narrow the scope of 'score' more if you wanted to.
> try{
> score = Double.parseDouble(q1);
> if ( score < 0 || score > 100.0)
> return "Value out of allowed range";
>
> }catch (Exception e)
What exception are you catching?
For most routines, catching just 'Exception' is an antipattern.
> {
> return "Value is non-numeric";
And as Daniel noted, this is not often the best way to handle an exception.
I would wonder if logging the exception is appropriate here, perhaps.
> }
>
> m_quiz1 = score; //accept and set instance variable m_quiz1
> return null; //signifies that submitted value was accepted
> }
It's more conventional to return a value than 'null' or a 'String' that are not in the value domain.
It's likely to be better engineering to separate setting and validation into different methods.
--
Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Validating form inputs? Davej <galt_57@hotmail.com> - 2012-03-20 11:16 -0700
Re: Validating form inputs? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-20 12:08 -0700
Re: Validating form inputs? Lew <lewbloch@gmail.com> - 2012-03-20 12:50 -0700
Re: Validating form inputs? markspace <-@.> - 2012-03-21 09:24 -0700
Re: Validating form inputs? Davej <galt_57@hotmail.com> - 2012-03-21 12:02 -0700
Re: Validating form inputs? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-21 12:59 -0700
Re: Validating form inputs? Lew <lewbloch@gmail.com> - 2012-03-21 13:37 -0700
Re: Validating form inputs? Davej <galt_57@hotmail.com> - 2012-03-20 12:56 -0700
Re: Validating form inputs? Lew <lewbloch@gmail.com> - 2012-03-20 13:58 -0700
Re: Validating form inputs? markspace <-@.> - 2012-03-21 09:30 -0700
Re: Validating form inputs? Roedy Green <see_website@mindprod.com.invalid> - 2012-03-20 14:28 -0700
Re: Validating form inputs? Gene Wirchenko <genew@ocis.net> - 2012-03-20 19:16 -0700
Re: Validating form inputs? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-21 17:41 -0300
Re: Validating form inputs? Gene Wirchenko <genew@ocis.net> - 2012-03-21 14:46 -0700
Re: Validating form inputs? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-21 19:54 -0300
Re: Validating form inputs? Roedy Green <see_website@mindprod.com.invalid> - 2012-03-22 10:34 -0700
Re: Validating form inputs? Lew <lewbloch@gmail.com> - 2012-03-21 14:56 -0700
Re: Validating form inputs? Gene Wirchenko <genew@ocis.net> - 2012-03-21 19:09 -0700
Re: Validating form inputs? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-22 08:36 -0700
Re: Validating form inputs? Gene Wirchenko <genew@ocis.net> - 2012-03-22 09:40 -0700
csiph-web