adamquaile / adamquailefieldsetbundle Goto Github PK
View Code? Open in Web Editor NEWFieldset Type in Symfony Forms
Fieldset Type in Symfony Forms
Not sure if this is a bug, or me going about things the wrong way, but as I understand it; when you want to target a form element you can use $form->get(field_name) - where "$form" is the Form object created by Controller->createForm($type, $entity).
I can target anything that is added directly though FormBuilderInterface->add(name, type, options) but If I try to target anything that is a child of a fieldset created with this bundle (using the same string as set in the fieldset[options][fields][name] variable), symfony throws an error of "Child "field_name" does not exist.
Is this a limitation of the FieldsetBundle, or an example of my ignorance of Symfony's methods?
How can I access the underlying object for the child element?
Could you change the bundle so overwriting the fieldset_widget in a custom fields.html.twig works?
That file can be set in config.yml:
# Twig Configuration
twig:
form:
resources:
- ':Form:fields.html.twig'
Now, I have to copy fieldset.html.twig to /app/Resources/AdamQuaileFieldsetBundle/views/Types/fieldset.html.twig
I believe it's better to follow the structure shown on http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html and https://gist.github.com/spcmky/8512371
I have a form without a class in which I add a fieldset with fields that need to be validated. The following form only validates the field customer_address
<?php
namespace My\NiceBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
*/
class ContactController extends Controller
{
/**
*/
public function indexAction(Request $request)
{
$defaultData = array();
$form = $this->createFormBuilder($defaultData,
array(
'translation_domain' => 'form',
'attr' => array(
'novalidate' => 'novalidate' // Disable HTML5 validation for testing server validation.
),
))
->add('customer_address','choice',array(
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large'
),
'constraints' => new NotBlank()
))
->add('contact', 'fieldset', array(
'label' => false,
'legend' => 'fieldset.contact',
'fields' => array(
array(
'name' => 'email-address',
'type' => 'email',
'attr' => array(
'label' => 'input.email-address',
),
'constraints' => new NotBlank()
),
array(
'name' => 'username',
'type' => 'text',
'attr' => array(
'label' => 'input.username',
),
),
array(
'name' => 'message',
'type' => 'textarea',
'attr' => array(
'label' => 'input.message',
),
'constraints' => new NotBlank(),
),
),
'constraints' => new NotBlank(),
))
->add('button', 'fieldset', array(
'label' => false,
'legend' => '',
'fields' => array(
array(
'name' => 'send-email',
'type' => 'submit',
'attr' => array(
'label' => 'button.send',
),
),
),
))
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
}
return $this->render('MyNiceBundle:Contact:index.html.twig', array('form' => $form->createView()));
}
}
How can I add the 'constraints' to the fields in the fieldset so it works?
NB The form is rendered in twig with:
{{ form(form) }}
Hi,
when I test this bundle, I see that the validation errors for fields are displayed at the top of the form instead next of invalid field
Hi,
I just installed and followed your directions to implement fieldsets on a Symfony 3.3 version and I get stuck with the "Could not load type "fieldset"" error.
Here are my log:
`Symfony\Component\Form\Exception\InvalidArgumentException:
Could not load type "fieldset"
at vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php:87
at Symfony\Component\Form\FormRegistry->getType('fieldset')
(vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php:74)
at Symfony\Component\Form\FormFactory->createNamedBuilder('my_group_example_one', 'fieldset', null, array('label' => false, 'legend' => 'Your fieldset legend', 'fields' => object(Closure)))
(vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php:106)
at Symfony\Component\Form\FormBuilder->create('my_group_example_one', 'fieldset', array('label' => false, 'legend' => 'Your fieldset legend', 'fields' => object(Closure)))
(vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php:269)
at Symfony\Component\Form\FormBuilder->resolveChildren()
(vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php:215)
at Symfony\Component\Form\FormBuilder->getForm()
(vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php:30)
at Symfony\Component\Form\FormFactory->create('NewsletterBundle\Form\EmailType', object(Email), array())
(vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:331)
at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('NewsletterBundle\Form\EmailType', object(Email))
(src\NewsletterBundle\Controller\NewsletterController.php:33)
at NewsletterBundle\Controller\NewsletterController->indexAction(object(Request))
at call_user_func_array(array(object(NewsletterController), 'indexAction'), array(object(Request)))
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:153)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(web\app_dev.php:29)
at require('S:\htdocs\4.TMP\newsletter\web\app_dev.php')
(vendor\symfony\symfony\src\Symfony\Bundle\WebServerBundle\Resources\router.php:42)`
Any clue on how to make it work ?
Hey, I've got a problem similar to issue #4; I can't get select boxes to render their content, and I can't work out why.
I've triple checked the format (as per the other issues resolution) and unless I'm being blind, I can't see anything wrong with it.
The options I'm passing to $buider->add() are as follows:
$builder->add(
'applicant_details',
'fieldset',
array(
'label' => false,
'legend' => 'Your Details',
'fields' => array(
array(
'name' => 'title',
'type' => 'choice',
'attr' => array(
'label' => 'Title',
'choices' => array(
'Mr' => 'Mr',
'Mrs' => 'Mrs',
'Miss' => 'Miss',
'Ms' => 'Ms',
'Mx' => 'Mx',
),
),
),
),
)
Can anyone see where I'm going wrong?
Create a simple test case running in all supported versions of symfony.
At a minimum it should test that the form type compiles and doesn't rely on features that were deprecated in v2 and removed in v3.
The options "fields", "legend" do not exist. Known options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".
500 Internal Server Error - UndefinedOptionsException
Reverting back to cd1e383 fixes the error.
This error happens when I set the fields or legend variable here:
$builder->add('general', 'fieldset', array(
'label' => false,
'legend' => 'General',
'fields' => array(
array(
'name'=>'name',
'type'=>'text',
'attr'=> array(
'label' => 'entry_name',
'constraints' => array(
new NotBlank(),
new Length(array(
'min' => 3,
'max' => 255
)),
)
)
),
....... and so on.
Did I miss something?
When I tried to use with new symfony versions (I use 6.1.2) got an:
Unable to find template "AdamQuaileFieldsetBundle:Types:fieldset.html.twig" (looked into: C:\domains\my-project/templates, C:\domains\my-project\vendor\symfony\twig-bridge/Resources/views/Form)
Is it possible to update for new versions?
I have a form to which I pass a translation_domain as parameter.
Am I missing something or could it truely be that the legend titles won't be translated automatically?
$builder
->add(
'program_group',
'fieldset',
[
'label' => false,
'legend' => 'Program details',
'fields' => [
[
'name' => 'name',
'type' => 'choice',
'attr' => ['label' => false],
'expanded' => true,
'multiple' => true,
'choices' => [
'Analytical' => 'Analytical',
'Human Resources' => 'Human Resources',
'Government Communication Service' => 'Government Communication Service',
'Northern Ireland' => 'Northern Ireland',
'Generalist' => 'Generalist',
'Commercial and Finance' => 'Commercial and Finance',
'Digital and Technology' => 'Digital and Technology',
'European' => 'European',
'Summer Diversity Internship Programme' => 'Summer Diversity Internship Programme',
'Early Diversity Internship Programme' => 'Early Diversity Internship Programme',
],
],
]
]
);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.