easyadmin action buttons in custom template - forms

I'm creating my own template for the edit page the thing is the actions buttons don't show so I tried to add them in configureActions function:
public function configureActions(Actions $actions): Actions
{
return parent::configureActions($actions)->add(Crud::PAGE_EDIT, Action::SAVE_AND_CONTINUE);
}
but i get this error
The "saveAndContinue" action already exists in the "edit" page, so you can't add it again. Instead, you can use the "updateAction()" method to update any options of an existing action+
This is my edit twig:
{% extends '#!EasyAdmin/layout.html.twig' %}<div class="tab-pane fade show active" id="client">
{% block edit_form %}
<div class="form-group">
{{ form_label(edit_form.name) }}
{{ form_widget(edit_form.name, {'attr': {'class': 'form-control' }}) }}
</div>
<div class="form-group">
{{ form_label(edit_form.surname) }}
{{ form_widget(edit_form.surname, {'attr': {'class': 'form-control' }}) }}
</div>{% endblock %}

Related

Why does Symfony 3.4 form displays EntityType choices with label although 'label' => false was used?

I am working on an existing Symfony 3.4 based project and trying to add and render a new form. Although the 'label' => false option was used, the fields are rendered including a label. Why?
// Symfony
class SomeController extends Controller {
public function userListAction(Request $request) {
$users = $someService->getUsers();
$formBuilder = $this->createFormBuilder()
->add('users', EntityType::class, [
'label' => false, // also tested '' and 'someLabel'
'class' => 'AppBundle:User',
'choices' => $users,
'multiple' => true,
'expanded' => true,
]);
$variables = array(
'form' => $formBuilder->getForm()->createView(),
);
return $this->render('AppBundle:Pages:user_list.html.twig', $variables);
}
}
// Twig
{% extends 'AppBundle::layout.html.twig' %}
{% block page_content %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
This shows a list of checkboxes for all users including the username as label.
Where does Symfony get the information to use the username as label? As far as I know no custom form widget was defined for the User class. Is there any way to check this for sure? Maybe there is something hidden in the vendor bundles like FOSUserBundle?
Why is the 'label' => false option ignored?
Edit:
Different ways of rendering the form does not solve the problem:
{{ form_start(form) }}
{{ form_row(form) }}
{{ form_end(form) }}
Result:
<div id="form_users">
<div class="form-group">
<div class="checkbox">
<label for="form_users_547">
<input type="checkbox" id="form_users_547" name="form[users][]" value="547">
someUserName
</label>
</div>
</div>
</div>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Result:
<div id="form_users">
<div class="checkbox">
<label for="form_users_547">
<input type="checkbox" id="form_users_547" name="form[users][]" value="547">
someUserName
</label>
</div>
</div>
{{ form_start(form) }}
{% for userFormView in form.users %}
{{ form_row(userFormView) }}
{% endfor %}
{{ form_end(form) }}
Result:
Basically the same as before with form_row
You need to use ‘choice_label’ => ‘YOUR PROPERTY PATH’ in the field options.
Pretty match is written in the docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
If the entity class cast to string then is used if is not it will throw an exception. It looks like your entity User cast to the user name and that’s why it works.
You should try to use {{ form_row(form) }} which should render the whole field correctly.

prototype field is empty after form(form) but set when set before the form(form) symfony

when I follow this tutorial: http://symfony.com/doc/current/cookbook/form/form_collections.html
and I render the
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
the prototype stay empty if I put it after the {{ form(form)}} but gets filled If I put it i before the {{ form(form) }} tag. any one an idea why this is and how to solve it.
thanks
The tag {{ form(form) }} is supposed to output all your form, so there is nothing to output after this tag.
If the tag {{ form(form) }} does not output the prototype, then it was not configured right in the form type class.
But if you chose to output prototype by using form_widget, you should not use form(form) and should output the form by parts:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_row(form.another_form_property) }}
</div>
<div>
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
</div>
{{ form_end(form) }}

symfony2 fosUserbundle input type with bootstrap

I'm quite new to symfony2, i have managed to implement fosUserBundle and Braincrafted bootstrap bundle.
I am trying to style the registration form to use boot strap and the inputs with input-sm class
the username and email are displaying as input-sm but the 2 password fields refuse to resize to the class i have applied.
is there somewhere in the fosUserbundle where the password field widget is configured
{% trans_default_domain 'FOSUserBundle' %}
{{ form_start(form, { 'style': 'horizontal', 'col_size': 'xs', 'label_col': 5, 'widget_col': 7, attr: {class: 'pull-left'}}) }}
{{ form_row(form.username, { attr: {class: 'input-sm'}}) }}
{{ form_row(form.email, { attr: {class: 'input-sm'}}) }}
{{ form_row(form.plainPassword, { attr: {class: 'input-sm'}}) }}
{{ form_row(form.plainPassword.second, { attr: {class: 'input-sm'}}) }}
<div class="col-sm-5"></div>
<div class="col-sm-7">
<input class="btn-primary btn-sm" type="submit" value="{{ 'registration.submit'|trans }}" />
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
FosUserBundle or not, you can overwrite Twig form rendering directly to achieve this, have a look at How to customize form rendering in SF cookbook.
Basically you can either get more precise in your template by splitting form_row into labels, widgets and errors to target the input ("form_widget" below) more specifically and apply the correct classes :
{{ form_errors(form.plainPassword) }}
{{ form_label(form.plainPassword) }}
{{ form_widget(form.plainPassword) }}
You can also generate your own form style for desired type of input by decalring it directly in your template :
{% form_theme form _self %}
{% block integer_widget %}
<div class="integer_widget">
// Your custom layout
</div>
{% endblock %}
{% Block body %}
// Rendering your form, your custom layout will be used.
{% endblock %}
You can also declare it in a separate template if you want to use it somewhere else, refer to the above cookbook page to see how.

multiple form in symfony2

I have a form for adding comments, and i want to know how can i multiply this form on the same page.
I tried this code but it does not work :
{% for statut in statuts %}
<div>
{{ statut.sujet }}
<form action="" method="post" {{ form_enctype(formC) }}>
{{ form_errors(formC) }}
{{ form_errors(formC.commentaire) }}
{{ form_widget(formC.commentaire) }}
<div><input type="submit" class="btn btn-info"/><div>
{{ form_rest(formC) }}
</form>
</div>
{% endfor %}
Does somebody know how to do that ? or any suggestions
You need to do this in the controller like get all statuts then run a for loop for each statuts and get comment form for each statut field store them in array and return the array.
In your twig template then run a for loop for the same.

Rendering a Form in a Twig Template with Symfony2

I have a base Twig template that has a search bar form in it at the top of the page in a Twig block. I have another block later on named "content" that my children pages fill out. Currently, my base template looks like this:
{% block admin_bar %}
<div id="search">
<form action="{{ path('search') }}" method="post" {{ form_enctype(search_form) }}>
{{ form_widget(search_form.term) }}
{{ form_widget(search_form.type) }}
{{ form_widget(search_form.pool) }}
{{ form_widget(search_form._token) }}
<input type="submit" value="Search" />
</form>
</div>
{% endblock %}
{% block content %}
{% endblock %}
However, when trying to render a child template I need to pass in the search_form variable along with it. Is there anyway (short of writing out the HTML tags myself) I can avoid having to create this search_form variable and pass it in everytime I want to render a child view? I'm using Twig in conjunction with Symfony2.
Thanks!
Embedded Controller is what you need. Put your admin_bar block into separate file:
{# src/Acme/AcmeBundle/Resources/views/Search/index.html.twig #}
<div id="search">
<form action="{{ path('search') }}" method="post" {{ form_enctype(search_form) }}>
{{ form_widget(search_form.term) }}
{{ form_widget(search_form.type) }}
{{ form_widget(search_form.pool) }}
{{ form_widget(search_form._token) }}
<input type="submit" value="Search" />
</form>
</div>
Create controller for this template:
class SearchController extends Controller
{
public function indexAction()
{
// build the search_form
return $this->render('AcmeAcmeBundle:Search:index.html.twig', array('search_form' => $searchForm));
}
}
And then embed controller into your original template:
{% block admin_bar %}
{% render "AcmeAcmeBundle:search:index" %}
{% endblock %}
{% block content %}
{% endblock %}