AnsweredAssumed Answered

Share form validation handler

Question asked by chrisokelly on Jul 13, 2012
Latest reply on Aug 27, 2015 by stacy
Hi,

I am attempting to create a custom form validation handler to check that one date is later than another date. I have attempted so far with information from http://wiki.alfresco.com/wiki/Forms_Developer_Guide, following the tutorial here ->http://www.tribloom.com/blogs/michael/2011/05/03/share-dynamic-forms/. So so far I have, in a jar file at config/alfresco/web-extension/site-webscripts/org/alfresco/components/form/form.get.head.ftl

<@script type="text/javascript" src="${page.url.context}/res/js/formValidation.js"></@script>
in the same jar file at source/web/js/formValidation.js:
/**
* Corporate Document Expiry and Reminder date's validation handler, tests that the expiry date > reminder date.
*
* @method corporateDatesValidation
* @param field {object} The element representing the field the validation is for
* @param args {object} Not used
* @param event {object} The event that caused this handler to be called, maybe null
* @param form {object} The forms runtime class instance the field is being managed by
* @param silent {boolean} Determines whether the user should be informed upon failure
* @param message {string} Message to display when validation fails, maybe null
* @static
*/
Alfresco.forms.validation.corporateDates = function corporateDatesValidation(field, args, event, form, silent, message)
   {
   Alfresco.logger.warn("Validating state of field '" + field.id + "'");
   var valid = true;
   valid = YAHOO.lang.trim(field.value).length !== 0;
   if (valid)
      {
      Alfresco.logger.warn("field value length test passed, checking dates can be parsed");
      // check dates can be parsed
      str_expiryDate = field.form.prop_mcwf_expiryDate.value;
      str_reminderDate = field.form.prop_mcwf_reminderDate.value;
      Alfresco.logger.warn("Expiry Date: " + str_expiryDate + " | Reminder Date: " + str_reminderDate);
      d_expiryDate = Date.parse(str_expiryDate);
      d_reminderDate = Date.parse(str_reminderDate);
      if (d_expiryDate && d_reminderDate)
         {
         Alfresco.logger.warn("Dates Parsed, checking expiry > reminder");
         // check expirydate>reminder date
         valid = d_expiryDate>d_reminderDate;
         Alfresco.logger.warn("The result of the date validation was: " + valid);
         }
      }
      if (!valid && !silent && form)
      {
         // if the keyCode from the event is the TAB or SHIFT keys don't show the error
         if (event && event.keyCode != 9 && event.keyCode != 16 || !event)
         {
            form.addError("Expiry and Review dates are mandatory. Expiry date must be after Review date.", field);
         }
      }

      return valid;
   };
and in my share-config-custom.xml I have:
        <config evaluator="task-type" condition="mcwf:setExpirationTask">
                <forms>
                        <form>
                                <field-visibility>
                                        <show id="mcwf:expiryDate" />
                                        <show id="mcwf:reminderDate" />
                                        <show id="transitions" />
                                </field-visibility>
                                <appearance>
                                        <set id="" appearance="title" label-id="workflow.set.general" />
                                        <set id="other" appearance="title" label-id="workflow.set.other" />
                                        <set id="response" appearance="title" labelid="workflow.set.response" />
                                        <field id="mcwf:expiryDate" set="other">
                                                <constraint-handler>
                                                        <constraint type="MANDATORY" validation-handler="Alfresco.forms.validation.corporateDates" event="keyup" />
                                                </constraint-handler>
                                        </field>
                                        <field id="mcwf:reminderDate" set="other">
                                                <constraint-handler>
                                                        <constraint type="MANDATORY" validation-handler="Alfresco.forms.validation.corporateDates" event="keyup" />
                                                </constraint-handler>
                                        </field>
                                        <field id="transitions" set="response" />
                                </appearance>
                        </form>
                        <form id="workflow-details">
                                <field-visibility>
                                </field-visibility>
                                <appearance>
                                </appearance>
                        </form>
                </forms>
        </config>

I have also tried with event="change" (I thought keyup might not be triggered given the actual date field being hidden above the displayed date entry field), and originally I had mandatory="true" in the fields (only changed this trial and error wise on my last restart).

As you can see, I've riddled the js with logger messages, none of which are generated. Earlier on in share-config-custom.xml I have enabled client debugging, and I can see that it appears the normal date and time validation is being run. (although with firebug I can see that the formValidation.js file is loaded properly)
09:52:34 DEBUG - Determining whether submit elements can be enabled…
09:52:34 DEBUG - Validating mandatory state of field 'page_x002e_data-form_x002e_task-edit_x0023_default_prop_mcwf_expiryDate'
09:52:34 DEBUG - Validating mandatory state of field 'page_x002e_data-form_x002e_task-edit_x0023_default_prop_mcwf_reminderDate'
09:52:34 DEBUG - Validating field 'page_x002e_data-form_x002e_task-edit_x0023_default_prop_mcwf_expiryDate-cntrl-date' has a valid date and time
09:52:34 DEBUG - Validating field 'page_x002e_data-form_x002e_task-edit_x0023_default_prop_mcwf_reminderDate-cntrl-date' has a valid date and time

I get the feeling that the issue is this (I have nothing to back this up other than observed behavior): perhaps date fields don't work so great yet with the extensibility side of Shares validation handlers, likely due to what I mentioned above - the fact that the date field is actually a text input, a hidden input and a javascript control all rolled up, rather than a single input like other fields. As a result (perhaps) when a date field is detected to be mandatory, it automatically uses the date-time validation handler, which could well be the only one which works with the field (and even then, it seems most of the work is done by the inline js changing the class of the field, the validation handler just determines if it has the invalid class).

Am I anywhere near the mark here? Can anyone see a way around this, or possibly just something silly that I've missed?

EDIT: further info, I have tested running from the firebug console the following command:
YAHOO.Bubbling.fire("registerValidationHandler",{fieldId: "page_x002e_data-form_x002e_task-edit_x0023_default_prop_mcwf_expiryDate",handler: Alfresco.forms.validation.corporateDates,when: "onchange"}); 
.

When I do so, the validation handler works fine, so at this point I can confirm the only issue is actually adding it to the field. At this point my thoughts are that I will go with the messy solution, defining a custom field template and including script to run the code above when the page loads, but this seems fairly inelegant to me.

Outcomes