How can I render a single button defined through Symfony's Forms, using Twig?
Exactly how you render all the other form widgets e.g. if your submit button is configured like this:
$builder->add('submit', 'submit');
Then just render the "submit" form widget:
{{ form_widget(form.submit) }}
Related
I create a form in controller:
$form = $this->createFormBuilder()
->add('test', TextType::class)
->add('submit', SubmitType::class)
->getForm();
and I have this code in my twig page:
{{ form(form, { 'attr': {'autocomplete': 'off'} }) }}
Now when I write a value for input (for example "123456") and when I submit the form and it's invalid, the page refreshes and my input value display "123456" and when I refresh page again value not change.
You have several ways to do it:
You can create a new form type and customise its rendering with a global form theme template
You can render the specific field differently
You can try to overwrite the value attribute while rendering the field (could not work on some form themes)
Read the symfony documentation on form themes to know which solution is better for your use-case or application.
Anyway, for a validation code input (as read in the comments) I suggest to you to implement a new form type entirely and handle its rendering, options and logic separately.
In my base twig file I'm rendering my footer. In the footer these is an registration form for subscripting to the newsletter. This is de render call in the base twig.
{{ render(controller('MyBundle:Global:footer')) }}
Via this footer controller I'm rendering the footer. Hereby the controller code:
/**
* #Route("/{_locale}/newsletter/", defaults={"_locale": "nl"}, requirements={"_locale": "nl|en|de"}, name="_newsletter")
*/
public function footerAction(Request $request)
{
$form = $this->createForm(new NewsletterType());
$form->handleRequest($request);
if ($form->isValid()) {
return $this->redirectToRoute('_404');
} else {
return $this->render('MyBundle:global:footer.html.twig', array('form' => $form->createView()));
}
}
If I submit the form what only a email input is and an submit button then this route is triggered, only the form is not validated. In this example for test I want to redirect it to the 404 page. But it just reders the footer only?
I assume that you render this form on many pages because you place it in the footer. As any form you need an action attribute in the form element e.g
<form method="post" action="somewhere">
And the value of the action attribute is where your data will arrive if someone hits the Send button.
My Solution is to add an extra page that shows the same form. Like you are used to with Symfony and than render the same form in the footer and be sure that your form submits to the new page with the same form.
e.g.
$form->setAction($this->generateUrl('target_route'))
Now if anybody submits the form it will be sent to the page with the same form and if there are any errors he will see them on this page.
I'm using submit form type in my FormType class (I want it to be customized for css classes and button label).
Since it's a search, I decided to go with GET, not default POST. What I noticed in Symfony2, is that:
submit field gets a "name" parameter
and when form is submitted, one of params in url is an empty "submit" (i.e.: http://url....?phrase=searchphrase&submit)
I tried to remove auto-genarated HTML "name" attr from the "button / submit" HTML tag in controller, in form type class, and even in Twig template, e.g. by overriding "name" - nothing seems to work for Symfony2, and the name="submit" for this button is always generated.
It there a way to remove this HTML attr from submit button, or am I forced to only render whole submit button by myself, and remove it from Form Type class?
Template for the search form is very basic, nothing extraordinary for Symfony2:
{{ form_start(form) }}
<div>
{{ form_widget(form.target) }}
</div>
<div>
{{ form_widget(form.phrase) }}
{{ form_widget(form.submit) }}
</div>
{{ form_errors(form) }}
{{ form_end(form) }}
And for the form type, I use standard submit:
$builder->add('submit', 'submit', array('label' => 'search', attr(name => null) ....)
As you see, here name attr is explicilty set to null. Symfony2 generate it any way, and give it a "submit" value to this 'name' attr.
The only problem is: can I make somehow Symfony2 to NOT generate "name" attr in the HTML Button/Submit tag? If Symfony2 cannot do that, it seems that the only way is to simply remove "submity" from Form Type class, and put HTML for this button directly in the template by myself, which I'm trying to not do (but if it's not possible, I will have to).
To me, it seems like it's a problem with Symfony2. Submit type is quite new thing in forms here, and I can imagine that auto-generated "name" attr follows the same rules as other form types in Symfony2 - although it is really not needed in HTML forms!
SOLUTION: So, I ended up with rendering it all by myself, and removed it from Form Type. Still I think Symfony2 shouldn't generate 'name' for this particular tag - I saw never ever "name" attr being assigned to SUBMIT button HTML tag in any form on the web, it's obviously not needed and not desired.
I think there are three solutions:
Have a form without the submit button and create the button outside of the form. Then submit the form with JS.
Override the default twig template responsible for generating HTML content of the submit button.
Generate the HTML content by yourself and not use the name there.
I need to add an attribute to a form field in symfony.
I do it like this in my form type:
->add('myfield','text',array('attr'=>array('myattrib'=>"test")))
But this overwrites defult attributes of that field (e.g. classes)
How can I add an attribute to a field without overwriting other attributes?
Thanks
you can add it using twig if you wanna just to keep your php code clear, here is an exemple :
{{ form_row(form.Address,{
'attr':{
'class':'form-control',
'min-length':'4',
'required':'true',
}
}) }}
In your form add the attribute like this :
->add('myfield','text',array('attr'=>array('myattrib'=>"test"),'mapped'=>false))
Otherwise you can add it in your twig view when rendering you form with form_row() just add the input like normale html and get int in your action after posting like this :
$posted_value = $this->get('request')->request->get('Name attribute of your input')
I'm having some trouble trying to work out how to render a form within a modal box within Yii.
Currently I have the following code, in which I've just worked in getting a link to display a modal box.
CHtml::link(' Email this Gift', '#', array(
'id' => 'giftModal',
'onclick'=>'js:bootbox.confirm("hello world")',
)
);
I really need to render a form within the modal box. How do I do that, I have actually spent quite a while looking at how to accomplish this, but I've clearly been searching incorrectly, so would appreciate any guidance.
Thank you
Put what you want into hidden div, in this case I put my form into other view for convenience.
<!-- dialog contents on hidden div -->
<div id="modal-content" class="hide">
<div id="modal-body">
<!-- put whatever you want to show up on bootbox here -->
<?php
//example
$model = Category::model()->findByPk(1);
$this->renderPartial('//test/child-view', array('model'=>$model)) ?>
</div>
</div>
Then pass message into bootbox with above content
<script>
$(function(){
bootbox.confirm($('#modal-body').html());
</script>
When you are working with form, the button of modal box is outside your form, you have to customize a bit to make your form working properly.
Example when click button "OK" of bootbox you call submit your form by script
$('my-form-selector').submit();
Important: Because in this code I got HTML from hidden div, so there would have two forms (one on bootbox, one on hidden div). You have to add class bootbox as prefix of form element to indicate the form which you manipulate on bootbox instead (in my case, bootbox is just generated class by its self library and is the parent of content on modal box, my-form-selector could be #form-id, .form-class-name, etc)
bootbox.confirm($('#modal-body').html(), function(result){
if(result){
console.log($('.bootbox my-form-selector').parent().parent()); //<--it should print the object of modal bootbox, it ensures the form is in modal box, not one on hidden div-->
$('.bootbox my-form-selector').submit();
}});
I think you should use dialog instead of confirm, because there you can fully customize your modal box