I have an entity Game and an entity Player , and each game has 3 players
I'd like to know how to embed PlayerType in GameTpe for 3 times and then display them in form.twig without using javascript
GameType
class GameType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('required' => true))
->add('description', 'text', array('required' => true))
->add('date', 'date', array('required' => true))
->add('players', new PlayerType()); //how to embed playerType 3 times
}
PlayerType
class PlayerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'texet', array('required' => true))
->add('age', 'integer', array('required' => true));
//............
}
form.twig
<form method="post" action="" >
{{ form_widget(form.name) }}
{{ form_widget(form.description) }}
{{ form_widget(form.date) }}
// how to display this form 3 times
{{ form_widget(form.players) }}
<input type="submit" class="btn btn-primary" />
</form>
If your game always have three players why don't you add three fields to Game entity?
Then for example use getter to collect them all.
Related
I have following form in Symfony 2.8:
$form = $this->createFormBuilder()
->add('name', 'text')
->add('email', 'text')
->add('phone', 'text')
->add($this->createFormBuilder()
->create('address', 'form', array('virtual' => true))
->add('street', 'text')
->add('city', 'text')
->add('zip', 'text')
)
->getForm();
And I would like to dynamically add addresses in JS. I can add by CollectionType single input, as per following documentation: https://symfony.com/doc/current/reference/forms/types/collection.html
But I would like to add whole subform address. So I would like to achieve following HTML result:
<input name="form[address][0][street]" />
<input name="form[address][0][city]" />
<input name="form[address][0][zip]" />
not
<input name="form[address][street][0]" />
<input name="form[address][city][0]" />
<input name="form[address][zip][0]" />
Can anybody help? Thanks!
thanks to the comments I solved in in following way:
class Address
{
private $street;
private $city;
public function getStreet()
{
return $this->street;
}
public function setStreet($street)
{
$this->street = $street;
}
public function getCity()
{
return $this->city;
}
public function setCity($city)
{
$this->street = $city;
}
}
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('street', 'text')
->add('city', 'text');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Address::class,
));
}
}
then in controller:
$data = array(
"addresses" => array(
0 => new Address())
);
// $data is to show first empty subform
$form = $this->createFormBuilder($data)
->add('name', 'text')
->add('email', 'text')
->add('phone', 'text')
->add('addresses',
CollectionType::class,
array(
'entry_type' => AddressType::class,
'empty_data' => true
))
->getForm();
and the twig template looks like this:
{{ form_start(form) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
{{ form_label(form.email) }}
{{ form_widget(form.email) }}
{% for address in form.addresses %}
{{ form_widget(address) }}
{% endfor %}
{{ form_end(form) }}
Sorry in advance for the quality of my English
I would create a form to create a Employee. Employee and Team are connected with the ManytoOne bidirectional Relation, Team and Division are connected with the ManytoOne bidirectional Relation, and Division and Center are connected with the manytoOne bidirectional Relation. In my form I want to be able to filter the list of Teams with Javascript, according to the choice made on a first select on Center then Division to reduce the list of Teams.
Here is my code :
EmployeeType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('birthDate', DateType::class)
->add('firstName', TextType::class)
->add('name', TextType::class)
->add('isActive', CheckboxType::class, array('required' => false))
->add('employeePicture', EmployeePictureType::class)
->add('team', TeamType::class)
->add('save', SubmitType::class);
}
TeamType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Team',
'choice_label' => 'shortName',
'placeholder' => 'Choisir une équipe'
))
->add('divsion', DivisionType::class);
}
DivisionType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Division',
'choice_label' => 'shortName',
'placeholder' => 'Choisir un GMR'
))
->add('center', CenterType::class);
}
And CenterType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Center',
'choice_label' => 'shortName',
'placeholder' => 'Choisir un centre'
));
}
My controller :
public function addAction(Request $request) {
$employee = new Employee();
$form = $this->createForm(EmployeeType::class, $employee);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($employee);
$em->flush();
return $this->redirectToRoute('rte_live_working_employee_view', array('id' => $employee->getId()));
}
return $this->render('RTELiveWorkingBundle:Employee:add.html.twig', array('form' => $form->createView()));
}
My forms :
I have the error :
Catchable Fatal Error: Method
RTE\LiveWorkingBundle\Entity\Team::__toString() must return a string
value
But I have to implement __toString() in my entity :
public function __toString()
{
return $this->getShortName();
}
have you got an idea ?
Question:
Consider following Order form with so many requirements:
Title: [_________________]
REQUIREMENTS:
What sizes? [X] Small [X] Medium [_] Large
What shapes? [_] Circle [X] Square [_] Triangle
What colors? [X] Red [_] Green [X] Blue
.
.
.
How can I generate and handle the form in Symfony 3.2?
What do I think:
[ Order ] ------OneToMany------ [ Requirement ] ------OneToMany------ [ Selection ]
OrderType
class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form = $builder
->add('title', TextType::class, array());
->add('requirements', CollectionType::class,
array(
'entry_type' => RequirementType::class
)
)
->add('submit', SubmitType::class, array(();
return $form;
}
}
Problem
I don't know how to write the RequirementType, as they are not exactly the same (size, shape, color, ...).
This is what I think:
RequirementType
class RequirementType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form = $builder
->add(??????, EntityType::class,
array(
'label' => ??????,
'expanded' => true,
'multiple' => true,
'class' => Selection::class,
'query_builder' => call_user_func(function (EntityRepository $er, $requirement) {
return $er->createQueryBuilder('s')
->where('s.requirement = :requirement')
->setParameter('requirement', $requirement)
},$em->getRepository($args['class']), $requirement);
)
);
return $form;
}
}
If I correctly understand, the requirement’s attributes ("Small", "Medium", "Large"…) are stored in the Collection’ table, and are related to the requirement (“sizes”, “shapes”, “colors” …) with a oneToMany relation (a requirement can have multiple selection) ….
If so,
the following code works:
OrderType.php
$builder
->add('requirements', CollectionType::class,
array(
'entry_type' => RequirementType::class
)
);
RequirementType.php :
$builder
->add('name', HiddenType::class, array('disabled'=>true))
->add('collections', EntityType::class, array(
'class' => ‘AppBundle:Collection’,
'choice_label' => 'name', 'multiple' =>true))
In your Twig view :
{{ form_start(orderForm) }}
{% for requirement in orderForm.requirements %}
<label>{{ requirement.name.vars.value }}</label>
{{ form_widget(requirement.collections) }}
{{ form_widget(requirement.name) }}
<br>
{% endfor %}
{{ form_end(orderForm) }}
I've problem with my form type. I have an entity activity and an other entity class. It's in ManyToMany. When I display the form, it's in ChoiceType, but I want it to be in CheckboxType. So I've :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('libelle')
->add('horraire')
->add('horraireDebut')
->add('horraireFin')
->add('description')
->add('classes');
}
It display a ChoiceType but I want a CheckboxType, so I changed this to :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('libelle')
->add('horraire')
->add('horraireDebut')
->add('horraireFin')
->add('description')
->add('classes', CheckboxType::class);
}
But this only displays one checkbox while I have several recordings (which appears well with the first code).
My form.html.twig :
<div class="form-group{% if form.classes.vars.errors|length %} has-error{% endif %}">
<label for="{{ form.classes.vars.id }}" class="col-sm-3 control-label no-padding-right required">Classes <span class="red">*</span></label>
<div class="col-sm-9">
{{ form_widget(form.classes,{'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.classes) }}
</div>
How can I get a checkbox line or a checkbox dropdown ?
Thanks!
Just use multiple and expanded options together to achieve this (Ref):
$builder->add('classes', null, array(
'multiple' => true,
'expanded' => true,
));
Then checkboxes will be rendered.
Note that null value mean EntityType::class in case you use Doctrine ORM. Otherwise, use EntityType::class and 'class' => Entity::class option.
In order to achieve what you're looking for, you could also try the EntityType with a query builder:
->add('classes', EntityType::class, array(
'by_reference' => true,
'multiple' => true,
'expanded' => false,
'class' => 'AppBundle\Entity\Class',
'property' => 'name',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
$qb = $er->createQueryBuilder('c');
return $qb->orderBy('c.name', 'ASC');
}
))
I'm new to Symfony and twig templates. The problem I have is that I can't figure it out how to render the embedded form's fields separately in a twig template. I tried to search for it but probably others used a bit different forms and those examples didn't work for me.
My forms:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('user', new UserType());
$builder->add(
'terms',
'checkbox',
array('property_path' => 'termsAccepted')
);
$builder->add('Register', 'submit');
}
public function getName()
{
return 'registration';
}
}
and
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text');
$builder->add('name', 'text');
$builder->add('email', 'email');
$builder->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
'invalid_message' => 'Neteisingai pakartotas slaptažodis.',
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Keliones\MainBundle\Entity\User'
));
}
public function getName()
{
return 'user';
}
}
How the rendering field by field should look for that?
Basically Symfony and Form Framework creates Form object base on Type configuration classes. Form object has createView() method which is passed to view http://api.symfony.com/2.4/Symfony/Component/Form/FormView.html
In twig you can access embeded FormView object like that:
{# access embeded form #}
{{ form_row(form.user.username) }}
{{ form_row(form.user.email) }}
{{ form_row(form.user.password) }}
{# access main form field #}
{{ form_row(form.terms) }}