Cake PHP - Form in the view index.ctp

Asked

Viewed 169 times

1

I got the views: index.ctp, add.ctp, edit.ctp. Generated via modified Bake and everything, now what I need is in the view index, when you click ADD NEW or EDIT, instead of going to the respective view, just display a modal with the form.

ADD

public function add() {
    if ($this->request->is('post')) {
        $this->Compra->create();
        if ($this->Compra->save($this->request->data)) {
            $this->Session->setFlash(__('The compra has been saved.'), 'default', array('class' => 'alert alert-success'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The compra could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
        }
    }
    $formaPagamentos = $this->Compra->FormaPagamento->find('list', array('fields' => array('FormaPagamento.descricao')));
    $suppliers = $this->Compra->Supplier->find('list', array('fields' => array('Supplier.fantasia')));
    $this->set(compact('formaPagamentos', 'suppliers'));
}

Index

public function index() {
    $this->Compra->recursive = 0;
    $this->Paginator->settings = array('limit' => 20, 'order' => array('Compra.data' => 'DESC'));

    if ($this->request->is('requested')) {
        return $this->paginate();
    } else {
        $this->set('compras', $this->Paginator->paginate());
    }

    $formaPagamentos = $this->Compra->FormaPagamento->find('list', array('fields' => array('FormaPagamento.descricao')));
    $suppliers = $this->Compra->Supplier->find('list', array('fields' => array('Supplier.fantasia')));
    $this->set(compact('formaPagamentos', 'suppliers'));

}

form in the modal

<div class="modal-body">
        <?php echo $this->Form->create('Compra', array('action' => 'add', 'role' => 'form')); ?>

            <div class="form-group">
                <?php echo $this->Form->input('valor', array('class' => 'form-control', 'placeholder' => 'Valor'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('data', array('class' => 'form-control', 'placeholder' => 'Data'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('entrega', array('class' => 'form-control', 'placeholder' => 'Entrega'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('forma_pagamento_id', array('class' => 'form-control', 'placeholder' => 'Forma de Pagamento', 'label' => 'Forma de Pagamento'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('data_pagamento', array('type' => 'text', 'class' => 'form-control', 'placeholder' => 'Data Pagamento'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('supplier_id', array('class' => 'form-control', 'placeholder' => 'Fornecedor', 'label' => 'Fornecedor'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('status', array('options' => array(0 => '[Informe o Status]', 1 => 'Pedido Realizado', 2 => 'Pedido Entregue', 3 => 'Pedido entregue e Pago'), 'class' => 'form-control', 'placeholder' => 'Status do Pagamento'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('obs', array('class' => 'form-control', 'placeholder' => 'Observações...'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->submit(__('Submit'), array('class' => 'btn btn-default')); ?>
            </div>

        <?php echo $this->Form->end() ?>
  </div>
  • 1

    Could you put the html code from this view?

  • Ok, I entered the code. So far I can send the form to the add action, but at the time of saving it gives error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.