I have an interesting problem. I couldn't find any solution in stackoverflow and also google. I have an Entity User and User have some metas. So I created a UserMeta Entity and also UserMetaValue. In user form there is lots of tabs. And I used these metas in the tabs. Some of them in first tabs, some of them in other tabs. And all tabs have their own form. When I bind a form on active tab, active tab metas update, others changed to NULL.
StudentPersonalType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentPersonalType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add("first_name", null, array(
"label" => "İsim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("last_name", null, array(
"label" => "Soyisim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("username", null, array(
"label" => "Öğrenci Numarası",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("email", null, array(
"label" => "Email",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType()
));
}
public function getName(){
return "personal";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
StudentEducationType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentEducationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType(),
'by_reference' => false
));
}
public function getName(){
return "education";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
And twig
<div id="personal-info" class="tab-pane row-fluid active">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{{ form_row(form.first_name) }}
{{ form_row(form.last_name) }}
{{ form_row(form.email) }}
{{ form_row(form.username) }}
{% for meta in form.metas %}
{% if meta.value.vars.label in formValues.personal %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(form._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
<div id="education-info" class="tab-pane row-fluid">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{% for meta in educationForm.metas %}
{% if meta.value.vars.label in formValues.education %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(educationForm._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
I filter collection fields by check it in twig file with IF statement.
I ask my question again, How could I use metas in different form in same page without affecting the others?
You have done half the job of filtering out the irrelevant fields for rendering. But you also need to filter out the irrelevant fields for form binding.
When you bind the request to the form it is expecting values for every UserMeta entity because there is a field for all of them in the UserMetaType collection. You'll need to remove all the UserMetaType forms which don't correspond to one of your submitted values. It's probably best to this with a FormEvents::PRE_BIND listener.
You can see a simpler example of this at Form: Avoid setting null to non submitted field. It will be slightly more complex for you, because you'll have to iterate through the collection of UserMetaType forms and remove the ones which you don't want bound.
Related
I have a SquadType form which contains an embedded SquadMemberType form as a collectionType field.
Each time I press the add squad member button on my form, I can add a squadMember form from the prototype. Works perfectly.
The fact is, each time I add a squadMember form, it displays the name and tag label. I want to find a way to display these labels only for the first squadMember but not for the others.
I tried to set label to false in the form squadMemberType class and to display it directly in the template with no success (didn't find a way to make it work).
Is there a way to achieve that ?
My main form :
class squadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('squadName', TextType::class)
->add('squadDescription', TextareaType::class)
->add('squadMembers', CollectionType::class, [
'label' => ' ',
'entry_type' => SquadMemberType::class,
'entry_options' => [
'attr' => [
'class' => 'email-box',
'style' => 'display:flex; gap:1rem;',
]
],
'allow_add' => true,
])
->add(
'save',
SubmitType::class, ['label' => 'Create Squad']
)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Squad::class,
]);
}
}
My embedded form :
class SquadMemberType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('squadMemberName', TextType::class, [
'label_attr' => [
'class' => 'formLabel',
],
'label' => 'Name',
])
->add('squadMemberTag', IntegerType::class, [
'label' => 'Tag',
// 'label' => false,
'label_attr' => [
'class' => 'formLabel',
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => SquadMember::class,
]);
}
}
My twig :
<div class="content">
{{ form_start(form, {'attr': {'class': 'form-horizontal', 'id': 'create-squad'}}) }}
<div class="creation-form">
<fieldset class="creation-form-globalInfo">
<div class="form-group {{ form.squadName.vars.valid==false ? "has-error" }}">
{{ form_label(form.squadName, null, {
'label': 'Squad Name',
'label_attr': {'class': 'control-label'}}) }}
<div class="">
{{ form_widget(form.squadName) }}
{{ form_errors(form.squadName) }}
</div>
</div>
<div class="form-group {{ form.squadDescription.vars.valid==false ? "has-error" }}">
{{ form_label(form.squadDescription, null, {
'label': 'Squad Description',
'label_attr': {'class': 'control-label'}}) }}
<div class="">
{{ form_widget(form.squadDescription) }}
{{ form_errors(form.squadDescription) }}
</div>
</div>
</fieldset>
<fieldset class="creation-form-info">
<ul id="squadMember-fields-list"
data-prototype="{{ form_widget(form.squadMembers.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}"
data-widget-counter="{{ form.squadMembers|length }}"
class="squad-form">
{% for squadMemberField in form.squadMembers %}
<li>
{{ form_errors(squadMemberField) }}
{{ form_widget(squadMemberField) }}
</li>
{% endfor %}
</ul>
<button type="button"
class="add-another-collection-widget secundary-button"
data-list-selector="squadMember-fields-list">
{{ 'squad.add.squadMember.button.label'|trans }}
</button>
</fieldset>
</div>
<div class="validation-bottom">
{{ form_row(form.save,{
'label': 'squad.add.confirm.button',
'attr': {
'class': 'primary-button'
}
})
}}
</div>
{{ form_end(form) }}
</div>
my script :
{% block javascripts %}
<script>
$(document).ready( function () {
$('.add-another-collection-widget').click(function (e) {
var list = jQuery(jQuery(this).attr('data-list-selector'));
// Try to find the counter of the list or use the length of the list
var counter = list.data('widget-counter') || list.children().length;
// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data('widget-counter', counter);
// create a new list element and add it to the list
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
});
</script>
{% endblock %}
When building a form in symfony form builder changing the choice attribute can be done.
However, for the label attribute this doesn't seem possible.
Here is how i modify the choice:
$builder->add('type', EntityType::class, [
'class' => Resourcetype::class,
'multiple' => true,
'expanded' => true,
'choice_attr' => function (?Resourcetype $type) {
return ['class' => $type->getSafeName() . '-parent parent' : $type->getSafeName()
];
});
Is this possible for the label_attr field?
EntityType doesn't provide option for modifying choice label attributes.
You should do it yourself.
1. Simple solution
Iterate over choices in templating engine, one by one and render it yourselves. Obtain entity from choice and set label attribute.
{{ form_start(form) }}
{%- for choice in form.choices %}
<div>
{% set entity = form.choices.vars.choices[choice.vars.value].data %}
{{ form_widget(choice) }}
{{ form_label(choice, null, {
label_attr: {class: 'test-' ~ entity.number}
}) }}
</div>
{% endfor -%}
{{ form_end(form) }}
2. Clean solution
Create custom type extending EntityType:
https://symfony.com/doc/current/form/create_custom_field_type.html
Create new option in type definition allowing closures e.g. "choice_label_attr" and pass closure to view:
// src/Form/Type/CustomEntityType.php
namespace App\Form\Type;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class CustomEntityType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('choice_label_attr');
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['choice_label_attr'] = $options['choice_label_attr']
}
public function getParent(): string
{
return EntityType::class;
}
}
Extend template for choice type:
https://symfony.com/doc/current/form/form_themes.html#applying-themes-to-all-forms
Use "choice_label_attr" callback inside extended template:
{% use "bootstrap_4_layout.html.twig" %}
{% block custom_entity_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
{{- form_widget(child) -}}
{{- form_label(child, null, {class: choice_label_attr(form.choices.vars.choices[child.vars.value].data), translation_domain: choice_translation_domain}) -}}
{% endfor -%}
</div>
{%- endblock custom_entity_widget_expanded %}
More info: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig
Usage example:
use App\Form\Type\CustomEntityType ;
$builder->add('type', CustomEntityType::class, [
'class' => Resourcetype::class,
'multiple' => true,
'expanded' => true,
'choice_attr' => function (?Resourcetype $type) {
return [
'class' => sprintf('%s-parent parent', $type->getSafeName()) : $type->getSafeName()
];
});
Solution 2. is written from head and can contain some bugs but I hope you get the idea.
Both solution are using Twig and Bootstrap 4 form layout, but it is not requirement.
To allow customisation of the registration form, some form fields are generated from database informations and the form in embedded into the registration form.
While each fields of the embedded form is typed, for some reason, when I render them using twig, some types like url or number are rendered as text type input.
Yet, all fields form the main form (nom, prenom, email, plainPassword) are rendered with the assigned type.
As you can see in the code fragments, I'm properly using form_widget and form_widget to render each input, thus type handling is done by Symfony.
When I dump each formView for each field, within field.vars.block_prefixes (array), I can find the type of the input as it should be.
As example, this is the content of a text input :
"block_prefixes" => array:3 [▼
0 => "form"
1 => "text"
2 => "_security_extraDataCollection_datum-30"
]
The content of a url input :
"block_prefixes" => array:4 [▼
0 => "form"
1 => "text"
2 => "url"
3 => "_security_extraDataCollection_datum-31"
]
And the content of a number input :
"block_prefixes" => array:3 [▼
0 => "form"
1 => "number"
2 => "_security_extraDataCollection_datum-33"
]
At first, I thought that was because I was using material-component-web, but even without CSS, this problem occur.
Any idea as to why url and number type are turned to text type when I render them from enbedded form?
Registration form
public function buildForm(FormBuilderInterface $builder, array $options) {
/** #var array $extraData */
$extraData=$options['extra_data'];
$builder->add('nom')
->add('prenom')
->add('email', EmailType::class)
->add('plainPassword', PasswordType::class, array(
'mapped'=>false,
'constraints'=>array(
new NotBlank(array(
'message'=>'Please enter a password',
)),
new Length(array(
'min'=>6,
'max'=>4096,
)),
),
));
if($extraData !== null && is_array($extraData) && count($extraData)) {
$builder->add('extraDataCollection', UnmappedMixedType::class, array(
'mapped'=>false,
'data'=>$extraData,
));
}
}
UnmappedMixedType form
public function buildForm(FormBuilderInterface $builder, array $options) {
/** #var array $extraData */
$extraData=$options['data'];
/** #var ExtraData $extraDatum */
foreach($extraData as $extraDatum) {
if($extraDatum->getType() == 'text') {
$builder->add('datum-'.$extraDatum->getId(), TextType::class, array(
'mapped'=>false,
'required'=>$extraDatum->getIsObligatoire(),
'label'=>$extraDatum->getLabel(),
));
} elseif($extraDatum->getType() == 'url') {
$builder->add('datum-'.$extraDatum->getId(), UrlType::class, array(
'mapped'=>false,
'required'=>$extraDatum->getIsObligatoire(),
'label'=>$extraDatum->getLabel(),
));
} elseif($extraDatum->getType() == 'number') {
$builder->add('datum-'.$extraDatum->getId(), NumberType::class, array(
'mapped'=>false,
'required'=>$extraDatum->getIsObligatoire(),
'label'=>$extraDatum->getLabel(),
));
} elseif($extraDatum->getType() == 'checkbox') {
$builder->add('datum-'.$extraDatum->getId(), CheckboxType::class, array(
'mapped'=>false,
'required'=>$extraDatum->getIsObligatoire(),
'label'=>$extraDatum->getLabel(),
));
} elseif($extraDatum->getType() == 'choice' && $extraDatum->getChoix() !== null && count($extraDatum->getChoix()) >= 1) {
$builder->add('datum-'.$extraDatum->getId(), ChoiceType::class, array(
'mapped'=>false,
'required'=>$extraDatum->getIsObligatoire(),
'label'=>$extraDatum->getLabel(),
'multiple'=>$extraDatum->getIsChoixMultipleUtilisateur(),
'choices'=>array_combine($extraDatum->getChoix(), $extraDatum->getChoix()),
));
}
}
}
Twig view
{% if form.extraDataForm is defined %}
<div class="app-auth-left-frame-extra">
<div class="app-form-container">
<div class="app-form_field-container">
{% for field in form.extraDataForm %}
{{ dump(field) }}
{% if field.vars.block_prefixes[1] == 'text' or field.vars.block_prefixes[1] == 'number' %}
<div class="mdc-text-field mdc-text-field--outlined">
{{ form_widget(field, {'attr': {'class': 'mdc-text-field__input'}}) }}
<div class="mdc-notched-outline">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
{{ form_label(field, null, {'label_attr': {'class': 'mdc-floating-label'}}) }}
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
</div>
{% elseif field.vars.block_prefixes[1] == 'checkbox' %}
<div class="mdc-form-field">
<div class="mdc-checkbox">
{{ form_widget(field, {'attr': {'class': 'mdc-checkbox__native-control'}}) }}
<div class="mdc-checkbox__background">
<!--suppress HtmlUnknownAttribute -->
<svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59"></path>
</svg>
</div>
</div>
{{ form_label(field, null, {'label_attr': {'class': 'app-txt-light-emphasis'}}) }}
</div>
{% elseif field.vars.block_prefixes[1] == 'choice' %}
<div>{{ form_widget(field) }}</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
{% endif %}
UrlType Field as the documentations says:
The UrlType field is a text field that prepends the submitted value with a given protocol (e.g. http://) if the submitted value doesn't already have a protocol.
If you would like an input with url type create your own Form Field Type like that:
class MyUrlType extends AbstractType {
/**
* {#inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['type'] = 'url';
}
/**
* {#inheritdoc}
*/
public function getParent()
{
return __NAMESPACE__.'\TextType';
}
}
From here https://symfony.com/doc/current/form/create_custom_field_type.html
I have built out a form which submits fine, however subform values all end up as null on the receiving end when I look at them in the controller.
Here is my UserProfileType form, based on the Userclass. So specifically, the subforms we are looking at are subscriptionTier1, subscriptionTier1, and subscriptionTier1:
class UserProfileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('email', EmailType::class)
// etc... I'll keep out the unimportant fields
// here are the subforms whose values show up as null on the back end
->add('subscriptionTier1', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('subscriptionTier2', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('subscriptionTier3', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'save'],
])
;
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
'mode' => null
)
);
}
}
Here is what my UserSubscriptionTierType form type class looks like:
class UserSubscriptionTierType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'attr' => [
'maxlength' => 25
]
])
->add('price', ChoiceType::class, [
'choices' => [
'$10 per month' => 10,
'$20 per month' => 20,
'$30 per month' => 30
]
])
->add('description', TextareaType::class)
->add('messaging', CheckboxType::class, [
'required' => false
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => UserSubscriptionTier::class,
));
}
}
As you can see in the picture below, the subform subscriptionTier1's name, price, and description values are all set on the front-end:
Here is what the twig code looks like for the subscriptionTier1 subform:
<div class="row justify-content-center w-100 ml-0 mr-0 mt-3 tier tier-1" >
<div class="col-lg-6 mt-1">
<div class="form-group form-control-lg w-100">
<label for="firstname" class="mb-2">Tier 1 Name</label>
{{ form_widget(form.subscriptionTier1.name, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="col-lg-6 mt-1">
<div class="form-group form-control-lg w-100">
<label for="tier1price" class="mb-2">Tier 1 Price</label>
{{ form_widget(form.subscriptionTier1.price, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="col-lg-12 mt-3">
<div class="form-group form-control-lg w-100">
<label for="bio" class="mb-2">Tier 1 Description
<sup class="text-danger">*</sup>
</label>
{{ form_widget(form.subscriptionTier1.description, { 'attr': {'class': 'form-control border-box', 'rows':'8'}}) }}
</div>
</div>
<div class="col-lg-12">
<div class="form-group form-control-lg w-100">
<div class="form-check">
<label class="form-check-label">
{{ form_widget(form.subscriptionTier1.messaging, { 'attr': {'class': 'form-control form-check-input'}}) }}
<span class="form-check-sign"></span>
Enable Messaging
</label>
</div>
</div>
</div>
</div>
Here is the code on the receiving end, the controller:
public function saveProfileAction(Request $request)
{
$user = $this->getUser();
$form = $this->createForm(UserProfileType::class, $user);
$form->handleRequest($request);
dump($form->get('subscriptionTier1')->getData());
So in debugging, if I dump just the first form subscriptionTier1, you can see that the values are all null.
ProfileController.php on line 269:
Form {#2235 ▼
-config: FormBuilder {#2236 ▶}
-parent: Form {#2112 ▶}
-children: OrderedHashMap {#2237 ▶}
-errors: []
-submitted: true
-clickedButton: null
-modelData: UserSubscriptionTier {#2280 ▼
-id: null
-posts: ArrayCollection {#2323 ▶}
-subscriptions: null
-name: null // Don't understand why this is null
-price: null // Don't understand why this is null
-description: null // Don't understand why this is null
-tierNumber: null
-versionNumber: null
-messaging: false
-user: null
+"subsciptions": ArrayCollection {#2089 ▶}
}
-normData: UserSubscriptionTier {#2280 ▶}
-viewData: UserSubscriptionTier {#2280 ▶}
-extraData: []
-transformationFailure: null
-defaultDataSet: true
-lockSetData: false
}
Would anyone know why the values are not passing to the back-end (or getting nulled out)?
Since the data_class option of UserProfileType form type is set to User class, the model data will be an instance of User class and since the User class doesn't have fields like subscriptionTier1 etc, these will not appear in your model data.
Instead, you could access unmapped fields in a form in a controller like this:
$subscriptionTier1 = $form->get('subscriptionTier1')->getData();
Documentation here
EDIT: You can only access the value of subscriptionTier1 etc, only after the form has been handled and only if the form has been submitted, otherwise it will be null:
public function saveProfileAction(Request $request)
{
$user = $this->getUser();
$form = $this->createForm(UserProfileType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$subscriptionTier1 = $form->get('subscriptionTier1')->getData();
dump($subscriptionTier1);
}
I don't know if this is just a workaround or of this is actually the best practices way to do it, but here's how I got it to work:
I had the following subform tags in:
{{ form_start(form.subscriptionTier1) }}
etc...
{{ form_end(form.subscriptionTier1) }}
This will nest form tags. Apparently you are not allowed to nest forms like this.
So taking out the {{ form_start(form.subscriptionTier1) }} , {{ form_end(form.subscriptionTier1) }} for all the subscriptionTier1, subscriptionTier1, subscriptionTier1and then checking for the form values in the request object worked.
I dont know why not can show a variable, I read the manual and I searched in many websites, but I have no idea what I'm doing wrong...
I have a classType (there is not from a entity), the class form have 3 combobox and a textbox, when I submit the form, another template (show2.html.twig) render the new form where I want show the textbox value... I speend many time and a have 0 result ;)
When I see the Symfony profiler, I can see the values of the form in the "Request POST Parameters" section, but I'cant catch it and show it in the twig template...
that is the code wath dont work for my
$this->get('request')->request->get('campo', 'can not show it ¬¬')
in the template dont show the "campo" value, just "can not show it ¬¬"
this is my classType
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('Pais', 'entity', array(
'class' => 'UnadeniZonaBundle:Pais',
'property' => 'paisnomb',
))
->add('Provincia', 'entity', array(
'class' => 'UnadeniZonaBundle:Provincia',
'property' => 'provnomb',
))
->add('Ciudad', 'entity', array(
'class' => 'UnadeniZonaBundle:Ciudad',
'property' => 'ciudnomb',
))
->add('Campo', 'text', array(
'label' => 'campo',));
}
public function getName() {
return 'ciudad2';
}
}
My controller...
public function newAction(Request $request) {
$form = $this->createForm(new Ciudad2Type());
return $this->render('UnadeniZonaBundle:Ciudad2:new.html.twig', array(
'form' => $form->createView(),
'mensaje' => 'test'
));
}
public function showAction(Request $request) {
if ($request->isMethod('POST')) {
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $this->get('request')->request->get('campo', 'can not show it ¬¬')
));
}
}
this the templates
(new.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
<form action="{{ path('ciudad2_show') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
{{ mensaje }}
</form>
{% endblock %}
(Show2.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
{{ mensaje }}
{% endblock %}
If you try this :
$postData = $request->request->get('ciudad2');
$targetValue = $postData['Campo'];
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $targetValue)
));
It's a typo issue. Change campo to Campo in your controller.
if ($this->getRequest()->isMethod('POST')) {
$request = $this->getRequest->request;
$campo = $request->get('Campo', 'cannot show it.', true);
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $campo
));
}