📣 We're Hiring!

Validation of a field: edit an existing record

Hi everyone,
I created the validation of a field as described like this:

  1. in custom/Extension/modules/“Module”/.php
    I added

     $dictionary["Module"]["fields"]["field"]["validation"] = array (
           'type' => 'callback',
           'callback' => "
     					function(formname,nameIndex) {						
     						var str1;   							
     						str1=$('#' + nameIndex).val();
     				        name='&name='+$('#' + nameIndex).val();
     												
     						var return_value='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=='found'){
     										add_error_style(formname,nameIndex,'ERROR. name already present!');
     										return_value='false';
     									}								
     								  }
     							  }
     							};
     							xmlhttp.open('GET', 'index.php?entryPoint=entryPointName' + name + id_yard, false);
     							xmlhttp.send();
     							
     							if(return_value=='false'){
     								return false;
     							}
     						  }
     					}"	  
     	  
     );
    
  2. I created an entrypoint (entryPointName)

  3. the entrypoint refers to a file (getHint.php)
    in this file there is the query (in SQL) to check the existence of the name I’m going to insert.

Everything works perfectly, at least for the insertion of new records, but if I open a record already created to make changes, trying to save it, the check is performed on the field of this record and tells me that the name is already present.

How can I solve this problem? Any ideas? Thanks

send the ID of the record as param too and add an exception where the name search should ignore that ID

Yes, that’s what I had thought of, but I can’t reach the id.
alert($(’#id’).val());
alert(id);
return empty values.

Also, I tried to bypass the check and it’s as if the record to be modified is a new record, it doesn’t update, but tries to create a new one

Can anyone give me an idea? I am still trying to solve this problem.

  1. I can’t get the id of the one I’m inserting to do a comparison (the idea is: I’m doing a new insertion, so I don’t have the id of the current record, and I do the check; if I have an id it means I’m editing, so it exists and I don’t have to do the check)
  2. even if I could get it, in case the name (which is the object of the validation) is changed, the check would not be done