未提交表单元素

Form elements not being submitted

所以我遇到了一个很难解决的奇怪问题。我有一个简单的表单,其中包含一些未提交的元素,所有这些元素只有一个共同点,它们是 select 个元素:

            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"
            ]);

在我的控制器中我有:

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"));
}

重要的部分是调试语句。当我使用表单插入数据时它显示了这一点。

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

这些都是我表单中的文本 and/or 数字元素,它们都很好。虽然它变得有点奇怪。我在我的 table 中验证需要那些 id 字段:

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")

但是当我使用 patchEntity 插入数据时,这似乎从未被触发,只有当我实际调用保存函数并尝试插入数据库时​​才会被捕获。

这是最常见的问题:

如果您在 $spirit = $this->Spirits->newEntity(); 之前勾选 debug($this->request->getData());,您将看到所有提交的数据!

接下来转到 Spirit Entity 并仔细检查您的字段 "spirit_type_id,.." 是否可访问!

/**
 * 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,
];

或更好的方法:

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

编辑

调试

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

看看有没有错误。

如果 $this->request->getData() 没有显示您的所有字段,最可能的原因是您的表单存在某种问题; CakePHP 没有很多方法可以从这里丢弃您的数据。您可以使用浏览器工具(现在大多数浏览器内置)检查页面请求中实际从浏览器发送的数据,从而缩小范围。

如果事实证明这些字段确实根本没有被发送,那么问题几乎可以肯定在您的表单中。例如,您可能提前关闭它,或者可能存在 HTML 错误使浏览器感到困惑。确保您所有的输入标签都在 <form></form> 之间,如果是,则尝试使用 HTML 验证器来检查您的代码。网上有很多选项,甚至浏览器内置的检查器通常也可以帮助您发现此类问题。