Validate db field

Hi,
What I would like is to validate an entry field that must be unique, therefore, when saving, a check is made on the DB, if it already exists, do not save.
Among the various procedures found on the net, I found this:

in
custom / Extension / modules / <MODULE> / Ext / Vardefs / <FILE_NAME> .php

I added this:

$dictionary ["<MODULES>"] ["fields"] ["<ID_FIELD>"] ["validation"] = array (
      'type' => 'callback',
      'callback' => 'function(formname, nameIndex) {if ($ ("#" + nameIndex) .val ()! = 999) {add_error_style(formname, nameIndex, "Only 999 is allowed!"); return false;}; return true;} ',
    ),

That works, but only for a simple string check, as mentioned above, the check must be done on a DB field.
I tried to edit like this:

$dictionary ["<MODULES>"] ["fields"] ["<ID_FIELD>"] ["validation"] = array (
      'type' => 'callback',
      'callback' => "
function showHint() {
var str;
str = $('#' + nameIndex) .val();

  if (str.length == 0) {
    document.getElementById ('<ID_FIELD>') .innerHTML = '';
    return;
  } else {
    var xmlhttp = new XMLHttpRequest ();
    xmlhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById ('<ID_FIELD>') .innerHTML = this.responseText;
      }
    };
    xmlhttp.open ('GET', 'gethint.php? q =' + str, true);
    xmlhttp.send ();
  }
} "
    );

what is entered in the text field is sent to the gethint.php file which does the comparison with the DB field, at least it should work like this, but it doesn’t. The gethint.php file is not called.
Do you have any idea how to fix this please? Thanks

nobody knows how to fix it, please?

@fab
Are you made entry point for gethint.php?
Look at the documentation here:

ok, I created the entrypoint, the fact remains that the gethint file is not called.
If I launch it via url, ok:

site/index?entryPoint=myEntryPoint

but nothing happens from here:

xmlhttp.open('GET', 'gethint.php? q =' + str, true);

I also tried with:

xmlhttp.open('GET', 'site / suitecrm / index? entryPoint = myEntryPoint, true);

but nothing

@fab
If the launch, OK:
site/index?entryPoint=myEntryPoint

I think that code should be:

xmlhttp.open('GET', 'site/index?entryPoint=myEntryPoint, true);

unfortunately there are no errors in suitecrm.log, it just does not load the file and I can’t understand why

I saw the reference to the other post, but it is not useful in my case, the entrypoint works for me, the problem is the recall of the file in:

xmlhttp.open ('GET', 'gethint.php? q =' + str, true);

where the gethint.php file is not called.
I’m still hitting my head and
I replaced the above statement with this:

$ .post ('index.php? entryPoint = test', {name: $ ('# name'). val ()});

where in the entrypoint test there is a call to the gethint file
and in this case it works.
The problems are 2:

  1. I have to pass to the file gethint.php $ (’# name’). Val () (passing it as seen above, it is not read by the $ _REQUEST);
  2. in gethint.php the passed value is retrieved and return the query result

how do i solve these problems? I’m really going crazy

@fab
Perhaps your request should be:

$.post('index.php, {entryPoint:'test',name:$('#name').val()});

Your code with XMLHttpRequest object will be work too:

var str;
str = '&name='$('#name') .val();
xmlhttp.open('GET', 'index.php?entryPoint=test' + str, true);

I missed in your the first post that you called file gethint.php but should called entry point myEntryPoint.

I want to give you small advice. You code will be better read if you will use 3 back quote with name of language before code and 3 back quote after code:
```javascript
…<yourcode>…
```

I have made some progress, now I can call the gethint file passing it the value of the field, in gethint (via $_REQUEST) I also take the passed value:

$.post('index.php?entryPoint=myEntryPoint', {name: str}, function(data){
alert(data);
if (data == 'exists') {

alert(data);
$('#' + nameIndex).innerHTML = this.responseText;
return false;
}
else if (data == 'unique') {

alert(data);
$('#' + nameIndex).innerHTML = '';
return true;
}
});

Now the problem is the return of the data. In gethint (for now, besides the $_REQUEST), I just entered echo ‘exists’, which should be contained in ‘data’, as seen above, but ‘data’ contains nothing.
I inserted the alerts to verify, but everything inside function (data) {…
it looks like it is not running

I managed to solve, placed here the solution for those who have the same problem:

function(formname, nameIndex){
var str;
var str1;

str1 = $('#' + nameIndex).val();
str = '& name =' + $('#' + nameIndex).val();
var msg = 'true';

if(str1.length == 0){
       add_error_style(formname, nameIndex, 'ERROR. Empty field !!!');
       return false;
} else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4){
       if(xmlhttp.status == 200){
             var response = xmlhttp.responseText.trim();

             if(response === 'ok'){
                add_error_style(formname, nameIndex, 'ERROR. name exist !!!');
                msg = 'false';
             }
       }
  }
};

xmlhttp.open('GET', 'index.php?entryPoint=myEntryPoint' + str, false);
xmlhttp.send();

if (msg === 'false')
   return false;
}

} "

The response is a bit slow (not immediate, maybe a second or so, but it shows) but it works