Search code examples
javascriptalfrescoalfresco-share

Alfresco Form field validation Javascript not being loaded by the browser


I'm hoping someone here can help out with this problem, because I've done everything as per the book, to create a Javascript function for validating an Alfresco form field. Here's what I've done:

Altered the share-config-custom.xml file, as follows:

<forms>
  <dependencies>
    <js src="js/date-range-validator.js" />
  </dependencies>

  <form>
    <appearance>
      <field id="myfield:dfc-start-date">
        <constraint-handlers>
          <constraint type="MANDATORY" validation-handler="myspace.forms.validation.checkDateRangeValidity" event="keyup"/>
        </constraint-handlers>
      </field>
    ...
  </form>  
</forms>

Created a date-range-validator.js file, which I placed in src/main/assembly/web/js

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

myspace.forms.validation.checkDateRangeValidity = function checkDateRangeValidity(field, args, event, form, silent, message) {
  var valid = true;
  alert('Checked date ' + field);
  return valid;
}

However, when the form is displayed I get an exception because "myspace" isn't defined, so it looks like my validator code isn't being loaded. I've tried placing the Javascript file in a variety of locations within the project, but I never see my Javascript validation code in the browser.

So I suppose it's a question of where to place the Javascript within the project structure.


Solution

  • If anyone is interested, I got a solution thanks to Jeff Potts, who suggested that I put the form definition into an extension module. Details on the Alfresco forum.

    https://community.alfresco.com/thread/240434-form-field-validation-handler-issue

    <extension>
      <modules>
        <module>
            <id>Someco Share Form Configuration</id>
            <version>1.0</version>
            <auto-deploy>true</auto-deploy>
            <configurations>
    
                <config>
                    <forms>
                        <dependencies>
                            <js src="/resources/someco-share/js/validator.js"/>
                        </dependencies>
                    </forms>
                </config>
    
    
    
                <config evaluator="node-type" condition="sc:doc">
                    <forms>
                        <form>
                            <field-visibility>
                                <show id="cm:name" />
                                <show id="cm:title" force="true" />
                                <show id="sc:someProp" />
    
                            </field-visibility>
                            <appearance>
                                <field id="cm:name">
                                    <control>
                                        <control-param name="maxLength">255</control-param>
                                    </control>
                                </field>
                                <field id="cm:title">
                                    <control template="/org/alfresco/components/form/controls/textfield.ftl" />
                                </field>
                                <field id="sc:someProp" label-id="form.field.label.sc.someProp" description-id="form.field.description.sc.someProp" help-id="form.field.help.sc.someProp">
                                    <constraint-handlers>
                                        <constraint type="scDateValidator" message-id="constraint.scDateValidation" validation-handler="SomeCo.forms.validation.scSomePropValidatior" event="onChange"/>
                                    </constraint-handlers>
                                </field>
                            </appearance>
                        </form>
    
                    </config>
    
                </configurations>
            </module>
        </modules>
      </extension>