gpt4 book ai didi

cakephp - 如何减少 CakePHP 中的表单代码重复

转载 作者:行者123 更新时间:2023-12-04 16:18:55 25 4
gpt4 key购买 nike

我在 CakePHP 中有一个表单,其中有几十个字段。从我看到的所有示例中,添加 View 和编辑 View 都有重复的表单代码。

有什么技巧可以防止重复吗? CakePHP 中最好的方法是什么?

最佳答案

我所做的是将所有表单字段放在一个元素中,然后将元素插入 add.ctp 和 edit.ctp

不要忘记在edit.ctp中添加带有id的隐藏字段

这样所有可见元素都在一个文件中,更易于维护。

View/MyModel/add.ctp
echo $this->Form->create('MyModel');
echo $this->element('my_form');
echo $this->Form->end();

View/MyModel/edit.ctp
echo $this->Form->create('MyModel');
echo $this->Form->input('id');
echo $this->element('my_form');
echo $this->Form->end();

View/Elements/my_form.ctp
// your form inputs
// whatever they are

关于cakephp - 如何减少 CakePHP 中的表单代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4900801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com