кнопка отмены сохраняет пустую строку в db cakephp

0

Я изучаю концепции Cakephp, ниже приведен код для моего контроллера в cakephp, сохранение происходит успешно, но отмена добавляет пустую строку в базу данных.

<?php
   echo $this->Form->create('Customer');
    echo $this->Form->input('customer_name');
    .....
    echo $this->Form->button(
            'Save', 
            array('class' => 'button save')
        );

    echo $this->Form->button(
            'Cancel', 
            array('class' => 'button cancel')
        );

    echo $this->Form->end();

?>

И ниже моя форма представления

<?php
public function add() {
    if ($this->request->is('post')) {

        $this->Customer->create();
        if ($this->Customer->save($this->request->data)) {
            $this->Session->setFlash(__('Your customer has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        elseif ($this->Customer->cancel('')) {
            $this->Session->setFlash(__('Customer info has not been saved'));
            return $this->redirect(array('action' => 'index'));
        }
    }

}

?>
Теги:
cakephp
cancel-button

1 ответ

1
Лучший ответ

Вы создаете кнопку в своем представлении. В Cakephp тег кнопки действует как кнопка отправки. Поэтому вам нужно либо создать ссылку (перенаправлять на предыдущий сайт):

$this->Html->link('Cancel', array('controller' => 'customer', 'action' => 'index'));

или не позволяя кнопке отправить форму, например, через javascript (jQuery):

$('.button cancel').on('click', function(e)){
    e.preventDefault();
}

Согласно этому сайту есть еще один вариант:

echo $this->Form->button(
        'Cancel', 
        array('type' => 'button', 'class' => 'btn button cancel')
);

Ещё вопросы

Сообщество Overcoder
Наверх
Меню