English 中文(简体)
不同 JQuery 用户界面标签但只有一页的 Symfony2 多个表格
原标题:Symfony2 multiple forms in different JQuery UI tabs but single page

I m facing a problem that I can summarize as it follows: I have a TWIG template page like this (reg.html.twig):

{% extends "::base.html.twig" %}
{% block body %}
<ul class="tabs">
    <li class="left"><a href="#tab1">tab1</a></li>
    <li class="left"><a href="#tab2">tab2</a></li>
    <li class="left"><a href="#tab3">tab3</a></li>
    <li class="right"><a href="#tab4">tab4</a></li>
</ul>
<div class="tabs_container">
    <div id="tab1" class="blocco-tab">
        <form action="{{ path( AAA ) }}" method="post" {{ form_enctype(form) }}>
            <div id="name_field">
                {{ form_row(form.name) }}
            </div><!-- /name_field -->
            <div id="address">
                 {{ form_row(form.addresses[0].road) }}
            </div><!-- /address_field -->
        </form>
    </div>
    <div id="tab2" class="blocco-tab">
        <form action="{{ path( BBB ) }}" method="post" {{ form_enctype(form) }}>
            <div id="surname_field">
                {{ form_row(form.surname) }}
            </div><!-- /surname_field -->
        </form>
    </div>
</div> <!-- contenitore_tabs -->
{% endblock %}

Fields name, surname and addresses belong to a sample Symfony2 entity Person. addresses is the first and only element of a collection of addresses (I need this as collection for other reasons)

联署材料的工作档案是:

jQuery(document).ready(function() {
    $(".blocco-tab").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".blocco-tab:first").show();
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".blocco-tab").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });
});

实体文件 :

class Person {
    protected $name;
    protected $surname;
    protected $addresses;

    public function __construct(){
        $this->addresses = new ArrayCollection();
    }
}

在“默认主计长”中:

public function tab1Action(Request $request){
    $person = new Person();
    $address = new Address();
    $addr_coll = new ArrayCollection();
    $addr_coll->add($address);
    $tab1_type = new Tab1Type();
    $person->setAddresses($addr_coll);
    $form = $this->createForm($tab1_type, $person);
    if ($request->getMethod() ==  POST )
    {
        $form->bindRequest($request);
        if ($form->isValid())
    /*ecc ecc ecc*/
}

public function tab2Action(Request $request){
    $person = new Person();
    $tab2_type = new Tab2Type();
    $form = $this->createForm($tab2_type, $person);
    if ($request->getMethod() ==  POST )
    {
        $form->bindRequest($request);
        if ($form->isValid())
    /*ecc ecc ecc*/
}

事实上,我采取了让每个FormType都拥有我不需要的字段, 但都放置了隐藏的和属性的域, @gt; 假的, 因为我不能只翻译我想要的域, 因为其他域会在运行时造成错误( 无效),

将每种表格放在不同的页面上(wwwdifferent routes), 由不同的主计长负责, 一切都很好, 所以这不是一个与基本使用交响乐有关的问题,

我要用这个标签,怎么解决?

  1. Do I have to make a single Action handling everything?
  2. Do I have to make a single form?
  3. Do I miss something?

事先感谢大家,我希望我清楚地解释我的问题。

最佳回答

正如我在解开以单一形式包装所有标签之前提到的那样。 两位解决方案都很好, 感谢您抽出时间 。

莱努萨多

问题回答

您只是对不同的窗体使用相同的变量

<div id="tab1" class="blocco-tab">
    <form action="{{ path( AAA ) }}" method="post" {{ form_enctype(**form1**) }}>
        <div id="name_field">
            {{ form_row(**form1**.name) }}
        </div><!-- /name_field -->
        <div id="address">
             {{ form_row(**form1**.addresses[0].road) }}
        </div><!-- /address_field -->
    </form>
</div>
<div id="tab2" class="blocco-tab">
    <form action="{{ path( BBB ) }}" method="post" {{ form_enctype(**form2**) }}>
        <div id="surname_field">
            {{ form_row(**form2**.surname) }}
        </div><!-- /surname_field -->
    </form>
</div>

尝试单窗体

{% extends "::base.html.twig" %}
{% block body %}
<form action="{{ path( AAA ) }}" method="post" {{ form_enctype(form) }}>
<ul class="tabs">
    <li class="left"><a href="#tab1">tab1</a></li>
    <li class="left"><a href="#tab2">tab2</a></li>
    <li class="left"><a href="#tab3">tab3</a></li>
    <li class="right"><a href="#tab4">tab4</a></li>
</ul>
<div class="tabs_container">
    <div id="tab1" class="blocco-tab">
            <div id="name_field">
                {{ form_row(form.name) }}
            </div><!-- /name_field -->
            <div id="address">
                 {{ form_row(form.addresses[0].road) }}
            </div><!-- /address_field -->
    </div>
    <div id="tab2" class="blocco-tab">
            <div id="surname_field">
                {{ form_row(form.surname) }}
            </div><!-- /surname_field -->
    </div>
</div> <!-- contenitore_tabs -->
 </form>
{% endblock %}

然后在 ocontroller aaaAction ()

public function aaaAction(Request $request){
    $person = new Person();
    $address = new Address();
    $addr_coll = new ArrayCollection();
    $addr_coll->add($address);
    $person->setAddresses($addr_coll);
    $form = $this->createForm(new PersonType(), $person);
    if ($request->getMethod() ==  POST )
    {
        $form->bindRequest($request);
        if ($form->isValid())
    /*ecc ecc ecc*/
}

窗体构建器类和类

class PersonType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add( name , null, array())
                ->add( surname , null, array())
                ->add( addresses , null, array())
        ;
    }

    public function getName()
    {
        return  person ;
    }

    public function getDefaultOptions(array $options)
    {
        return array(
             data_class  =>  AcmeYourBundleEntityPerson ,
        );
    }
}

如果任何其他人有这个问题,你可以这样解决(我不知道这是不是最优雅的解决方案,但我认为这比为每件事制造一个大形式要好)。 这个例子混合了配置和修改FOSUserBundle形式:

您的主显示模板( Profile: show. html. twig in my case) 中包含的显示模板 :

        {% if profileForm is defined %}
            {% include  MyBundle:Profile:edit.html.twig  with { form :profileForm} %}
        {% else %}
            {% render  MyBundle:Profile:edit  %}
        {% endif %}

重复更改拼写 :

        {% if passwdForm is defined %}
            {% include  MyBundle:ChangePassword:changePassword.html.twig  with { form :passwdForm} %}
        {% else %}
            {% render  FOSUserBundle:ChangePassword:changePassword  %}
        {% endif %}

在您的控制器( 添加) 中 :

if ($form->isValid()) {
    .....
else {
    return $this->container->get( templating )->renderResponse( FOSUserBundle:Profile:show.html. .$this->container->getParameter( fos_user.template.engine ),
                     array( user  => $user,  profileForm  => $form->createView()));
        }

相应添加配置配置格式和 passwdForm 。 在我看来, 修改过的控制器是配置控制器和 ChangePassword 控制器( FOSUserBundle overs) 。

至于您的标签,如果发现任何错误,您可以添加javascript (或 twig) 打开标签。

希望这能帮助:)

这就是我如何做到的:

<div class="form-tabs">
<ul class="nav nav-tabs" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="basic-tab" data-toggle="tab" href="#basic" role="tab">Basic</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="advanced-tab" data-toggle="tab" href="#advanced" role="tab">Advanced</a>
    </li>
</ul>

<div class="tab-content">
    <div class="tab-pane fade show active" id="basic" role="tabpanel">
        {{ form_start(formsimple, { attr : { novalidate :  novalidate }}) }}
        {{ form_widget(formsimple) }}
        {{ form_end(formsimple) }}
    </div>
    <div class="tab-pane fade" id="advanced" role="tabpanel">
        {{ form_start(form, { attr : { novalidate :  novalidate }}) }}
        {{ form_widget(form) }}
        {{ form_end(form) }}
    </div>
</div>

在控制器中 :

$formTypesimple = EventTypeMultiSimple::class;
$formType = EventTypeMulti::class;
$form = $this->createForm($formType, $event, [
     validation_groups  => [ create ,  Default ],
     user_choices  => $eventChoices,
]);
$formsimple = $this->createForm($formTypesimple, $event, [
     validation_groups  => [ create ,  Default ],
     user_choices  => $eventChoices,
]);

return $this->render( Dashboard/Shared/Event/add-edit.html.twig , [
    "event" => $event,
    "form" => $form->createView(),
    "formsimple" => $formsimple->createView(),
]);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签