X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=doc%2FRequest.pod;h=bc2b303555fedc51257a4e446fefc499def7aba8;hb=ab11625025348dd2f0b4324413463358f52aede3;hp=1782cd5df5b23c5b1c5600aade6e0e0e8d8fabfb;hpb=b90c6e146c423746001d304953b6b136acc666db;p=maypole.git diff --git a/doc/Request.pod b/doc/Request.pod index 1782cd5..bc2b303 100644 --- a/doc/Request.pod +++ b/doc/Request.pod @@ -609,6 +609,82 @@ on an ISBN: The request will carry on as though it were a normal C POST, but with the additional fields we have provided. +=head3 Catching errors in a form + +A user has submitted erroneous input to an edit/create form. You want to +send him back to the form with errors displayed against the erroneous +fields, but have the other fields maintain the values that the user +submitted. + +B: This is basically what the default C template and +C method conspire to do, but it's worth highlighting again how +they work. + +If there are any errors, these are placed in a hash, with each error +keyed to the erroneous field. The hash is put into the template as +C, and we process the same F template again: + + $r->{template_args}{errors} = \%errors; + $r->{template} = "edit"; + +This throws us back to the form, and so the form's template should take +note of the errors, like so: + + FOR col = classmetadata.columns; + NEXT IF col == "id"; + "

"; + ""; classmetadata.colnames.$col; ""; + ": "; + item.to_field(col).as_HTML; + "

"; + IF errors.$col; + ""; errors.$col; ""; + END; + END; + +If we're designing our own templates, instead of using generic ones, we +can make this process a lot simpler. For instance: + + + First name: + + + Last name: + + + [% IF errors.forename OR errors.surname %] + + [% errors.forename %] + [% errors.surname %] + + [% END %] + +The next thing we want to do is to put the originally-submitted values +back into the form. We can do this relatively easily because Maypole +passes the Maypole request object to the form, and the POST parameters +are going to be stored in a hash as C. Hence: + + + First name: + + + Last name: + + +Finally, we might want to only re-fill a field if it is not erroneous, so +that we don't get the same bad input resubmitted. This is easy enough: + + + First name: + + + Last name: + + =head3 Uploading files and other data You want the user to be able to upload files to store in the database.