Rule in model to format date

1

I have two rules in two fields.

In the data_expenditure field, I want to get the form d/m/a and pass a-m-d . It turns out that when saved in the database, it stays as 0000-00-00 . I do not know how to debug in the model to see how it is transforming and in the controller, when debugging $this->request->data ) does not get validated.

The valor_despesa field is ok. You are changing the "," to ".". The problem is only in the formatting of data_despesa .

What's wrong?

Follow the model code:

<?php

class Despesa extends AppModel {

    public $name='despesa';
    public $useTable='despesas';
    public $primaryKey='id_despesa';

    public $validate=array(
'data_despesa'=>array(  
    'data'=>array(
'rule'=>array('data'))),

'valor_despesa'=>array( 
'preco'=>array(
'rule'=>array('preco'))));

    public function data($check) {
        $dataDespesa=explode("/", $check['data_despesa']);

        $dataDaDespesa="";

        $dataDaDespesa=$dataDespesa[2]."-".$dataDespesa[1]."-".$dataDespesa[0];

        return true;
    }  

    public function preco($check) {
        $valorDespesa=str_replace(",", ".", $check['valor_despesa']);

        return true;
    }

}
?>
    
asked by anonymous 19.11.2015 / 13:44

1 answer

2

To change the date format of a DateTime object that cake probably uses, use the format() method.

Change:

public function data($check) {
    $dataDespesa=explode("/", $check['data_despesa']);
    $dataDaDespesa="";
    $dataDaDespesa=$dataDespesa[2]."-".$dataDespesa[1]."-".$dataDespesa[0];
    return true;
} 

To:

public function data($check) {
    $dataDespesa = $check['data_despesa']->format('Y-m-d');
    return true;
} 

I do not know the cake, maybe it is necessary to change the return true to return $dataDespesa .

How to format monetary values in PHP see this answer

    
19.11.2015 / 18:04