2017-10-20 62 views
1

我需要从表单传递数据。我有多年的下拉从视图中进行选择:他们使用FormHelper将CakePHP 2.x中的日期保存为空

echo $this->Form->input('Zona.agno', array(
    'label'   => 'Año', 
    'type'   => 'year',  
    'minYear'  => date('Y') , 
    'maxYear'  => date('Y') + 4, 
    'orderYear'  => 'asc', 
    'dateFormat' => 'Y',   
    'empty' => '--Seleccione Año--' 
)); 

上面显示,但是当我点击提交,调试告诉我,该数据是空的。

在数据库中,数据类型为date

控制器是:

if ($this->request->is('post')) { 
    $this->request->data('Zona.activo', 1); 
    if ($this->Zona->save($this->request->data)) { 
     $this->mensaje(__('La Zona ha sido creada.')); 
     $this->redirect(array('action' => 'index')); 
    } else { 
     $this->mensaje(__('La Zona no ha sido creada, por favor verifica los datos.'), 'error'); 
    } 
} 

的误差修改为:

$this->validationErrors(array) 

Zona(array) 
agno(array) 
0 Debe ingresar año que aplica 

回答

0

你的问题是,你正在尝试一个不完整的日期保存到date领域在数据库中,并且因为它是无效的,ORM将该字段设置为NULL。

你这里有两种选择:

  • 更改为smallint字段类型在数据库中,这可能是它应该是从一开始就考虑agno(AÑO)转换为year

  • 添加其他两个领域,所以这三个出示有效日期:

    <? echo $this->Form->input('Contact.birthdate', array('type' => 'month')); ?>   
    <? echo $this->Form->input('Contact.birthdate', array('type' => 'day')); ?> 
    

    ,不过也许这并不是你所追求的。

+0

改变数据库中的字段,但是你还是不救它 – frtgu34

+0

工作的第二个选项,如果我拿到的话蛾的数据,但保养得很好,非常感谢你 – frtgu34