CakePHP:添加字段以形成(动态) [英] CakePHP: add fields to form (dynamically)

查看:124
本文介绍了CakePHP:添加字段以形成(动态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态添加字段到我的CakePHP表单,但似乎我使用的每个方法都有自己的缺点,我不能解决。这里是我尝试:


  1. 不要添加新的输入,只需用逗号分隔单个输入中的值。这看起来很容易,当尝试破解输入并将每个值保存在数据库中的新行时出现一些问题。但是我放弃了这个想法,当我不得不编辑这些值...收集所有的值,并在输入中将它们引入,然后当我保存检查所有值是否仍然存在,如果没有,删除表中的相应行。


  2. 我尝试使用JS创建新的输入。但在这里,我偶然发现了两个问题:安全组件,这是一个黑洞尝试(我真的需要这个组件来检查表单的完整性)和事实,如果验证失败,我的输入将与它们的值一起消失。我必须补充一个单一的模型可以有无限的字段,所以输入将是像 name = [Model] [14] [field],name = [Model] [17] [field] code>,因此我不能限制安全组件不验证这些特定的输入(我知道他们的ID只有当我添加他们与JS,反正,安全组件构建其数据后)。


  3. 使用PHP添加字段(因此,在添加新字段后重新加载页面),但我似乎无法找到如何在另一个字段之后添加字段,以及如何在验证错误后维护输入数据。


任何建议都非常感谢! >

谢谢!

解决方案

我不确定我能给你完全回答你的问题,但希望我可以给你一些想法。



我发现自己在类似的情况下试图创建管理系统的投票。 p>

每个Poll都有很多PollOptions,我想在poll / admin_edit页面上动态添加所需的poll选项。



我使用CakePHP的内置功能加上一些Javascript来管理它。



在admin_edit视图中构建表单时,投票字段和下面我添加了:

 < div id =poll-options> 
<?php
if(isset($ this-> data ['PollOption'])){
$ i = 0;
foreach($ this-> data ['PollOption'] as $ opt){
echo $ form-> hidden(PollOption. $ i.id);
echo $ form-> input(PollOption。$ i.name,array('label'=>Option。($ i + 1)));
$ i ++;
}
}
?>
< / div>

$ this-> data 在pollsController。如果Poll已经有相关的PollOptions,它们也被包括在 $ this-> data 中。此外,如果表单已经提交并且存在验证错误,则当显示页面时,所有需要的PollOption字段都会重新构建,因为它们仍然位于 $ this-> data 从控制器。



这样做,以确保我总是有我需要的视图中的字段。



提交表单时,我尝试使用简单的方式保存数据

  $ this-> Poll-> ; saveAll($ this-> data,array('atomic'=> false,'validate'=>'first')); 

(如果您不确定上述语法,可以检查CakePHP API或docs) / p>

我使用Javascript(jQuery)动态添加PollOption字段:

  $('#add-option-button')。click(function(event){
var optionCount = $('#poll-options> div')。size()+ 1;
var inputHtml ='< div class =input text>< label for =PollOption'+ optionCount +'Name> Option'+ optionCount
+'< / label>< input id =PollOption'+ optionCount +'Name'type =textname =data [PollOption] ['+ optionCount +'] [name]/>< / div>';
event.preventDefault ();
$('#poll-options')。append(inputHtml);
});

你提到你自己这样做,但是如果你请尝试使用表单上的安全组件。



希望这有助于指出正确的方向,或者给你一些想法。


I am trying to dynamically add fields to my CakePHP form, but it seems that each method I used had its own downsides which I couldn't fix. Here is what I tried:

  1. Don't add a new input, just separate the values in a single input with a comma. It seemed pretty easy, with some problems when trying to explode the input and save each value in a new row in the database. But I abandoned the idea when I had to edit these values...gather all the values and implode them in an input, then when I saved check whether all values are still there, if not, delete the corresponding row from the table...To much work to do.

  2. I tried creating new inputs on the fly, with JS. But here, I stumbled upon 2 problems: the Security component, which was throwing a blackhole attempt (and I really need this component to check the integrity of the form) and the fact that if validation fails, my inputs will disappear together with their values. I must add that a single Model may have unlimited fields, so the inputs will be something like name=[Model][14][field], name=[Model][17][field], therefore I can't restrict the Security component not to validate those certain inputs (I know their IDs only when I add them with JS, anyway, way after the Security component builds its data).

  3. Adding fields with PHP (therefore, reload the page after a new field is added), but I cannot seem to find out how do I add a field exactly after another present field, and also, how to maintain the input's data upon validation errors.

Any suggestion is highly appreciated!

Thank you!

解决方案

I'm not sure I can give you a full answer to your question, but hopefully I can give you a couple ideas.

I found myself in a similar situation when trying to create an admin system for polls.

Each Poll had many PollOptions and I wanted to make it possible to dynamically add as many poll options as needed on my polls/admin_edit page.

I managed this with CakePHP's built-in functionality plus a bit of Javascript.

When building the form in the admin_edit view, I first put in the Poll fields and under that I added this:

<div id="poll-options">
<?php 
if (isset($this->data['PollOption'])) {
    $i = 0;
    foreach ($this->data['PollOption'] as $opt) {
        echo $form->hidden("PollOption.$i.id");
        echo $form->input("PollOption.$i.name", array('label' => "Option " . ($i + 1)));
        $i++;
    }
}
?>
</div>

$this->data was set in PollsController. If the Poll already had related PollOptions, they were also included in $this->data. Also, if the form had already been submitted and there were validation errors, all the needed PollOption fields were built again when the page displayed because they were still in $this->data from the controller.

So that took care of making sure I always had the fields I needed in the view.

When the form was submitted, I attempted to save the data with a simple

$this->Poll->saveAll($this->data, array('atomic' => false, 'validate' => 'first'));

(You can check the CakePHP API or docs if you're not sure of the above syntax)

I used Javascript (jQuery) to dynamically add the PollOption fields:

$('#add-option-button').click(function(event){
    var optionCount = $('#poll-options > div').size() + 1;
    var inputHtml = '<div class="input text"><label for="PollOption' + optionCount + 'Name">Option ' + optionCount
        + '</label><input id="PollOption' + optionCount + 'Name" type="text" name="data[PollOption][' + optionCount + '][name]" /></div>';
    event.preventDefault();
    $('#poll-options').append(inputHtml);
});

You mentioned you were doing this yourself, but you shouldn't have any trouble with it if you don't try to use the Security component on the form.

Hope this helps point you in the right direction, or maybe give you some ideas.

这篇关于CakePHP:添加字段以形成(动态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆