timkl
timkl

Reputation: 3339

Form doesn't submit data

I am currently playing around with CakePHP and I'm having a problem with a form that doesn't submit data to my database. There's no error message and when I debug, the form data is seems to be added to the array (see attached image).

This is my model:

class Split extends AppModel {

public $validate = array(
    'title' => array('rule' => 'notEmpty'),
    'url' => array('rule' => 'notEmpty'),
    'descr' => array('rule' => 'notEmpty')
);

And this is my view:

<h1>Add New Case</h1>

<?php
pr($this->request->data);
echo $this->Form->create('Split');
echo $this->Form->input('title');
echo $this->Form->input('url');
echo $this->Form->input('descr', array('rows' => '2'));
echo $this->Form->end('Add New Case');
?>

This is the add function in the Controller:

public function add() {

    if ($this->request->is('split')) {

        $this->Split->create();
        if ($this->Split->save($this->request->data)) {
            $this->Session->setFlash('Your case has been saved.');
            $this->redirect(array('action' => 'index'));

        } else {
            $this->Session->setFlash('Unable to add case.');
        }
    }

}

After pressing submit data is inserted into the Split array, but the form is not submitted:

After pressing submit

This is the HTML that is rendered out:

<form action="/uilab/splits/add" id="SplitAddForm" method="post" accept-charset="utf-8">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>

<div class="input text required">

    <label for="SplitTitle">Title</label>

    <input name="data[Split][title]" maxlength="255" type="text" value="Some title" id="SplitTitle"></div>

    <div class="input text required">
        <label for="SplitUrl">Url</label>
        <input name="data[Split][url]" maxlength="255" type="text" value="http://www.someurl.com" id="SplitUrl">
    </div>

    <div class="input textarea required">
        <label for="SplitDescr">Descr</label>
            <textarea name="data[Split][descr]" rows="2" cols="30" id="SplitDescr">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</textarea>
        </div>

    <div class="submit"><input type="submit" value="Add New Case"></div>

</form>

I'm pretty new to CakePHP so any ideas on how to troubleshoot this is highly appreciated.

Oh, and happy new year btw! :)

Upvotes: 1

Views: 353

Answers (1)

Ross
Ross

Reputation: 17967

if ($this->request->is('split')) {

should be

if ($this->request->is('post')) {

Read about request handling.

The request will never be split so the code is never being executed. Valid values are post, get, put...

Upvotes: 2

Related Questions