cancel
Showing results for 
Search instead for 
Did you mean: 

Form field validation handler issue

afielden
Confirmed Champ
Confirmed Champ

I'm hoping someone here can help out with this problem, because I've done everything as per the book, to use a Javascript function to validate a 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 fbs == "undefined" || !fbs) {
  var fbs = {};
}

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 the problem still persists.

12 REPLIES 12

I do indeed have the config element in the XML.

Thanks for the alternate solution suggestion. Do you happen to have a link to your code please?

jpotts
World-Class Innovator
World-Class Innovator

Okay, thanks for checking.

Alternative solution is to create an xml file under src/main/resources/alfresco/web-extension/site-data/extensions. Name it whatever you want, like validator.xml.

In that file you would put something like:

<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>

That is just awesome Jeff, you are a superstar! Using this approach, with minor modifications, the Javascript validator function is now visible in the browser, and is actually being called by my form. You're very welcome to post this answer up on SO, and I'll give you the rep points.

Thanks also to Sanjay Bandhniya for contributing.

Many thanks, I've been banging my head against this brick wall for days. I'd still love to know why the original config approach wasn't working.