Suppressing html_error() behaviour that's within html()?

0 views
Skip to first unread message

Woei Shyang

unread,
Sep 7, 2009, 10:56:40 AM9/7/09
to Rose::HTML::Objects
Hi all,

I've recently started experimenting with Rose::HTML::Objects to build
forms, and I noticed that for the HTML output that is generated for
each field, it seems like RHTMLO has already performed validation on
it, and included the output of html_error() together with the output
from html().

I only want to display the errors when the user has submitted the
form, but not when the form is initially displayed and loaded, is
there a way to suppress the behaviour?

John Siracusa

unread,
Sep 7, 2009, 2:53:40 PM9/7/09
to rose-htm...@googlegroups.com
On Mon, Sep 7, 2009 at 10:56 AM, Woei Shyang<g...@woeishyang.com> wrote:
> I've recently started experimenting with Rose::HTML::Objects to build
> forms, and I noticed that for the HTML output that is generated for
> each field, it seems like RHTMLO has already performed validation on
> it, and included the output of html_error() together with the output
> from html().

The html() method "returns the HTML serialization of the field, along
with the HTML error message, if any."

http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Form/Field.pm#html

An error message will only exist, however, if validate() has been called.

My guess is that you are re-using form objects that were validate()d
in an earlier request. Re-using form objects is fine, and in fact
recommended, but you must be sure to reset() (or clear(), I suppose)
the form before each use.

http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Form.pm#reset
http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Form.pm#clear

This whole process (reset()ing forms, calling validate() on
submission, etc.) is best handled by integrating this flow into your
web application framework.

-John

Woei Shyang

unread,
Sep 8, 2009, 10:27:32 AM9/8/09
to Rose::HTML::Objects
Thanks for your prompt reply John :)

One more question though. Validation takes place on individual fields
right? Suppose on a form I have constraints where one field is
dependent on another field (ie: date of birth must be specified if
user wants to join our 'birthday sweepstakes' for instance). Do I
override validate() on the form object and perform the validation
there? Also how do I tell the 'date of birth' field to return a fail
in such a case, since the validate method for that field would be
independent of the 'birthday sweepstakes' field.

Sorry if the example seems a bit vague, that's something that just
rolled off the top of my head :)

Sean Quinlan

unread,
Sep 8, 2009, 11:34:24 AM9/8/09
to rose-htm...@googlegroups.com
Woei, I'm going to try to make myself useful and see if I can answer your questions.


On Tue, Sep 8, 2009 at 10:27 AM, Woei Shyang <g...@woeishyang.com> wrote:

Thanks for your prompt reply John :)

One more question though. Validation takes place on individual fields
right? Suppose on a form I have constraints where one field is
dependent on another field (ie: date of birth must be specified if
user wants to join our 'birthday sweepstakes' for instance). Do I
override validate() on the form object and perform the validation
there?

Yes. For example, in your form package:

sub validate {
  my($self) = shift;
 
  # default validation checks
  my $simple_valid = $self->SUPER::validate(@_);
}
 
Also how do I tell the 'date of birth' field to return a fail
in such a case, since the validate method for that field would be
independent of the 'birthday sweepstakes' field.

You set the error on the field:

sub validate {
  my($self) = shift;
 
  # default validation checks
  my $simple_valid = $self->SUPER::validate(@_);

  my $no_sweepstakes_for_you = 0;
  # ... complex validation on date of birth & sweepstakes, set $no_sweepstakes_for_you = 1 on fail
  if ($no_sweepstakes_for_you) {
    $self->field('date_of_birth')->error ('Sorry, you did not win the birthday sweepstakes.');
    return 0;
  }

  return 0 unless $simple_valid;
}
 

Sorry if the example seems a bit vague, that's something that just
rolled off the top of my head :)

Hopefully my examples matched your context. ;)

--
Sean Quinlan
gpg public-key http://grendels-den.org/sean.asc

Reply all
Reply to author
Forward
0 new messages