Execute Query in Client Side Form_CustomValidate with Custom API

Greetings,
I want to apply a validation in form_customValidate, in which I execute a query to the database to know the current value of a field and compare it against the value selected by the user.i try this:

var $row = $(this).fields();
var $selected= $row['period'].val();
var $updated = "";

    	$.get(ew.getApiUrl(["currentVacationPeriod", $row['employeeCode'].val()]), function(res) {
        	if (res){
        		//alert(res);
        		//console.log(res);        		
        		$updated = res;   //it must be the Currect Period Value
        		 ew.alert(updated); //blank message
            }
        });

    if ($selected !== $updated) {
        	ew.alert("The period selected must be de older", "", "danger");
    		$('#r_period').focus();
    		return this.addCustomError('period', 'The period selected must be de older'); // Return false if invalid        	
        }

But the query shows nothing.Api_Action:

    //Get the older period vacation
    $app->get('/currentVacationPeriod/{employeeCode}', function ($request, $response, $args) {
        $code = $args["employeeCode"] ?? null; // Get the input value
        if ($code !== null) {
            $response = $response->write(ExecuteScalar("SELECT MIN(period) as period FROM vvacations_period_days WHERE employeeID = '" . AdjustSql($code) . "'")); // Output field value as string            
        }
        return $response; // Return Psr\Http\Message\ResponseInterface object 
    });

How can i do the query comparation??Thanks

You have wrong code: (Press F12 in your browser and go to the Console panel to check for JavaScript errors)onoboa wrote:

$updated = res; //it must be the Currect Period Value
ew.alert(updated); //blank message // ← > $update > vs. > updated

if ($selected !== $updated) { // If you meant to compare the value returned from the server (> $updated> ), you need to put your code inside callback for the > $.get()> , otherwise > $updated > is always > “”

$(‘#r_period’).focus(); // Are you sure a row can be focused?

You may debug by, eg.

console.log($row);
...
console.log(res);
// etc.

You may also press F12 in your browser and go to the Network panel to check:

Thanks for your answer, I will check my code.