I have this configuration:
A page containing a search field, at the submit in the same page I want a list of every result matching the research, everyone linking to a corresponding route. For example if I find 4 elements, I want that in the resulting page 4 links Azienda1, Azienda2 ecc.
Now I get this error:
An exception has been thrown during the rendering of a template ("The "ABCAziendaBundle_visualizza_azienda" route has some missing mandatory parameters ("id_azienda").") in ::base.html.twig at line 27.
500 Internal Server Error - Twig_Error_Runtime
1 linked Exception:
MissingMandatoryParametersException
Here are the key files,
#config.yml
ABCAziendaBundle_visualizza_azienda:
pattern: /visualizza_azienda/{id_azienda}
defaults: { _controller: ABCAziendaBundle:Default:showAzienda }
requirements:
id_azienda: \d+
ABCAziendaBundle_azienda_index:
pattern: /
defaults: { _controller: ABCAICAziendaBundle:Default:indexAzienda }
#DefaultController.php
public function indexAziendaAction(Request $request) {
$searchFormType = new SearchAziendaType();
$form = $this->createForm($searchFormType);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
$data = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$aziende = $em->getRepository('ABCAziendaBundle:Azienda')->findAziendaByAliasOrRagioneSocialeSubstring($data["search_field"]);
return $this->render('ABCAziendaBundle:Default:indexAzienda.html.twig', array('form' => $form->createView(), 'aziende' => $aziende));
}
}
return $this->render('ABCAziendaBundle:Default:indexAzienda.html.twig', array('form' => $form->createView()));
}
public function showAziendaAction($id_azienda) {
echo "non entra qui";
}
#indexAzienda.html.twig
{% extends "::base.html.twig" %}
{% block pagetitle %}ABC{% endblock %}
{% block body %}
<h2>Ricerca azienda</h2>
<div id="form_container">
<form action="{{ path('ABCAziendaBundle_azienda_index') }}" method="post" {{ form_enctype(form) }}>
{{ form_label(form.search_field, "Ricerca Azienda") }}
{{ form_widget(form.search_field) }}
<input type="submit" />
</form>
<button>nuova azienda</button>
</div>
{% if aziende is defined %}
{% for azienda in aziende %}
<div class="areaTot">
{{azienda.alias}}
</div>
{% endfor %}
{% else %}
<div class="areaTot">
<p>"NIENTE"</p>
</div>
{% endif %}
{% endblock %}
I think its a typo on your side:
<a href="{{ path('ABCAziendaBundle_visualizza_azienda', { 'azienda_id' : azienda.id }) }}">
Should be:
<a href="{{ path('ABCAziendaBundle_visualizza_azienda', { 'id_azienda' : azienda.id }) }}">
The difference is the route parameter, you wrote azienda_id initialy, but the route parameter name is id_azienda
Should clear the error.
Regards,
Matt
Related
Trying to make a simple form but i keep getting "Variable "subscriptionForm" does not exist."
enter image description here
Here is Controller method:
public function subscriptionForm(Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|array
{
$subscriptionForm = $this
->getFactory()
->createSubscriptionForm()
->handleRequest($request);
if ($subscriptionForm->isSubmitted() && $subscriptionForm->isValid()) {
// Call the client for e.g. to save the subscriber.
// Redirect to home page after successful subscription
return $this->redirectResponseInternal('home');
}
return $this->viewResponse([
'subscriptionForm' => $subscriptionForm->createView(),
]);
}
Twig file:
{% block body %}
{{ form_start(subscriptionForm) }}
{{ form_widget(subscriptionForm.email) }}
{{ form_errors(subscriptionForm.email) }}
<input type="submit" value="Subscribe" />
{{ form_end(subscriptionForm) }}
{% endblock %}
Spryker assigns all twig variables within a _view object. Try adding these code on top of the twig file:
{% define data = {
subscriptionForm: _view.subscriptionForm
} %}
I have an issue getting a book from the Google Books API in Symfony2
I am posting this form for a page (/google)...
<form action="/googlevolume" method="post">
<input type="text" name="title" id="title" size="40" value="">
<input type="submit" value="Search">
</form>
and this is my controller for the result page (/googlevolume)...
public function googlevolume(Request $request)
{
$enquiry = new Enquiry();
$form->bind($request);
$response = $enquiry->get("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData());
$data=$response->json();
$response2=$data['items'];
return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', array('items' => $response2));
}
I have tried posting this number from the form
1781100489
Which is the same as going to:
https://www.googleapis.com/books/v1/volumes?q=1781100489
However, when i put that number in the form and press search, i get this error
Controller "Blogger\BlogBundle\Controller\PageController::googlevolumeAction" for URI "/googlevolume" is not callable.
this is from my routing file...
google:
pattern: /google
defaults: { _controller: BloggerBlogBundle:Page:google }
requirements:
_method: GET
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolume }
requirements:
_method: POST
this is googlevolume.html.twig ...
{# src/Blogger/BlogBundle/Resources/views/Page/googlevolume.html.twig #}
{% extends 'BloggerBlogBundle::layout.html.twig' %}
{% block title %} Google Books{% endblock%}
{% block body %}
<header>
<h1>Google Book</h1>
</header>
<br>
{% for item in items %}
<article>
<img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/>
<h4>{{ item.volumeInfo.title}}</h4>
{% if item.volumeInfo.description is defined %}
{{ item.volumeInfo.description }}
{% endif %}
<strong> {{ item.volumeInfo.publishedDate }}</strong><br/>
<b>{{ item.volumeInfo.authors | join }}</b>
</article>
{% endblock %}
Anyone got any ideas where i'm going wrong with this?
Thanks
It looks like you are trying to serialize the request object when really you just want the value from the form. Try changing your request to the api to:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
...
public function googlevolumeAction(Request $request)
{
$form = $this->createFormBuilder(null,[
'csrf_protection' => false
])
->add('title','text')
->add('Search', 'submit')
->getForm();
$form->bind($request);
if($form->isValid()){
$enquiry = new Enquiry();
$response = json_decode(file_get_contents("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData()),true);
if(array_key_exists('items',$response)){
return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', [
'items' => $response['items']
]);
} else {
return new Response('Google Volume did not have any items', 400);
}
}
return new Response('Google Volume Not Found', 404);
}
Then the route:
googlevolume:
pattern: /googlevolume
defaults: { _controller: BloggerBlogBundle:Page:googlevolumeAction }
requirements:
_method: POST
Then clear the cache:
php app/console cache:clear --env=prod
or just for dev
php app/console cache:clear
I want to get the id attribute from an entity using "lazy loading".
This is my controller:
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$topics = $em->getRepository('BackendBundle:Topic')->findAll();
$posts = $em->getRepository('BackendBundle:Post')->findAll();
return $this->render('BackendBundle:Home:home.html.twig',
['topics' => $topics, 'posts' => $posts]
);
}
And here is my template block:
{% block article %}
{% for post in posts %}
<h3>
<a href="{{ path('backend_posts_post',
{ 'topic_id': post.topic.id, 'post_id': post.id }) }}">
{{ post.title }}
</a>
</h3>
<br>
<p>{{ post.text }}</p>
<hr>
{% endfor %}
{% endblock %}
I'm trying to get the topic id. Every post depends on a topic and should be possible to get his identifier.
Solved. I was trying to access a post whitout topic.
I have a trouble with using the flashbag messages. My case is quite simple :
My code
Editing a page using a form :
# src/Namespace/MyBundle/Resources/views/Edit/form.html.twig
<form action="{{ path('form_url_save', {'id': id }) }}" method="POST">
{{ form_widget(form) }}
</form>
Save the data form in a database via a controller :
# src/Namespace/MyBundle/Controller/EntityController.php
public function saveAction(Request $request, Entity $entity = null) {
try {
if (!$entity) {
$entity = new Entity();
}
$form = $this->createForm(new EntityType(), $entity);
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
// Entity manager
$em = $this->getDoctrine()->getManager();
// Persist data
$em->persist($form->getData());
// Saving process
$em->flush();
// Add flashbag message
$this->get('session')->getFlashBag()->add('success', 'The backup was done successfully'));
} else {
throw new \Exception($form->getErrorsAsString());
}
}
} catch (\Exception $e) {
$this->get('session')->getFlashBag()->add('error', $e->getMessage());
}
return $this->redirect('home_page_url');
}
Display a successfull message on front :
# app/Resources/views/front.html.twig
<html>
<head></head>
<body>
<div class="main">
{% set flashbag = app.session.flashbag.all %}
{% if flashbag is not empty %}
<div class="messages-container">
{% for type, messages in flashbag %}
{% for message in messages %}
<div class="alert alert-{{ type }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}
</div>
{% endif %}
<div class="content">
// My content
</div>
</div>
</body>
</html>
How my theme is organized ?
app/Resources/views/front.html.twig
|__ src/Namespace/MyBundle/Resources/views/Edit/form.html.twig // extends front.html.twig
My trouble :
app.session.flashbag.all in front.html.twig
==> Flashbag is empty
app.session.flashbag.all in form.html.twig
==> Flashbag is good and had the success message
So why i can't put the code in the front.html.twig ?
This is because FlashBag::all() returns all messages and then empty the flashes container. Use FlashBag::peekAll() method to check if flashbag contains messages.
Example:
{% if app.session.flashbag.peekAll()|length %}
{% include 'BraincraftedBootstrapBundle::flash.html.twig' %}
{% endif %}
I've just stumbled on the same problem and found this: Symfony2 FlashBag stopped working after upgrade to 2.4?
If the other subject doesn't answer your question you might want to try and dump your flashbag to see it's structure. Do do so, try adding this in your twig template:
{% set array = app.session.flashbag.all %}
{% dump(array) %}
You might be surprised by what comes out, at least I've been:
array(1) { ["test"]=> array(1) { [0]=> string(28) "Ceci est un test de flashbag" } }
Which means that you do have messages in your flashbag but you can't get the content the right way as it's in a second array. My solution:
{% for tag, line in array %}
{% for underline in line %}
<div class="{{tag}}">{{ underline }}</div>
{% endfor %}
{% endfor %}
Hope this helps
I have a custom validator on my Symfony2 project.
The validation works fine, but the method is somehow accessed twice.
Here is my custom validator: My other resolved question
The problem is the next:
As you can see, the error message is displayed twice. When I am trying to var dump something in the validate method, the vardump is also displayed twice. Any idea why the validate is called twice? This is called when I am using $form->bind($request); in my controller.
EDIT
Here is the twig template:
{% extends 'MerrinMainBundle::layout.html.twig' %}
{% block page_title %}
MDPI Conversion system (Merrin) 3.0 - New Conversion
{% endblock %}
{% block main %}
{% for flashMessage in app.session.flashbag.get('user-notice') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}
<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
</form>
{% endblock %}
And the controller call
public function createAction()
{
$em_scipub = $this->getDoctrine()->getManager();
$em_mdpipub = $this->getDoctrine()->getManager('mdpipub');
$enquiry = new Manuscript();
$formType = new NewManuscriptType();
$form = $this->createForm($formType, $enquiry);
$request = $this->getRequest();
$valid = true;
$error = '';
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
... do something ...
$em_scipub->persist($enquiry);
$em_scipub->flush();
$flash_message = "<a href='edit/".$enquiry->getId()."'>New Manuscript</a> sucessfully created.";
$this->get('session')->getFlashBag()->set('user-notice', $flash_message);
return $this->redirect($this->generateUrl('MerrinMainBundle_new'));
}
else
$valid = false;
}
$path = $this->generateUrl('MerrinMainBundle_new');
return $this->render('MerrinMainBundle:Pages:new_conversion.html.twig.twig', array(
'valid' => $valid,
'path' => $path,
'form' => $form->createView(),
) );
}
EDIT2:
The validate function:
public function validate($value, Constraint $constraint)
{
$doi = $value->getDoi();
preg_match('/[^\/]+/i', $doi, $publisherDoiAbbr);
if($publisherDoiAbbr[0] !== $value->getPublisher()->getDoiAbbreviation()) {
$this->context->addViolation($constraint->message_publisher_DOI);
}
else {
preg_match("/[a-z]+/",$doi, $journalDoiAbbr);
$em_mdpipub = $this->entityManager;
$journal = $em_mdpipub->getRepository('MerrinMdpiPubBundle:Journal')->findOneBy(array('doi_abbreviation' => $journalDoiAbbr));
if($journal == null) {
$this->context->addViolation($constraint->message_journal_DOI);
}
}
preg_match('/\d*$/i', $doi, $doiNumericPart);
if(strlen($doiNumericPart[0]) < 8) {
$this->context->addViolation($constraint->message_volume_issue_firstpage_DOI);
}
}
And the twig template:
{% extends 'MerrinMainBundle::layout.html.twig' %}
{% block page_title %}
MDPI Conversion system (Merrin) 3.0 - New Conversion
{% endblock %}
{% block main %}
{% for flashMessage in app.session.flashbag.get('user-notice') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}
<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="700">
<tr>
<td>
{{ form_label(form.doi) }}
</td>
<td>
{{ form_widget(form.doi, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.publisher) }}
</td>
<td>
{{ form_widget(form.publisher) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.file) }}
</td>
<td>
{{ form_widget(form.file) }}
</td>
</tr>
<tr>
<td>
</td>
<td>
<input class="submit-confirm-button" type="submit" name="update-text" value="submit" />
<a class="cancel-link" href="{{ path('MerrinMainBundle_homepage' ) }}">Cancel</a>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
{% endblock %}
EDIT 3:
Here is how I am applying the validator to the entity:
/**
* Manuscript
*
* #IsDOI()
* #ORM\Table(name="manuscripts")
* #ORM\Entity(repositoryClass="Merrin\MainBundle\Repository\ManuscriptRepository")
* #ORM\HasLifecycleCallbacks
*
*/
class Manuscript
{
....
}
EDIT 4:
When I try to vardump the
$form->getErrors();
I am getting an array with two values:
array(2) {
[0]=>
object(Symfony\Component\Form\FormError)#507 (4) {
["message":"Symfony\Component\Form\FormError":private]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=>
array(0) {
}
["messagePluralization":protected]=>
NULL
}
[1]=>
object(Symfony\Component\Form\FormError)#542 (4) {
["message":"Symfony\Component\Form\FormError":private]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=>
array(0) {
}
["messagePluralization":protected]=>
NULL
}
}
This is possible if you are using validation groups and apply validator to several groups. And what #IsDOI() annotation mean? If its apply validation possible that you first add validator in validation.yml and second via this custom annotation.
The problem is that you're rendering it twice.
Here:
{{ form_errors(form) }}
followed by:
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
Since you are displaying the errors in single place, I recommend using only the first method.
As stated in the form_errors twig reference:
form_errors(view)
Renders any errors for the given field.
{{ form_errors(form.name) }}
{# render any "global" errors #}
{{ form_errors(form) }}
Since you have a lot of custom code (EntityManager, Validator) it is hard to tell the error with the amount of code you provide because it's not possible to recreate the error locally.
But here are my suggestions:
In your Validator there are two possible cases the following violation gets thrown
$this->context->addViolation($constraint->message_publisher_DOI);
On both occasions the error message is the same. Maybe both of these cases apply. Try to debug this by adding an individual error message for both cases.
I don't know the code of your Form-Class but maybe you apply your custom validator to multiple fields? Also remove validations for each field individually to see where the duplicate error message is thrown.
One last mistake I can imagine would be a custom Form-Template. If you have one check whether or not you maybe call the {{ form_error(form) }} block or any other block multiple times.
I hope one of my suggestions did help you.