dongshandun4363 2012-10-08 13:37
浏览 42
已采纳

Symfony2 Prototype包含不需要的字段

I'd like to create an admin panel for one of my projects, so I started with this tutorial: How to Embed a Collection of Forms

Continued with creating double embedded form, so i have an object, what have objects, and these objects also have objects :D The Doctrine mapping is good, and works, the problem: When I click on the "Add new Property" it creates the property, but when I click on the "Add Detail" (which adds a detail to the property) it creates the Detail's fields, but one extra field too: <label class="required">1label__</label> The formtype codes: For the main class

$builder
        ->add('forma')
    ;

    $builder->add('properties', 'collection',array(
        'type' => new PropertyType(),
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false
        ));

For the property class:

$builder
        ->add('nev')
        //->add('szokokut')
    ;

    $builder->add('details', 'collection',array(
        'type' => new DetailType(),
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false
        ));

For the Detail class:

$builder
        ->add('description')
        //->add('property')
    ;

These codes are from the buildFrom() functions.

Any idea why is there the extra field?

The first prototype:

<div id="szokokut_storebundle_szokokuttype_properties___name__"><div><label for="szokokut_storebundle_szokokuttype_properties___name___nev" class="required">Nev</label><input type="text" id="szokokut_storebundle_szokokuttype_properties___name___nev" name="szokokut_storebundle_szokokuttype[properties][__name__][nev]" required="required" maxlength="100" /></div><div><label class="required">Details</label><div id="szokokut_storebundle_szokokuttype_properties___name___details" data-prototype="&lt;div&gt;&lt;label class=&quot;required&quot;&gt;__name__label__&lt;/label&gt;&lt;div id=&quot;szokokut_storebundle_szokokuttype_properties___name___details___name__&quot;&gt;&lt;div&gt;&lt;label for=&quot;szokokut_storebundle_szokokuttype_properties___name___details___name___description&quot; class=&quot;required&quot;&gt;Description&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;szokokut_storebundle_szokokuttype_properties___name___details___name___description&quot; name=&quot;szokokut_storebundle_szokokuttype[properties][__name__][details][__name__][description]&quot; required=&quot;required&quot; maxlength=&quot;100&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"></div></div></div>

The second:

<div><label class="required">1label__</label><div id="szokokut_storebundle_szokokuttype_properties_1_details_1"><div><label for="szokokut_storebundle_szokokuttype_properties_1_details_1_description" class="required">Description</label><input type="text" id="szokokut_storebundle_szokokuttype_properties_1_details_1_description" name="szokokut_storebundle_szokokuttype[properties][1][details][1][description]" required="required" maxlength="100" /></div></div></div>

The problem will be here: Its from the tutorial (with a little change) and it replaces the __name__ field in both of the prototypes :/

var $addPropertyLink = $('<a href="#" class="add_property_link">Tulajdonság hozzáadása</a>');
var $newLinkLi = $('<p></p>').append($addPropertyLink);

jQuery(document).ready(function() {
propertyHolder.append($newLinkLi);

$addPropertyLink.on('click', function(e) {
    e.preventDefault();

    addPropertyForm(propertyHolder, $newLinkLi);
    });
});

function addPropertyForm(collectionHolder, $newLinkLi) {
    // Get the data-prototype we explained earlier
    var prototype = collectionHolder.attr('data-prototype');

    // Replace '__name__' in the prototype's HTML to
    // instead be a number based on the current collection's length.
    var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);

    // Display the form in the page in an li
    var $newFormLi = $('<p></p>').append(newForm);
    $newLinkLi.before($newFormLi);
    addFormDeleteLink($newFormLi);
    var $addDetailLink = $('<a href="#" class="add_detail_link">Részlet hozzáadása</a>');
    var $LinkLi = $('<p></p>').append($addDetailLink);
    $newFormLi.find('div[id$=_details]').append($LinkLi);

    $addDetailLink.on('click',function(e){
        e.preventDefault();

        addDetailForm($newFormLi.find('div[id$=_details]'),$LinkLi);
    });



}

function addFormDeleteLink($FormLi) {
    var $removeFormA = $('<a href="#">törlés</a>');
    $FormLi.append($removeFormA);

    $removeFormA.on('click', function(e) {
        // prevent the link from creating a "#" on the URL
        e.preventDefault();

        // remove the li for the tag form
        $FormLi.remove();
    });
}

function addDetailForm(collectionHolder,$newLinkLi){
    // Get the data-prototype we explained earlier
    var prototype = collectionHolder.attr('data-prototype');

    // Replace '__name__' in the prototype's HTML to
    // instead be a number based on the current collection's length.
    var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);

    // Display the form in the page in an li
    var $newFormLi = $('<p></p>').append(newForm);
    $newLinkLi.before($newFormLi);
    addFormDeleteLink($newFormLi);

}
  • 写回答

1条回答 默认 最新

  • dsolwotv00116 2012-10-08 14:07
    关注

    You should try to make sure your javascript only acts on the right set of elements, by using a good enough selector. Show your js for a more detailed answer.

    UPDATE Look at the code

    There seems to be an undocumented prototype_name option that you can change from name to whatever you want for one of your prototypes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀