cancel
Showing results for 
Search instead for 
Did you mean: 

validation-handler with ajax

_jan
Champ on-the-rise
Champ on-the-rise
Hi,
Is possible to make validation of input in create form with ajax call ?
I have this code but I am not able to make that return function wait for AJAX response.

Alfresco.forms.validation.checkNumber = function checkNumber(field, args, event, form, silent, message) { 
  //get actual value
  var billYear = field.value;
  var scriptURL = Alfresco.constants.PROXY_URI + "someco/Exists.json?billYear=" + billYear;
  var valid = false;
  // check if exists
  Alfresco.util.Ajax.jsonGet({
      url: scriptURL,
      scope: this,
      successCallback: {
         fn: function handleInfo(obj) {
            if(obj.json.result.toString() == "true") {        
               valid = false;
            } else {
               valid = true;
            }
            return valid;
         },
         scope: this
      },
      failureCallback: {
         fn: function(obj){
            valid = false;
         },
         scope: this
      }
   });
  return valid; 
};
5 REPLIES 5

aaditvmajmudar
Confirmed Champ
Confirmed Champ
Hi,

This will be asynchronous ajax call so yes return statement will not wait for the response of your call.

One alternative approach is to wait for the response of your ajax call by setTimeout.

You will check after each 1 sec for response of call and when you get the response, return.

Hope this helps.

Hi,

That is not working, because if I use
setTimeout
and in that function return
true
,
checkNumber(){}
is acting like it has no return value so it return
false
.
I am thinking about validating this value in custom text field after
onchange()
event, but I am not sure if it is good aproach.

When I write this, validator return false:
Alfresco.forms.validation.checkNumber = function checkNumber(field, args, event, form, silent, message) {
   setTimeout(function(){ return true;}, 1000);
};


Ján

aaditvmajmudar
Confirmed Champ
Confirmed Champ
You need to have one flag and that need to be set in your success AND failure method of AJAX call and in setTimeout function, you will check that if I am having that flag value set or not.

If yes then you write return statement otherwise nothing.

Can you try this?

I tried that and result was same. When it should return nothing or do nothing, it return
false
. Probably I will solve it by different way. I will add new atribute to that input which will be validated. This atribute contain restricted values and this values will be reffiled at time of creation of input and at keyup event. Then validator will only parse this atribute and validate by that.

mdavid_cu
Champ in-the-making
Champ in-the-making
The following example is an implementation of a custom ajax validator example over the keyup event.
Configuring the
share-config-custom.xml

<ol>
<li> Add new js file for custom validations</li>
[blockcode]
<config>
    <forms>
      <dependencies>
          <js src="/someco/custom-form-validators.js"/>
        </dependencies>
    </forms>
  </config>
[/blockcode]

<li>Add validator as a constraint handler in a form appearence field configuration</li>
[blockcode]
<field id="sco:field">
  <constraint-handlers>
    <constraint
        type="custom" 
        validation-handler="Alfresco.forms.validation.ajaxValidator"
        event="keyup"
        message-id="sco.invalid"/>
    </constraint-handlers>
</field>
[/blockcode]
<li>Implement the ajax validator in the custom file</li>
[javascript]
Alfresco.forms.validation.ajaxValidator = function(field, args, event, form, silent, message) {
 

  var
    // Always assume a valid field
    valid = true,
    url = Alfresco.constants.PROXY_URI + 'someco/endpoint';

  // remove any invalid class
  YAHOO.util.Dom.removeClass(field, 'invalid');

  Alfresco.util.Ajax.jsonGet({
      url: url,
      dataObj: {
        param1: 'val1',
        param2: 'val2'
      },
      successCallback: {
        fn: function(resp) {

          //if(resp.json.isValid){

            //invalidate the form
            form._valid = false;

            //make the field invalid with css
            YAHOO.util.Dom.addClass(field, 'invalid');
           
            // update submit elements state, if required
            if (form.showSubmitStateDynamically) {
              form._toggleSubmitElements(this._valid);
            }

            //Configuring message over the field
            field.setAttribute("title", msg);
            field.setAttribute(form._VALIDATION_MSG_ATTR, msg);

          // }
        });
    }
  });

  return valid;
};
[/javascript]