简体   繁体   中英

Cakephp Input Date to Database

I'm trying to take a date from the user and I can't get my date validation to pass. I'm not sure what I'm doing wrong.

//model code
public $validate = array(
    'expiry' => array(
        'rule' => array('date', 'ymd'),
        'message' => 'Invalid date format'
    )
);

// controller
public function add() {
    if ($this->request->is('post')) {
        $this->Entry->create();

        if ($this->Entry->save($this->request->data)) {
            $this->Session->setFlash(__('Your contest entry has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your entry.'));
    }
}

// view code
echo $this->Form->input('expiry', array('type' => 'date', 'dateFormat' => 'YMD'));

Can anyone tell me what I'm missing?

I got this working. Mostly my solution was to add this in my Model:

function beforeSave($options = array())
{
    // covert the expiry date string to a valid format for the database
    $this->data['Entry']['expiry'] = 
        date('Y-m-d H:i:s',
            strtotime(
                $this->data['Entry']['expiry']));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM