For the first time, i am working on Symfony widgets.
{% render "EducateToolsBundle:Shared:selectMenu" with {'entity': 'Stores'} %}
its Meaning is SharedController -->selectMenuAction
{% form_theme form 'EducateToolsBundle:shared:_form_theme.html.twig' %}
What is the meaning of this.
From where i am getting the values to this form .?
You must look at selectMenuAction() method in the Shared controller. It is that function that set what template is used. if using the default Symfony coding standards it should be something like selectMenu.html.twig in a Shared sub folder in the Resources/views of the bundle.
You should looks at the documentation first :
https://symfony.com/doc/current/form/form_customization.html
Because we won't teach you symfony here, it's very well explain in the documentation and it is a task too big for us.
In your case theForm.id is the field of the formType we want to displays.
If we want to display the <input> of an form of user lastname we would use
form_widget(userForm.lastname)
form_widget tells symfony to display only the widget (the ) of the id given.
if you want to have a label + input + the errors of the field you could use
form_row(userForm.lastname)
wich is almost equal to
<div>
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
(it depends of the form theme, but you should read the doc for the details ;) )
Related
EDIT: Doing some digging, and it looks like my problem stems from the fact that the form I'm creating builds off of the FOSUserBundle's registration form. Why is that an issue? Because while their RegistrationController throws a REGISTRATION_FAILURE event if the form isn't valid, it doesn't look like the event has a listener. So, the invalid state doesn't actually do anything aside from not being handled at all by the system. It doesn't even return the form (with errors).
I have a relatively simple form theme I want to use in order to display my form errors in a block at the top of my form:
{% form_theme form _self %}
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<div class="errors pt-2 mb-2">
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
I reference it in the form template with:
{% form_theme form 'Form/errors.html.twig' %}
And then I try to invoke it with a simple:
{{ form_errors(form) }}
But, when I intentionally provide incorrect data to my form inputs and try to submit, there's no error div present. The submission fails, due to there being 6 validation errors, but no errors are displayed/in the HTML source.
Looking at the documentation, I think I'm doing it right, but I'm obviously missing something.
You have two problems and you have to solve both of them in order to make the form validation work.
1st Problem:
First problem is that the listener is not working. If you intentionally make errors in the form data and the form validation is not kicking in, then you will never see the form_errors template. You have to go over the documentation for the FOSUserBundle.
First, check if you enabled/configured the validation.yml correctly.
Secondly, check the PHP class that generates the form. Make sure that the validation criteria are set properly.
Now, the most reliable way to check if the form is producing errors is to {{dump(form)}} in the Twig template. It will show all the data for the form, including the errors.
Keep working at it until you manage to see some errors. Otherwise you will not be able to do the next step.
2nd Problem:
The second problem is that you probably not including the correct template. I can not count how many times I had the same problem. You may have 3 or 4 templates that contain html/twig code for <form> but Symfony actually uses like 1 or 2 of them. Add your code to every one of them and make sure that your Twig block is not overridden by some other form block.
Also keep in mind that you have to clean both the app cache (php bin/console cache:clean -e prod) and the browser cache after every change in the templates. You might have done everything right and the damn cache is showing the cached results.
One last advice: use the browser developer tools. Often you find a lot of information and errors are shown in the tools' console.
what is the error of form?
For throwing error you have to set asserts on your entity.
for example:
class User extend baseUser
{
/**
* #Assert\NotBlank
*/
public $name;
}
for more information: https://symfony.com/doc/current/validation.html
Currently I am working on a controller class which should be able to let a customer add an item to a table in a database and view these items (e.g. a movie).
I am using the following twig code:
{% extends 'base.html.twig' %}
{% block body %}
{% if movies|length == 0 %}
There are no movie items available. Add a movie here to get started.
{% elseif movies|length != 0 %}
These are the results: ...
{% endif %}
{% endblock %}
What is the best way to let a user/customer or whatever the case, add an item to the table that needs to be shown? Should I let a user fill a form on the exact same page as the overview template (I personally do not think this is good, as I want to keep purpose of each page seperated) or should I make a different template with a form where the user will be send to (though this takes redirection time, which some users might be getting annoyed by)? As you can see I am using an anchor tag on the word "here". How should I set-up the anchor tag if I am to use a different template for creating records in a table?
As the documentation of Symfony shows the following:
Home
The path says _welcome and I think it refers to the name of the route that points to a certain controller. Would it be best to use this path function and what would I need to add where it now says _welcome (or was I correct one sentence ago)? And why is there an underscore in the given example?
I am asking this question because when I worked with ASP.NET MVC, there was this method called ActionLink() and made me wonder if this is most common use of redirecting, since you could also just add the template file location to the anchor tag href attribute?
If the form to add a new item is small (one text field + one submit button), you could add the form in the same page.
For example :
{% extends 'base.html.twig' %}
{% block body %}
// display your form here
{% if movies|length == 0 %}
There are no movie items available.
{% elseif movies|length != 0 %}
These are the results: ...
{% endif %}
{% endblock %}
But actually it's up to you to decide if you want it to be displayed in the same page or not.
In the case you decide to redirect the user to a new template, especially for the form, you write the name of the route of the correspondant controller :
here
So the code of the controller will be executed and will redirect to the page of your form.
Concerning "_welcome", I don't know why they write it like this. This is more the way to name a layout file than a route name.
I have a question: how can 1 view/page use 2 controllers to display different data?
For example:
1) Page displays DVD's information using DvdController.
2) On that same page I want to use a Template/Partial view, that displays list of actors, and that list should come from the second ActorsController.
I can add a template view inside another page but the second controller doesn't work:
MainPage on URL: website/dvd
{% block body %}
<div>
<h1>{{ dvd.title }} </h1>
</div>
<div>
{{ include('DVDBundle:Actors:actors.html.twig') }}
</div>
{% endblock %}
Above I use the partial view actors.html.twig to show the actors, strangely the html code/div's and so on are actually displayed and working on the page, however the controllers(ActorsController) method that meant to return this partial view is not executed for some reason.?
But if I go to the view directly via link: website/actors then its working.
The Symfony documentation on embedded controllers explains this nicely.
In short, you will need to do something like this in your template file:
<div>
{{ render(controller(
'DVDBundle:Actors:actors',
{ ... parameters to the controller action here ... }
)) }}
</div>
The important thing is calling the render function, which allows a controller to be rendered, as opposed to simply includeing a template. (This assumes that your have an ActorsController with an actorsAction method; change the names as appropriate to your controller.)
I am trying to override block theme and rebuild it with html and twig.
I cant seem to find the variables from the block type or content type and cant find the image url for example.
how can i reach it using kint?
The easiest way to dump everything is with
{{ dump() }}
inside your twig template.
I work on fairly large Drupal sites, and I use this to not exhaust the memory from looping through vars.
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
This will dump everything into a nice ordered list.
Hope this helps!
Also I'm not sure if you're already doing this, but if not -- turn on the twig debug tool, then check out your inspector of choice, and it'll give you suggestions and override data.
You can do this inside your sites/default/services.yml with
twig.config:
debug: true
If you have kint (of Devel module) installed, just use:
{{ kint(_context) }}
Its better than {{ dump() }}, cause kint can manage when the recursion is too long, avoiding memory issues. Second, have a nice way to display the information.
I would like to have a simple contact form in the footer of every page. I already created a controller which is working fine on a seperate page. But when i render the controller inside of a template:
{% render(controller('MyBundle:Default:contact', {'request': app.request })) %}
It is working for rendering the form, validation and sending the mail BUT
1.) my flashmessage is not shown:
$this->get('session')->getFlashBag()->add(...)
and 2.) When i try to redirect i get an error
return $this->redirect($this->generateUrl('homepage'))
So my question is:
How can i set the flash message and redirect?
Is rendering a controller in twig actually the best practice? Or are there other approaches?
(I also tried to create a twig extension but ran into different other problems like i cant use formbuilder function etc...)
Method get from FlashBag apart from getting the message in addition removes it... So probably you've invoked get method somewhere before. For example following code always shows nothing:
{% if (app.session.flashbag.get('message')) %}
<div class="message">{{ app.session.flashbag.get('message') }}</div>
{% endif %}
If this is the case, then instead of the first get you should use has method in the condition.
Moreove if you want get and only get the message from the flash bag - without removing it, you can use peek method.