表单元素未提交

时间:2018-11-09 03:38:08

标签: cakephp cakephp-3.0

所以我有一个奇怪的问题,我很难弄清楚。我有一个简单的表单,其中有一些未提交的元素,所有这些元素只有一个共同点,它们是选择元素:

            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'
]

这些都是我表单中的所有文本和/或数字元素,它们都很好。虽然有点奇怪。我的表中有验证要求这些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插入数据时,似乎从未触发过这种情况,只有当我实际调用save函数并尝试插入数据库时​​才会被捕获。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

这是最常见的问题:

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

接下来转到精神实体,并仔细检查您的字段“ 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();

查看是否有错误。

相关问题