donglian5309 2013-10-14 19:04
浏览 33

CakePHP表单创建函数:缺少id

(CakePHP 2.1, CentOS) Hey guys- so I'm doing something simple in CakePHP. I start a form using the FormHelper class from inside my edit.ctp view, using a familiar call:

echo $this->Form->create();

and when I go to mycoolsite/posts/edit/17 the markup contains

<form action="/posts/edit/17"

This is expected. Now, when I specify the model and the url, like this,

echo $this->Form->create('Post', array('url' =>  '/posts/edit'));

and again go to mycoolsite/posts/edit/17, I get this form tag in the markup, which is missing the "/17" from the action:

<form action="/newsite/posts/edit"

Is this normal? I could have sworn that the record id gets appended to the action automatically by the create function even when you specify the url parameter explicitly.

  • 写回答

2条回答 默认 最新

  • douzong5057 2013-10-14 19:17
    关注

    Don't forget to add the id in the form:

    echo $this->Form->create('Post');
    echo $this->Form->input('id');
    ...
    

    Then everythings works as it should - and as it's documented.

    And don't mangle with the url if you don't have to. It will post to itself on its own :) What do you are doing there results in expected behavior: You overwrite the url to post to with an invalid one.

    Also note that you should always prefer the array syntax over strings for flexibility. And no, it will not magically add ids/strings to string URLs.

    EDIT: In case you have multiple forms per action do don't have to use different urls to post to. Only different forms/ids to make them unique (maybe also containing a unique form field with the "action" it is supposed to do and to distinguish forms). But if you really want different target URLs, make sure you generate them in a valid way:

    echo $this->Form->create('Post', array('url' => array('action' => 'edit', $id)));
    

    $id being the value passed down from the controller where you read the record and use its id value here to populate the form with the full url including the missing id.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?