I'm using silex to build an election simulator app and I need to render a form using the twig template engine. Problem is, the size of the form will depend on the previous form's results and I need to render each field in its corresponding row. It looks very much like a matrix.
In the following example, Lists A and B got more than 10% in the first round so they advance to the second. You can then define the "report" of the votes from all lists that entered the election.
http://img15.hostingpics.net/pics/970049form.jpg
Each row is generated like so:
foreach($first_round_lists as $list)
{
foreach($winners as $key => $winner)
{
$form->add('list'.$i.$key, null, array('label' => false, 'attr'=> array('class'=>'entry data', 'maxlength'=>'5', 'value'=>'')));
}
$form->add('blanks-list'.$i, null, array('label' => false, 'attr'=> array('class'=>'entry data', 'maxlength'=>'5', 'value'=>'')));
$form->add('abstention-list'.$i, null, array('label' => false, 'attr'=> array('class'=>'entry data', 'maxlength'=>'5', 'readonly'=>"readonly")));
$i++;
}
Any ideas on how I could render this properly using twig ?
Related
I have a custom autocomplete input that is not bound to any entities:
$builder
->add('input', TextType::class, [
'autocomplete' => true,
'autocomplete_url' => 'https://path-to-autocomplete',
'tom_select_options' => [
'create' => false,
'preload' => true,
'maxItems' => 1,
'delimiter' => '/',
],
])
;
The input correctly requests the autocomplete URL, fetches results, renders the correct item label, and sends the correct item value with the form.
The problem arises after submitting the form at step #6.
Empty form is rendered.
We select an item with ID 15 and label Foo.
Input is rendered correctly.
Form is submitted.
Value of 15 is sent to the server with the form.
Now we have to re-render a form again with an initial value of the input of 15.
At this point, the input value is rendered as 15 instead of Foo. That makes perfect sense. The input just doesn't know how to get a label for an item with ID 15.
Question: how do I provide the input with data about the item label?
I expected the it to have something like reverse_autocomplete_url that would be called after input initialization to get items by their IDs but I don't think there is such an option.
Considering you used symfony form.
You used $form->isValid() etc..
Persisted and flush your object.
Then instead of re-rendering the twig, call redirectToRoute function to the current route.
If you provide more detail about your usecase i can give you a better answer.
You have to initialize the form field with options array:
'tom_select_options' => [
'options' => [
[
'value' => 15,
'text' => 'Foo',
],
],
],
value and text keys are customizable via valueField and labelField properties.
I have $form and some fields in the different tabs, that works okay, I want to add a field into the form
$this->getSubject()->getId()
if this condition is not NULL
What I did, after defining form that should be visible in all conditions i added this part of code, to add field url in the tab dimension (tab dimension is already added in previous formmapper definition):
if($this->getSubject()->getId() !== NULL){
$formMapper
->with('tab.dimension')
->add(
'url',
null,
[
'required' => false,
]
)
->end();
}
But the error i recive is:
New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.
Any help?
Use tab like this. If you want to add grouping than use it with function.
if($this->getSubject()->getId() !== NULL) {
$formMapper
->tab('dimension')
->add('url', null, [
'required' => false,
])
->end();
}
In a Form say I have a builder option like this:
->add('choice', ChoiceType::class, [
'choices' => [
'Cheese' => 'cheese',
'Plain' => 'plain
]
])
And let's say we are editing this option, in the database they've already selected plain. With twig we can write the widget like this:
{{ form_widget(BurgerForm.choice, {
'value': burger.type
}) }}
This will make the value in the database the pre-selected value for the select. But if you do the same thing with EntityType:
->add('choice', EntityType::class, [
'class' => 'AppBundle:BurgersTypes',
'choice_label' => 'type'
])
And you use the same twig it doesn't pre-select the option from the database. How can I get the value from the database to show as the pre-selected value of the widget?
Pre-selecting a value for this form means setting a value on the underlying data. In your case, the controller ought to look something like:
// src/AppBundle/Controller/MyController.php
namespace AppBundle\Controller\MyController;
use AppBundle\Entity\Order;
use AppBundle\Entity\BurgersTypes;
use AppBundle\Form\Type\FormType;
use Symfony\Component\HttpFoundation\Request;
public function formAction(Request $request)
{
$obj = new Order();
$defaultBurgerChoice = new BurgersTypes('cheese');
$ob->setChoice($defaultBurgerChoice);
$form = $this->create(FormType::class, $obj);
$form->handleRequest($request);
...
// Now, if the form needs to render a value for `choice`,
// it will have the value of BurgerForm.choice determined
// intentionally - by your default, or overridden and
// handled in the request!
return [
'BurgerForm' => $form->createView()
]
}
I do use Zikula 1.5.2dev
My module is generated with modulestudio
I have made two entries in the Category registry. One is showing at the node "Global" and one at the node "Type"
In Global are several entries I can select. Some other entries are inside Type.
The selection is working in my template like expected. But how can I use the node names as a label?
I can not figure out in which template I have to place to label (have to do more searching). But more important, I do not know the right twig syntax to catch the categories label.
if you assign a category to the template, the properties are accessible like normal class properties.
{{ category.name }}
if you need the display name, this is stored as an array with lang codes as keys
{{ category.display_name['de'] }}
Hope that helps.
That sounds good. But now I have recognized this label seem not to be placed in a pure template. There is a form type defined:
class ShowRoomItemType extends AbstractShowRoomItemType
{
/**
* #inheritDoc
*/
public function addCategoriesField(FormBuilderInterface $builder, array $options)
{
$builder->add('categories', CategoriesType::class, [
'label' => $this->__('Category') . ':',
'empty_data' => null,
'attr' => [
'class' => 'category-selector'
],
'required' => false,
'multiple' => false,
'module' => 'RKShowRoomModule',
'entity' => 'ShowRoomItemEntity',
'entityCategoryClass' => 'RK\ShowRoomModule\Entity\ShowRoomItemCategoryEntity',
// added:
'includeGrandChildren' => true
]);
}
}
In my template it is called like this:
{{ form_row(quickNavForm.categories) }}
For this my skills are very limmited. I will write a feature request at modulestudio. (https://github.com/Guite/MostGenerator/issues/1147)
But big thanks for your reply!
This has been fixed for core 1.5.4 / 2.0.4 in https://github.com/zikula/core/pull/3846
I'm using sonata admin and there is an option 'editable' => true for edit directly inline datas on the list view.
If my field is a text, it's ok, i can click, edit the text and save directly on the table.
But i don't want an input type="text" when i click on the field, but a list, i'm trying something like :
->add('etat', null, array('editable' => true), 'choice', array(
'choices' => array(
'Brut' => 'Brut',
'NRP' => 'NRP',
)
))
But no effetc.. is this possible ?
Since Sonata Admin Bundle 2.2 choice accepts the "editable" parameter in the list view. You use it like that:
$listMapper->add('etat', 'choice', [
'choices'=>['Brut'=>'Brut', 'NRP' => 'NRP',],
'editable'=>true,
]);
Doc: https://sonata-project.org/bundles/admin/2-2/doc/reference/field_types.html
It's possible for scalar values only. Hear some doc
http://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
Well it's not possible at the moment and won't be possible as I can guess in the near future. Your own realization should be written.