I have a simple form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, ['required' => true])
->add('doYouWant', ChoiceType::class, ['choices' => ['no' => 'no', 'yes' => 'yes']])
->add('type')
;
}
I would like the user after the selection doYouWant to "yes" to have a mandatory "type" option, so I am trying:
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($builder) {
$data = $event->getForm()->getData();
if ($data['doYouWant'] == 'yes') {
$builder->add('type', null, ['required' => true]);
}
}
);
But it does not matter...
I think the simplest way would be to add constraints to each field from your type, and then, in template, by using jquery you can toggle the visibility of your type field based on the selected value from the dropdown.
# AppBundle/Form/ExampleType.php
$builder
->add('name', null, [
'constraints' => [
new NotBlank(['message' => 'This cannot be empty']),
]
])
->add('doYouWant', ChoiceType::class, [
'placeholder' => 'Select',
'choices' => ['no' => 'No', 'yes' => 'Yes'],
'constraints' => [
new NotBlank(['message' => 'This cannot be empty']),
]
])
->add('type', EmailType::class, [
'constraints' => [
new NotBlank(['message' => 'This cannot be empty']),
new Email([
'message' => "The email '{{ value }}' is not a valid email",
])
]
])
;
I've added the type field as being of type email, just for testing purposes.
# Controller/DefaultController.php
/**
* #param Request $request
* #Route("/test", name="test")
* #return Response
*/
public function testAction(Request $request) : Response
{
$form = $this->createForm(ExampleType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dump($form->getData());die;
}
return $this->render('default/test.html.twig', [
'form' => $form->createView(),
]);
}
# default/test.html.twig (assuming you are using bootstrap and jquery)
{% block body %}
<div class="container">
<div class="row">
<div class="col-xs-12">
{{ form_start(form, { attr: { 'novalidate': 'novalidate' } }) }}
<div class="form-group">
{{ form_label(form.name) }}
{{ form_widget(form.name,{ attr:{ class:'form-control' } }) }}
{{ form_errors(form.name) }}
</div>
<div class="form-group">
{{ form_label(form.doYouWant) }}
{{ form_widget(form.doYouWant,{ attr:{ class:'form-control' } }) }}
{{ form_errors(form.doYouWant) }}
</div>
<div class="form-group type hidden">
{{ form_label(form.type) }}
{{ form_widget(form.type,{ attr:{ class:'form-control' } }) }}
{{ form_errors(form.type) }}
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Send</button>
</div>
{{ form_end(form) }}
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script>
$(document).ready(function(){
$('#appbundle_example_doYouWant').change(function(){
var choice = $(this).val();
if (choice == 'Yes') {
$('.type').removeClass('hidden');
} else {
$('.type').addClass('hidden');
}
});
});
</script>
{% endblock %}
You can use validation groups and put your assertions inside your entity.
And then you can choose validation groups based on the submitted data like so:
https://symfony.com/doc/current/form/data_based_validation.html
How to add assertion on entity:
class MyEntity {
/**
* #Assert\NotBlank(groups={"YourGroup"})
*/
protected $type;
}
Then on your form:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function (FormInterface $form) {
$data = $form->getData();
$want = $data->getDoYouWant();
if ($want) {
return ['YourGroup'];
}
return ['Default'];
},
));
}
Related
I've just updated to Symfony 6.2 (6.2.6 to be exact) and now, not sure why, unable to render form.
This is what debugger says:
Object of class Symfony\Component\Form\FormView could not be converted to string
In Symfony 6.2, according to the documentation, it should be possible to pass only FormInterface into render method in Controller. However, in both cases (meaning even using the createView()) method, it's unable to render the form itself. Any ideas where the problem might be?
Controller method:
#[Route(path: '/register', name: 'security-register')]
public function register(Request $request, MailUtil $util): Response
{
$form = $this->createForm(RegistrationForm::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$mail = new Mail();
$mail->setRecpient($form->get('email')->getData());
$mail->setTemplate("TestMail");
$util->sendMail($mail);
}
return $this->render("security/register.html.twig", ['form' => $form->createView()]);
}
Form class:
class RegistrationForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('email', EmailType::class, [
'required' => true
])
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'register.error.password',
'required' => true,
'first_options' => ['label' => 'Password'],
'second_options' => ['label' => 'Repeat password']
])
->add('submit', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => true,
'csrf_field_name' => '_csrf',
'csrf_token_id' => 'user_register'
]);
}
}
Twig:
{% block container %}
<div class="row">
<div class="col s6 offset-s3">
<div class="card-panel blue-grey darken-1" data-darkmode="card-panel blue-grey darken-3">
{% if not app.user %}
<h4 class="white-text center-align ">
Register
</h4>
{{ form_start(form) }}
{{ form.email }}
{{ form_end(form) }}
<div class="white-text center-align">
You are logged in as {{ app.user.userIdentifier }}<br><br><a class="waves-effect waves-light btn" href="{{ path('app_logout') }}">Logout</a>
</div>
{% endif %}
</div>
</div>
</div>
{% endblock %}
Tried to use new, Symfony 6.2 approach as stated in documentation: https://symfony.com/doc/current/forms.html#rendering-forms
And then tried to use the old one with createView() method.
Expected result should be rendered form, however both methods are throwing the same stacktrace.
In twig {{ form.email }} is wrong. This is probably why the error occur.
Switch to {{ form_row(form.email) }} for example
Or you can use widget and label separately.
{{ form_label(form.email) }}
{{ form_widget(form.email) }}
I have a form with a collectionType field, this field has 3 inputs and I want to align all 3 of them horizontally. Is it possible to do that in Symfony?
Also: Is it possible to choose the label for each element inside the collectionType?
Here's my form:
class WorkerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('Firstname')
->add('Lastname')
->add('tasks', CollectionType::class, [
'label' => 'Tasks',
'entry_type' => TasksType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'required' => false,
'by_reference' => false,
'delete_empty' => true,
'attr' => [
'class' => 'collection',
],
])
;
$builder->add('save', SubmitType::class, [
'label' => 'Valider',
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => worker::class,
]);
}
}
Here's my tasks form:
class TasksType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('label')
->add('start')
->add('end')
;
}
and here's my _form.html.twig:
{% block extra_js %}
<script src="{{ asset('jquery.collection.js') }}"></script>
{% endblock %}
{% block body %}
<div class="row">
{%
form_theme form
'jquery.collection.html.twig'
'TaksTemplate.html.twig'
%}
{{ form_start(form) }}
<div class="my-custom-class-for-errors">
{{ form_errors(form) }}
</div>
<div class="col">
{{ form_row(form.Firstname, {'label': 'firstname'}) }}
</div>
<div class="col" >
{{ form_row(form.Lastname, {'label': 'Lastname'}) }}
</div>
<div class="col">
{{ form_row(form.tasks, {'label': 'tasks'}) }}
</div>
</div>
{{ form_end(form) }} </div>
{% endblock %}
{% block script %}
<script type="text/javascript">
$('.collection').collection({
'drag_drop_options': {
'placeholder': null
}
});
</script>
{% endblock %}
Also: is is possible to choose the label for each element inside the collectionType ?
No. you can't have multiple label in symfony collectionType.
I want to align all 3 of them horizontally.
Ask this again with HTML CSS tags
I have a symfony form that contains a long list of items with checkboxes. I have successfully made knp paginator work and now the form is split into pages. However, when I move between pages I lose the checked/unchecked status of the checkboxes. I am sure that this is probably quite easy but I cannot seem to work it out. I am not very experienced with php yet.
Here is my controller:
/**
* #Route("/addquestions/{quizid}", name="addquestions")
*/
public function add(Request $request, PaginatorInterface $paginator, $quizid)
{
$repository = $this->getDoctrine()->getRepository(Quiz::class);
$quiz = $repository->find($quizid);
$repository = $this->getDoctrine()->getRepository(Question::class);
$questions = $repository->findAll();
// Paginate the results of the query
$pagination = $paginator->paginate($questions, $request->query->getInt('page', 1), 3);
$form = $this->createForm(AddQuizQuestionType::class, $quiz, ["pagination" => $pagination]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$questions = $form->get('question')->getData();
foreach ($questions as $question){
$quizquestion = new QuizQuestion();
$quizquestion->setQuestion($question);
$quiz->addQuizQuestion($quizquestion);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($quiz);
$entityManager->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('editquiz/addquestions.html.twig', [
'form' => $form->createView(),
'pagination' => $pagination,
]);
}
And my form - it uses the paginated query to display the items with a check box.
class AddQuizQuestionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('question', EntityType::class, [
'expanded' => true,
'multiple' => true,
'class' => Question::class,
'mapped' => false,
'choices' => $options['pagination'],
])
->add('submit', SubmitType::class, [
'label' => 'Submit',
])
->setMethod('GET')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Quiz::class,
'pagination' => null,
]);
}
}
and my template - it renders the checkboxes with images from the database.
{% extends 'base.html.twig' %}
{% block title %}CreatequizController{% endblock %}
{% block body %}
<h1>Create Quiz</h1>
{{ form_start(form) }}
{% for question in form.question %}
<h5>Question {{ question.vars.value }}</h5>
<div class="border mb-5">
<img src="{{ asset(question.vars.label) }}" alt=""/>
<div class="pl-3">
{{ form_widget(question) }}
</div>
</div>
{% endfor %}
<div class="navigation">{{ knp_pagination_render(pagination) }}</div>
{{ form_end(form) }}
{% endblock %}
I would like it so that when I jump between pages the checkboxes that I have selected remain checked.
Thank you in advance,
Martyn
I'm trying to customize my forms. I have been using
http://symfony.com/doc/current/form/form_customization.html
as reference for theme/form customization, but I'm not quite grasping the concept.
What I currently have
RegistrationForm.php
class UserRegistrationForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('username', TextType::class)
->add('plainPassword', RepeatedType::class, ['type' => PasswordType::class])
->add('IHaveReadAndAcceptTheTermsOfService', CheckboxType::class, array(
'mapped' => false,
'constraints' => new IsTrue(),));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'validation_groups' => ['Default', 'Registration']
]);
}
Register.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Register!</h1>
{{ form_start(form, {'attr':{'class':'boxsize'}}) }}
{{ form_row(form.username) }}
{{ form_row(form.email)}}
{{ form_row(form.plainPassword.first, {
'label': 'Password'
}) }}
{{ form_row(form.plainPassword.second, {
'label': 'Repeat Password'
}) }}
Terms of service
{{ form_row(form.IHaveReadAndAcceptTheTermsOfService) }}
cancel
<button type="submit" class="btn btn-primary" formnovalidate>
Register
</button>
<br></br>
<p>Privacy Policy
{{ form_end(form) }}
</div>
</div>
</div>
{% endblock %}
In CSS
.boxsize {
width: 200px;
}
What I am trying to do.
Make this boxsize the default size when I create a form. As it stands I would have to use class:boxsize for each form I create.
I'm trying to create an embedded form in symfony2 that will consist of a Contact form that will have embedded a ContactPhonenumber form to add phone numbers.
I have followed the instructions in Symfony2 documentation about embedding forms but I can't find a way to show at least an input field for phonenumbers. If I do it without errors, I only get back a label for the embedded phonenumber form.
How can I show an input there?
My code is:
My two entity form classes:
class ContactType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('contactType')
->add('title')
->add('name')
->add('givenName')
->add('middleName')
/*->add(
'contactPhonenumbers',
'collection',
array( 'type' => new ContactPhonenumberType($options) )
)
*/
->add('contactPhonenumbers', new ContactPhonenumberType($options) )
;
}
//.......
//More functions
//.......
}
class ContactPhonenumberType extends AbstractType
{
public function __construct(array $options=null) {
$this->options = $options;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('position')
->add('number')
->add(
$builder->create(
'contact',
'searchable',
array(
'data_class' => '....Bundle\Entity\Contact',
'class' => '....Bundle:Contact',
'dataroute' => '...._contact_searchable',
'classlistview' => new ....Bundle\Component\Listview\ContactList
)
)->addViewTransformer(
new NameToIdTransformer(
$options[ 'em' ],
$options[ 'request' ],
$this,
array(),
'....Bundle:Contact'
)
)
)
->add(
$builder->create(
'phonenumberType',
'searchable',
array(
'data_class' => '....Bundle\Entity\PhonenumberType',
'class' => '.....Bundle:PhonenumberType',
'dataroute' => '...._phonenumbertype_searchable',
'classlistview' => new ....\Component\Listview\PhonenumberTypeList
)
)->addViewTransformer(
new NameToIdTransformer(
$options[ 'em' ],
$options[ 'request' ],
$this,
array(),
'...Bundle:PhonenumberType'
)
)
)
;
}
}
And my function on controller
public function newAction( Request $request )
{
$em = $this->getDoctrine()->getManager();
$entity = new Contact();
// $number = new ArrayCollection();
// $number[] = new ContactPhonenumber();
// $entity->setContactPhonenumbers( $number );
$form = $this->createCreateForm( $entity );
return $this->render(
'...Bundle:Contact:new.html.twig',
array(
'entity' => $entity,
'form' => $form->createView()
)
);
}
And the form in twig:
<div class="widget-body">
{{ form_start( form, { 'attr': { 'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate' } } ) }}
{{ form_errors( form ) }}
{{ form_row( form.contactType ) }}
{{ form_row( form.title ) }}
{{ form_row( form.name ) }}
{{ form_row( form.givenName ) }}
{{ form_row( form.middleName ) }}
------>
{{ form_row(form.contactPhonenumbers) }}
<--------
{{ form_row( form.salutation ) }}
{{ form_row( form.address ) }}
{{ form_row( form.superiorContact ) }}
<div class="form-group">
<hr>
</div>
<div class="form-group">
<div class="col-sm-2"></div>
<div class="col-sm-10">
{{ form_widget( form.submit ) }}
{{ form_widget( form.cancel ) }}
</div>
</div>
{{ form_row( form._token ) }}
{{ form_end( form, { 'render_rest': false } ) }}