【问题标题】:How to fill forms with cakePHP如何用 cakePHP 填写表格
【发布时间】:2013-10-17 14:41:09
【问题描述】:

我正在使用 cakePHP v2.4,我正在尝试自动填写表格。 我在控制器中所做的是:

$contact = $this->Contact->findById($id);
$this->set('data', $contact);

在我看来,我像这样使用 de formHelper:

echo $this->Form->create('Person');
echo $this->Form->text('firstname', array('label' => 'Firstname'));
echo $this->Form->end();

我希望我输入的名字自动填充我从我的请求中获得的值。这是我在视图中执行 pr($data) 时得到的结果:

Array
(
[Contact] => Array
    (
        [id] => 15
        [created] => 0000-00-00 00:00:00
        [modified] => 0000-00-00 00:00:00
        [type] => person
    )

[Person] => Array
    (
        [contact_id] => 15
        [firstname] => eric
    )
)

如您所见,我有一个“联系人”表,根据类型,无论是“人”还是“公司”,我都会加载相应的表。 我不知道我在这里错过了什么,我还没有找到答案。

【问题讨论】:

    标签: php forms cakephp view controller


    【解决方案1】:

    它应该是$this->request->data = $contact;,并且应该只在 GET 请求上完成,以防您将表单数据发回相同的操作:

    if($this->request->is('post'))
    {
        // process the posted form data
    }
    else
    {
        $this->request->data = $this->Contact->findById($id);
    }
    

    使用bake shell 生成模型/控制器/视图并从该代码中学习。另请查看食谱:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#editing-posts

    【讨论】:

    • 完美,我做了 $this->request->data = $this->Contact->findById($id) 并且有效:) 谢谢!
    猜你喜欢
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2018-01-15
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多