cancel
Showing results for 
Search instead for 
Did you mean: 

Form fields validation

darkredd
Star Contributor
Star Contributor
Hello all,

I am trying to implement a custom form field validation, I have followed two tutorial along side studying the alfresco wiki and its related documentation; however I have not been able to achieve my objective.
1. http://blog.mwrobel.eu/field-validation-alfresco-share-form/
2. http://stackoverflow.com/questions/20710883/alfresco-share-forms-how-to-define-a-custom-validation-h...

Here is my js code which I am sure the function works:

if(typeof Mynamespace == "undefined" || !Mynamespace )
{
   var Mynamespace = {};
}

Mynamespace.forms.validation.checkEmailValidity = function checkEmailValidity(field, args, event, form, silent, message)
{
   var valid = true;

   valid = YAHOO.lang.trim(field.value).length !== 0;
   
   var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
   
   if(valid)
   {
      valid = re.test(field.value);
   }
   return valid;
}

I minified the script and place both the scripts in the "\webapps\share\components\form\" folder.
I have added the script to the form dependencies file "form.dependencies.inc".

And my form config:

<field id="my:field">
<constraint-handlers>
  <!– validation-handler param: js function name –>
  <constraint type="MANDATORY" validation-handler="Mynamespace.forms.validation.checkEmailValidity" event="keyup"/>
</constraint-handlers>
</field>


I am still unable to validate my field, what have I missed?

Help much appreciated.
3 REPLIES 3

tonyrivet
Champ in-the-making
Champ in-the-making
You should add your script as a dependency in your form configuration xml file like this (instead of using "form.dependencies.inc") :

<config>
   <forms>
      <dependencies>
         <js src="/path/to/your/script.js" />
      </dependencies>
   </forms>
</config>

darkredd
Star Contributor
Star Contributor
Hi Tony,

Thanks for the reply.
I tried your suggestion but now I get the red message "properties can not be display" when I try to edit the metadata.
Here is my dependencies injection:

   <config>
      <forms>
         <dependencies>
            <js src="${url.context}/res/js/email-validation.js" />
         </dependencies>
      </forms>
   </config>

This snippet is in my custom share config.
What else am I missing?

tonyrivet
Champ in-the-making
Champ in-the-making
You don't need to prefix your path by "${url.context}/res" here. This is done for you in form.js.ftl.
Your dependencies should look like this :

<config>
   <forms>
      <dependencies>
         <js src="/js/email-validation.js" />
      </dependencies>
   </forms>
</config>