I have actions "New" and "Edit". I want to reuse the same template in both actions. The problem is, when I'm creating a new entity, I want to show "New entity" in the page title. When I'm editing an entity I want to "Editing entity title".
I could pass a variable in each action indicating the action but I don't feel right doing it ... Are there any way to detect if it's a creation or edition in twig?
How would you solve this common issue?
you can pass the entity to the view and create a variable in twig
{% set isNew = not entity.id > 0 %}
easy?
if you want to pass only the form to the view you can get the entity directly from the form
{% set entity = form.get('value') %}
Related
I have customized ribbons for each parent form. I need to set subform ribbon to be same with parent form.
I initiate ribbon on each forms and subforms on "Form_Load" routine. Parent form ribbon will be different for different user. I cannot hard code it on form property. On subform, I use Me.Parent.Form.RibbonName to get parent form ribbon name.
Me.Parent.Form.RibbonName is able to return parent form name. But, Subform "Form_Load" is trigged before parent form "Form_Load" is trigged. Me.Parent.Form.RibbonName will return null value.
Will it possible to control the load sequence between parent form and subforms? Parent form need to be loaded before any subforms are loaded.
Thanks.
The sequence is three-fold:
Subform loads
Main form loads
Subrom loads
So have code in the subform that disables any code when opened initially. Set a flag.
When opened next time, run your code and/or sets your variables.
Yes you can. Set the Subforms holders control source to "" Then in the on load or at some other event of the parent form set the subform control to your subform.
Me.subFormHolder.SourceObject = "subfrmYourSubformName"
I think the title is not very clear, so I will explain better :
I have edited the CustomerRegister form the add a few fields, and I would like to add DefaultAdress fields too (defaultAdress is a linked object to Customer - I have a getDefaultAdresse method in CustomerObject). I would like to add all the fields that are in the DefaultAddress object (street, country, etc.)
I don't know how to do that...
Do I need to modify the CustomerRegistrationTypeExtension to add fields for the address ?
How can I call the fields in my twig file ? Like that : {{ form_row(form.defaultAddress.street) }} ?
I didn't find doc to explain that case.
Thanks for your help !
You should create CustomerRegistrationTypeExtension, just like you described. Inside of this extension, simply do:
use Sylius\Bundle\AddressingBundle\Form\Type\AddressType;
$builder->add('defaultAddress', AddressType::class)
Then override the template and you should be able to render the fields or entire form with:
{{ form_widget(form.defaultAddress) }}
I'd recommend using our standard address form template, by simply including it:
{% include '#SyliusShop/Common/Form/_address.html.twig' with {'form': form.defaultAddress} %}
I need to validate field in a Symfony2 form. It's OK, but the error is always displayed next to the related fields.
In case of any error, I also would like to display a message at the form of the form which would say 'Carreful, one or more fields are not valid, please look below'.
Is it possible ? Should I use a custom constraint to add a violation ?
Supposing your form name is form, you can do the following in your view (twig)
{% if not form.vars.valid %}
Carreful, one or more fields are not valid, please look below
{% endif %}
I need display particular fields in form in Symfony 2.6. I use a class Form. I have the folowing fields: name, email, message, send, recet. I need display all of them except recet .I try like this:
{{form_start(form)}}
{{ form_errors(form) }}
{{form_row(form.name)}}
{{form_row(form.email)}}
{{form_row(form.message)}}
{{form_end(form)}}
But, it's displaying all fields in form, it is not what I want. Even if i leave only {{form_start(form)}} and {{form_end(form)}} - its display all fields. Can someone help me wit this problem?
remove form_end, just close form with HTML
</form>
but then you must handle CSRF token generation by adding:
{{ form_widget(form._token) }}
{{ form_widget(form._token) }}
or try setting field you do not want to show using:
{% do form.recet.setRendered %}
but probably best way is not to add this field in the first place, rather than hiding it, by form options or event listeners depending on some criterias
A better solution is to remove the field from the form type. If you only remove it from the view, it may be interpreted as a blank submission for that field and delete existing data.
If you only use the form in one place, then just remove the field from the type. If you use it in multiple places, then you can selectively remove the field in a FormEvents::POST_SET_DATA event listener.
I created a custom form field extending de entity field for display a select with hierarchy of destinations. Table destination is a nested tree based on gedmo Nested Tree behavior. It's works fine but i need to show the hierarchy in select options based on tree level. For example:
Destination1
--Subdestination
--Subdestination
Destination2
--Subdestination
--Subdestination
----Subdestination
I build the tree correctly in custom form field class but i dont know how render properly the options for this select type. I was thinking via form theming override the block {% block choice_widget_options %} but this affect all selects.
How can i do that or there are a best way to do it?
you could try to implement __toString() function on your entity so ti displays proper number of "---" before label
check vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig to see how it workds and override the {% block choice_widget_expanded %} block or whichever type of list you use, you need to add {% form_theme form _self %} in the .twig file where your form is so the twig will search for overriden blocks in the same file