We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Add function when losing focus or save

Add function when losing focus or save

I would like to add this function to validate the ID or Ruc a person but previously did it with javascript, but I get error Phalcon and honestly I can not think where to put it. I would like to put it when saving or when you lose focus if possible

<?php
function validarCI($strCedula)
{
$suma = 0;
   $strOriginal = $strCedula;
$intProvincia = substr($strCedula,0,2);
   $intTercero = $strCedula[2];
$intUltimo = $strCedula[9];
if (! settype($strCedula,"float")) return FALSE;
if ((int) $intProvincia < 1 || (int) $intProvincia > 23) return FALSE;
if ((int) $intTercero == 7 || (int) $intTercero == <img src="/modules/smileys/packs/example/cool.png" title="Cool"      alt="Cool" /> return FALSE;
   for($indice = 0; $indice < 9; $indice++) {
     //echo $strOriginal[$indice],'</br>';
 switch($indice) {
    case 0:
    case 2:
    case 4:
    case 6:
    case 8:
       $arrProducto[$indice] = $strOriginal[$indice] * 2;
       if ($arrProducto[$indice] >= 10) $arrProducto[$indice] -= 9;
       //echo $arrProducto[$indice],'</br>';
       break;
    case 1:
    case 3:
    case 5:
    case 7:
       $arrProducto[$indice] = $strOriginal[$indice] * 1;
       if ($arrProducto[$indice] >= 10) $arrProducto[$indice] -= 9;
       //echo $arrProducto[$indice],'</br>';
       break;
 }   }
foreach($arrProducto as $indice => $producto) $suma += $producto;
$residuo = $suma % 10;
$intVerificador = $residuo==0 ? 0: 10 - $residuo;
return ($intVerificador == $intUltimo? TRUE: FALSE);    }

function validarRUC($strRUC){
if (strlen($strRUC) != 13) return FALSE;
$suma = 0;
$strOriginal = $strRUC;
$intProvincia = substr($strRUC,0,2);
$intTercero = $strRUC[2];
if (! settype($strRUC,"float")) return FALSE;
if ((int) $intProvincia < 1 || (int) $intProvincia > 23) return FALSE;
if ((int) $intTercero != 6 && (int) $intTercero != 9) {
   if (substr($strRUC,10,3) == '001') return validarCI(substr($strRUC,0,10));
      return FALSE;
}
if ((int) $intTercero == 6) {
  $intUltimo = $strOriginal[8];
  for($indice = 0; $indice < 9; $indice++) {
     //echo $strOriginal[$indice],'</br>';
     switch($indice) {
        case 0:
           $arrProducto[$indice] = $strOriginal[$indice] * 3;
           break;
        case 1:
           $arrProducto[$indice] = $strOriginal[$indice] * 2;
           break;
        case 2:
           $arrProducto[$indice] = $strOriginal[$indice] * 7;
           break;
        case 3:
           $arrProducto[$indice] = $strOriginal[$indice] * 6;
           break;
        case 4:
           $arrProducto[$indice] = $strOriginal[$indice] * 5;
           break;
        case 5:
           $arrProducto[$indice] = $strOriginal[$indice] * 4;
           break;
        case 6:
           $arrProducto[$indice] = $strOriginal[$indice] * 3;
           break;
        case 7:
           $arrProducto[$indice] = $strOriginal[$indice] * 2;
           break;
        case 8:
           $arrProducto[$indice] = 0;
           break;
     }
  }
}
else {
  $intUltimo = $strOriginal[9];
  for($indice = 0; $indice < 9; $indice++) {
     //echo $strOriginal[$indice],'</br>';
     switch($indice) {
        case 0:
           $arrProducto[$indice] = $strOriginal[$indice] * 4;
           break;
        case 1:
           $arrProducto[$indice] = $strOriginal[$indice] * 3;
           break;
        case 2:
           $arrProducto[$indice] = $strOriginal[$indice] * 2;
           break;
        case 3:
           $arrProducto[$indice] = $strOriginal[$indice] * 7;
           break;
        case 4:
           $arrProducto[$indice] = $strOriginal[$indice] * 6;
           break;
        case 5:
           $arrProducto[$indice] = $strOriginal[$indice] * 5;
           break;
        case 6:
           $arrProducto[$indice] = $strOriginal[$indice] * 4;
           break;
        case 7:
           $arrProducto[$indice] = $strOriginal[$indice] * 3;
           break;
        case 8:
           $arrProducto[$indice] = $strOriginal[$indice] * 2;
           break;
     }
  }
}
foreach($arrProducto as $indice => $producto) $suma += $producto;
 $residuo = $suma % 11;
$intVerificador = $residuo==0 ? 0: 11 - $residuo;
//echo "$intVerificador == $intUltimo";
return ($intVerificador == $intUltimo? TRUE: FALSE);
}

function validarID($strId)
{
switch(strlen($strId)) {
  case 10:
     return validarCI($strId);
     break;
  case 13:
     return validarRUC($strId);
     break;
  default:
     return FALSE;
}
}
?>


77.7k
Accepted
answer

Knowing the error message reported by Phalcon would be helpful, I don't think anyone will take the time to debug your code.

Phalcon models have built-in validation logic, you should use it: https://docs.phalcon.io/en/latest/reference/models.html#implementing-a-business-rule

If you want to validate HTML forms on the fly (when an input loses focus), you'll have to send an AJAX request to the server for evaluation.



81.2k

Thank you very much, then I will try to implement ajax certainly are other post passed on encryption not if I can help