Forum OpenACS Q&A: How to know if there's an error in a form (template level)

Hi all,

I'm writing a customized form template and I want to insert a message at the top of the form in case there's an error in any of the form fields.

I can see that "formerror" is created for each elements that has an error but I need a property at the form level that I can read in my template, something like "@form_id@.error_flag". Is there anything like that I can use?

Hi again,

Finally I inserted the following in my template and it seems to work:

<noparse>
<if [template::form::get_errors @id@] not nil>
    <div class="form-error">Error message goes here</div>
</if>
</noparse>

Heya Emma,

First very nice to see you're still around...

When you posted your question/answer, I got curious as I've seen a little about how the ad_complain stuff worked, and now I'm curious to see a full example of maybe a hypothetical foo.tcl and foo.adp where you customize the form, especially where you place the error msg. So, can you share about that?

-Jim

Hi Jim,

Yeah, I've been quite busy with other things lately but never stopped reading the forums :).

I'm using ad_form and I didn't want to use ad_complain or ad_returnerror that would abort the process. Since ad_form handles validation, I wanted to take advantage of it. My problem was a usability issue: the form is long enough to have to scroll to get to the last fields. If the user doesn't respond mandatory questions that are at the bottom, the error messages will appear next to those fields and the user won't be aware there was an error until she scrolls down, that's why I want to insert a message at the top of the template.

So in the Tcl, I have the usual ad_form declaration, and the adp is:

<master>
    <property name="doc(title)">@page_title@</property>
    <h1>@page_title@</h1>
    <formtemplate id="survey" style="custom-form-template"></formtemplate>

The custom-form-template.adp is a copy of acs-templating/resources/forms/standard.adp with a few minor changes plus the insertion I mentioned in my previous message. The message string is hard coded in the template for now, it could be a message key also, but predefined anyway. The result is not that elegant because the message is not inserted before the form but in between the hidden fields after the form tag:

<form id="survey" name="survey" method="post" action="survey" class="vertical-form">
<div><input type="hidden" name="form:mode" value="edit" ></div>
<div><input type="hidden" name="form:id" value="survey" ></div>
<div class="form-error">Error message goes here</div>  <====
<div><input type="hidden" name="__confirmed_p" value="0" ></div>
...

I guess a better way would be to extend ad_form and the form processor to include the error flag and message in the form structure and render it in a more convenient place of the HTML document but for my current need the solution I've found is good enough.