Arak Tai'Roth
Arak Tai'Roth

Reputation: 408

Form elements not being submitted

So I've got a weird problem that I'm having a hard time figuring out. I've got a simple form with a few elements that are not being submitted, all of these elements have only one thing in common, they're select elements:

            echo $this->Form->control("spirit_type_id", [
                "label" => false,
                "type" => "select",
                "options" => $spirit_types,
                "empty" => "Spirit Type"
            ]);

            echo $this->Form->control("country_id", [
                "label" => false,
                "type" => "select",
                "options" => $countries,
                "empty" => "Country"
            ]);

            echo $this->Form->control("region_id", [
                "label" => false,
                "type" => "select",
                "options" => $regions,
                "empty" => "Region"
            ]);

And in my controller I have:

public function add() {
    $spirit = $this->Spirits->newEntity();
    $spirit_types = $this->Spirits->SpiritTypes->find("list");
    $countries = $this->Spirits->Countries->find("list");
    $regions = $this->Spirits->Regions->find("list");

    if ($this->request->is("post")) {
        debug($this->request->getData());
        die();
        $spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
        $spirit->user_id = $this->Auth->user("id");

        if ($this->Spirits->save($spirit)) {
            $this->Flash->success("Your spirit was successfully saved.");

            $this->redirect(["action" => "index"]);
        } else {
            $this->Flash->error("Your spirit could not be saved.");
        }

    }

    $this->set(compact("spirit", "spirit_types", "countries", "regions"));
}

The important part is that debug statement. It shows this when I insert data using the form.

[
    'name' => 'Longrow Peated',
    'image' => 'imageLocation',
    'brand' => 'Springbank',
    'age' => '',
    'cost' => '55'
]

Those are all text and/or number elements in my form, and they all come out just fine. It gets a little weirder though. I have validation in my table to require those id fields:

public function validationDefault(Validator $validator) {
    $validator->requirePresence(
        "name", "brand", "spirit_type_id", "country_id", "region_id", "age", "cost", "image"
    )
        ->notEmpty("name", "We require a name")
        ->notEmpty("brand", "We require a brand or distillery")
        ->notEmpty("spirit_type_id", "We require a type of alchohol")
        ->notEmpty("country_id", "We require a country of origin")

But this doesn't ever seem to get triggered when I insert the data using patchEntity, it's only caught when I actually call the save function and I try inserting into the database.

Upvotes: 0

Views: 65

Answers (2)

Greg Schmidt
Greg Schmidt

Reputation: 5099

If $this->request->getData() is not showing all of your fields, the most likely cause would be some sort of problem with your form; there are not a lot of ways for CakePHP to discard your data from here. You can narrow it down by using browser tools (built into most of them now) to inspect the data actually being sent from your browser in the page request.

If it turns out that the fields really aren't being sent across at all, the problem is almost certainly in your form. For example, you might be closing it early, or there might be HTML errors that confuse the browser. Make sure that all of your input tags are between the <form> and </form>, and if they are then try an HTML validator to check your code. There are lots of options online, and even the inspectors built into browsers can often help you spot these sorts of issues.

Upvotes: 1

Salines
Salines

Reputation: 5767

This is the most common problem:

If you check debug($this->request->getData()); before $spirit = $this->Spirits->newEntity(); you then see all submitted data!

Next go to Spirit Entity and double check if your fields "spirit_type_id,.." accessible!

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * Note that when '*' is set to true, this allows all unspecified fields to
 * be mass assigned. For security purposes, it is advised to set '*' to false
 * (or remove it), and explicitly make individual fields accessible as needed.
 *
 * @var array
 */
protected $_accessible = [
    '*' => true, // quick fix
    'id' => false,
];

or better way:

protected $_accessible = [
    'spirit_type_id' => true,
    'country_id' => true,
    // etc ...
];

Edit

debug

$spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
debug($spirit); exit();

see if any errors.

Upvotes: 0

Related Questions