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.
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 used Assert to validate fields like
#Assert\NotBlank(message="this field cannot be empty")
$private title;
#Assert\NotBlank(message="this field cannot be empty")
$private description;
#Assert\NotBlank(message="this field cannot be empty")
$private price;
Now, when Im using form in html.twig
{% body block %}
{{ form(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
everything is allright, if I have an empty field I got my message, but when Im trying to divide this form like
{% body block %}
{{ form_start(form, {"attr": {"novalidate": "novalidate"}}) }}
{{ form_widget(form.title) }}
{{ form_widget(form.description) }}
{{ form_widget(form.price) }}
{{ form_rest(form) }}
{{ form_end(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
I'm getting something like default message that I cannot add an advert, but there are no messages next to my fields. What am I doing wrong?
I've tried use novalidate attribute in every form field but it still doesn't working
You should use {{ form_row(form.title) }} instead {{ form_widget(form.title) }} and so on. Alternatively add {{ form_error(form.title) }} to every {{ form_widget(form.title) }} and so on.
Explanation: form_widget render only form control ie. input box or drop-down. form_row render: form_label - the title of field, form_widget - the control, form_error if needed - the errors attached to field. It also wrap everything on nice div to group related parts.
Let's say I am using the Symfony Form Component to render a simple form with only two fields (username and email)
I am rendering both fields with form_row() to add custom css around, then call form_widget() to render the rest of the elements (another option would be form_rest())
What I am looking for is a way to check beforehand whether form_widget() will print any objects or not, in order to add custom html in case there are extra fields.
The way I did it is like this:
//app/form/index.html.twig
{# Other template code #}
...
{# Rendering form: #}
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{% set form_rendered = form_widget(form) %}
{% if form_rendered %}
<h3>Other fields</h3>
{{ form_rendered | raw }}
{% endif %}
{{ form_end(form) }}
{# End form #}
...
{# Other template code #}
However, I am not satisfied with it. Is there any better way?
Edit: When using CSRF Protection (activated by default), the previous code would ALWAYS print <h3>Other fields</h3>, since the form has an extra hidden field for the token that we didn't print. We would need to render it somewhere with {{ form_row(form._token) }}.
By checking the code in src/Symfony/Component/Form/FormView.php, I found a function that does exactly what I wanted.
//src/Symfony/Component/Form/FormView.php
/**
* Returns whether the view was already rendered.
*
* #return bool Whether this view's widget is rendered
*/
public function isRendered()
{
$hasChildren = 0 < count($this->children);
if (true === $this->rendered || !$hasChildren) {
return $this->rendered;
}
if ($hasChildren) {
foreach ($this->children as $child) {
if (!$child->isRendered()) {
return false;
}
}
return $this->rendered = true;
}
return false;
}
Now the function can be used in the template as such:
{% if not form.isRendered() %}
<h3>Other fields</h3>
{{ form_widget(form) }}
{% endif %}
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 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