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

¿BeforeSave not receive data before saving?

I want to validate before saving if the variable "$ CONT_CEDULA" meets the requirements otherwise not be saved. But when saving is as if the variable "$ CONT_CEDULA" not send data. I want to know if I'm doing well or some other event or function. The "echo" Initial no data

public function beforeSave() {

        echo $this->$CONT_CEDULA;
        switch (strlen($this->$CONT_CEDULA)) {
        case 10:
            return validarCI($this->$CONT_CEDULA);
            break;
        case 13:
            return validarRUC($this->$CONT_CEDULA);
            break;
        default:
           echo "Numero de caracteres invalidos"  ;
            return FALSE;
    }

And where you executing save ? Can you show it ? And also data which you passing to save method ?



81.2k

This is the save method, was created by the developer tools.

public function createAction()
{
    if (!$this->request->isPost()) {
        return $this->dispatcher->forward(array(
            "controller" => "spm_contacto",
            "action" => "index"
        ));
    }
    //Here if the content of the variable is displayed
    echo $this->request->getPost("CONT_CEDULA");
    $formato = 'Y-m-d';
    $fechanacimiento = Datetime::createFromFormat($formato, $this->request->getPost("CONT_FECNACI")); 
    echo $fechanacimiento->format('d/m/Y');
    $fechainscripcion = Datetime::createFromFormat($formato, $this->request->getPost("CONT_FECINSC"));

    $SPM_CONTACTO = new SpmContacto();

    $SPM_CONTACTO->CONT_CODIGO = $this->request->getPost("CONT_CODIGO");
    $SPM_CONTACTO->CONT_CEDULA = $this->request->getPost("CONT_CEDULA");
    $SPM_CONTACTO->CONT_RUCIDE = $this->request->getPost("CONT_RUCIDE");
    $SPM_CONTACTO->CONT_NOMBRE = $this->request->getPost("CONT_NOMBRE");
    $SPM_CONTACTO->CON_ESTADO = $this->request->getPost("CON_ESTADO");
    $SPM_CONTACTO->CONT_TELEFO = $this->request->getPost("CONT_TELEFO");
    $SPM_CONTACTO->CONT_DIRECC = $this->request->getPost("CONT_DIRECC");
    $SPM_CONTACTO->CONT_AREA = $this->request->getPost("CONT_AREA");
    $SPM_CONTACTO->CONT_CARGO = $this->request->getPost("CONT_CARGO");
    $SPM_CONTACTO->CONT_TIPOXX = $this->request->getPost("CONT_TIPOXX");
    $SPM_CONTACTO->CONT_EMAIL = $this->request->getPost("CONT_EMAIL");
    $SPM_CONTACTO->CONT_USUARIO = $this->request->getPost("CONT_USUARIO");
    $SPM_CONTACTO->CONT_CLAVE = $this->request->getPost("CONT_CLAVE");
    $SPM_CONTACTO->CONT_CLAVEE = sha1($this->request->getPost("CONT_CLAVEE"));
    $SPM_CONTACTO->CONT_FECNACI = $fechanacimiento->format('d/m/Y H:i:s');
    $SPM_CONTACTO->CONT_FECINSC = $fechainscripcion->format('d/m/Y H:i:s');   
    $SPM_CONTACTO->CONT_TIPOCODIGO = $this->request->getPost("CONT_TIPOCODIGO");

    if (!$SPM_CONTACTO->save()) {
        foreach ($SPM_CONTACTO->getMessages() as $message) {
            $this->flash->error(
                     "<div class='alert callout round' data-closable>"
                           . " <h5>Esto es Importante!</h5>"
                           . " <button class='close-button' aria-label='Dismiss alert' type='button' data-close>"
                           . "<span aria-hidden='true'>&times;</span> </button>"
                           . " <p>El referencia ". $message."no fue encontrado</p></div>"
                    );
        }

        return $this->dispatcher->forward(array(
            "controller" => "spm_contacto",
            "action" => "new"
        ));
    }

    $this->flash->success("<div class='success callout round' data-closable>"
                           . " <h5>Esto es Importante!</h5>"
                           . " <button class='close-button' aria-label='Dismiss alert' type='button' data-close>"
                           . "<span aria-hidden='true'>&times;</span> </button>"
                            . " <p>El contacto fue creado correctamente.</p></div>");

    return $this->dispatcher->forward(array(
        "controller" => "spm_contacto",
        "action" => "index"
    ));
}


81.2k

THIS IS THE COMPLETE MODEL

    <?php

class SpmContacto extends \Phalcon\Mvc\Model {

public $CONT_CODIGO;
public $CONT_CEDULA;
public $CONT_RUCIDE;
public $CONT_NOMBRE;
public $CON_ESTADO;
public $CONT_TELEFO;
public $CONT_DIRECC;
public $CONT_AREA;
public $CONT_CARGO;
public $CONT_TIPOXX;
public $CONT_EMAIL;
public $CONT_USUARIO;
public $CONT_CLAVE;
public $CONT_CLAVEE;
public $CONT_FECNACI;
public $CONT_FECINSC;
public $CONT_TIPOCODIGO;

/**
 * Initialize method for model.
 */
public function initialize() {
    $this->setSchema("SPOLS");
    $this->hasMany('CONT_CODIGO', 'SPMREFERENCIA', 'CONT_CODIGO', array('alias' => 'SPMREFERENCIA'));
    $this->hasMany('CONT_CODIGO', 'SPTDETALLE', 'CONT_CODIGO', array('alias' => 'SPTDETALLE'));
    $this->hasMany('CONT_CODIGO', 'SPTENCABEZADO', 'CONT_CODIGO', array('alias' => 'SPTENCABEZADO'));
}

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 == 8)
        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;
    }
}

public function beforeSave() {

    echo $this->$CONT_CEDULA;
    switch (strlen($this->$CONT_CEDULA)) {
        case 10:
            return validarCI($this->$CONT_CEDULA);
            break;
        case 13:
            return validarRUC($this->$CONT_CEDULA);
            break;
        default:
           echo "Numero de caracteres invalidos";
         return FALSE;
    }

     //echo $op;
}

public function getSource() {
    return 'SPM_CONTACTO';
}

public static function find($parameters = null) {
    return parent::find($parameters);
}

public static function findFirst($parameters = null) {
    return parent::findFirst($parameters);
}

}

Check what $this->request->getPost("CONT_CEDULA") return and refractor your code beacause right now it looks super bad.



81.2k
edited May '16

This code is generated by the developer tools. Ok I'll check. if data but not receive them at that time as the model



81.2k
Accepted
answer

The problem was the sign $ in the $ this-> CONT_CEDULA; It worked

public function beforeSave() {

    echo $this->CONT_CEDULA;
    switch (strlen($this->CONT_CEDULA)) {
    case 10:
        return validarCI($this->CONT_CEDULA);
        break;
    case 13:
        return validarRUC($this->CONT_CEDULA);
        break;
    default:
       echo "Numero de caracteres invalidos"  ;
        return FALSE;
}

Well i missed it. Your previous code $this->$CONT_CEDULA meant that you access field from $this where field name is value of $CONT_CEDULA.

Also try to really refractor yor code, right now it look pretty bad. For example:

        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;
            }
        }

You can move this to seperate method beacause you have this in two places.

 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;
        }

modulo ?

    $SPM_CONTACTO->CONT_CODIGO = $this->request->getPost("CONT_CODIGO");
    $SPM_CONTACTO->CONT_CEDULA = $this->request->getPost("CONT_CEDULA");
    $SPM_CONTACTO->CONT_RUCIDE = $this->request->getPost("CONT_RUCIDE");
    $SPM_CONTACTO->CONT_NOMBRE = $this->request->getPost("CONT_NOMBRE");
    $SPM_CONTACTO->CON_ESTADO = $this->request->getPost("CON_ESTADO");
    $SPM_CONTACTO->CONT_TELEFO = $this->request->getPost("CONT_TELEFO");
    $SPM_CONTACTO->CONT_DIRECC = $this->request->getPost("CONT_DIRECC");
    $SPM_CONTACTO->CONT_AREA = $this->request->getPost("CONT_AREA");
    $SPM_CONTACTO->CONT_CARGO = $this->request->getPost("CONT_CARGO");
    $SPM_CONTACTO->CONT_TIPOXX = $this->request->getPost("CONT_TIPOXX");
    $SPM_CONTACTO->CONT_EMAIL = $this->request->getPost("CONT_EMAIL");
    $SPM_CONTACTO->CONT_USUARIO = $this->request->getPost("CONT_USUARIO");
    $SPM_CONTACTO->CONT_CLAVE = $this->request->getPost("CONT_CLAVE");
    $SPM_CONTACTO->CONT_CLAVEE = sha1($this->request->getPost("CONT_CLAVEE"));
    $SPM_CONTACTO->CONT_FECNACI = $fechanacimiento->format('d/m/Y H:i:s');
    $SPM_CONTACTO->CONT_FECINSC = $fechainscripcion->format('d/m/Y H:i:s');   
    $SPM_CONTACTO->CONT_TIPOCODIGO = $this->request->getPost("CONT_TIPOCODIGO");

Instead of doing this you can just do:

$post = $this->request->getPost();
$whiteList = //Some method from our object which will return whitelist array
if (!$SPM_CONTACTO->save($post, $whiteList)) {
// rest of code
}

Also you should create and format date in beforeValidationOnSave method in your model.