Symfony 4.4 object not found by the #ParamConverter annotation - symfony

I know there are lots of similar topics already.
What I tried:
checking my routes (php bin/console router:match url)
Overriding the ParamConverter "App\Entity\User object not found by the #ParamConverter annotation"
Understanding #ParamConverter & #security annotations
And more solutions that didn't work for me.
Here is not working fragment of the controller:
/**
* Edit action.
*
* #param Request $request HTTP request
* #param User $user User entity
* #param UserPasswordEncoderInterface $passwordEncoder Password encoder
*
* #return Response HTTP response
*
* #throws ORMException
* #throws OptimisticLockException
*
* #Route(
* "/{id}/password",
* methods={"GET", "PUT"},
* requirements={"id": "[1-9]\d*"},
* name="app_password",
* )
*/
public function editPasswordUser(Request $request, User $user, UserPasswordEncoderInterface $passwordEncoder): Response
{
if (($this->getUser() == $user) || (is_array($this->getUser()->getRoles()) && in_array(
'ROLE_ADMIN',
$this->getUser()->getRoles()
))) {
$role = $user->getRoles();
$form = $this->createForm(NewPasswordType::class, $user, ['method' => 'PUT']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$user->getPassword()
)
);
$user->setUpdatedAt(new DateTime());
$this->userService->save($user);
$this->addFlash('success', 'message_updated_successfully');
return $this->redirectToRoute('detail_show');
}
return $this->render(
'security/password.html.twig',
[
'form' => $form->createView(),
'user' => $user,
'role' => $role,
]
);
} else {
return $this->redirectToRoute('detail_show');
}
}
Twig file:
{% extends 'base.html.twig' %}
{% block title %}
{{ 'title.user_editpasswd'|trans({'%id%': user.id|default('')}) }}
{% endblock %}
{% block body %}
<h1>{{ 'title.user_editpasswd'|trans({'%id%': user.id|default('')}) }}</h1>
{{ form_start(form, { method: 'PUT', action: url('app_password', {id: user.id}) }) }}
{{ form_widget(form) }}
<div class="form-group row float-sm-right">
<input type="submit" value="{{ 'action.save'|trans }}" class="btn btn-primary" />
</div>
<div>
{% if role[0] == 'ROLE_ADMIN' %}
<a href="{{ url('user_view', {id: user.id} ) }}" title="{{ 'action.back_to_view'|trans }}">
{{ 'action.back_to_view'|trans }}
</a>
{% endif %}
</div>
<div>
{% if role[0] == 'ROLE_ADMIN' %}
<a href="{{ url('user_index') }}" title="{{ 'action.index'|trans }}">
{{ 'action.index'|trans }}
</a>
{% endif %}
</div>
{{ form_end(form) }}
{% endblock %}
My app is almost finished, and that's the first and only time I have such an error.

Change argument type in controller action from User to \Symfony\Component\Security\Core\User\UserInterface and everything should work. Current user is registered in a container by this service id.

Related

Symfony3.4 Form errors rendered twice

I have a form with author and message fields and NotBlank() validation on both.
In twig, I do this:
{{ form_start(form) }}
{{ form_errors(form.author) }}
{{ form_label(form.author) }}
{{ form_widget(form.author) }}
{{ form_errors(form.message) }}
{{ form_label(form.message) }}
{{ form_widget(form.message) }}
{{ form_end(form) }}
If I press Save button with empty fields I EXPECT to see this:
But I get this:
Somehow the bottom error message comes from the {{ form_label(...) }} I say this, because if I comment the labels out and use static HTML for labels, the output is like on first picture.
I would prefer not to use static HTML for labels, but I don't understand where the second error messages came from.
Below my code:
Form
class TestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('author', TextType::class, ['required' => false, 'constraints' => [new NotBlank()]])
->add('message', TextType::class, ['required' => false, 'constraints' => [new NotBlank()]])
->add('save', SubmitType::class)
;
}
}
Controller
class TestController extends Controller
{
/**
* #Route("/testing", name="test")
* #param Request $request
* #return RedirectResponse|Response
*/
public function index(Request $request)
{
$form = $this->createForm(TestFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
return $this->redirectToRoute('test');
}
return $this->render('test/index.html.twig', [
'form' => $form->createView(),
]);
}
}
Template
{% extends 'base.html.twig' %}
{% block title %}Hello TestController!{% endblock %}
{% block body %}
<p>This is a test...</p>
{{ form_start(form) }}
{{ form_errors(form.author) }}
{{ form_label(form.author) }}
{{ form_widget(form.author) }}
{{ form_errors(form.message) }}
{{ form_label(form.message) }}
{{ form_widget(form.message) }}
{{ form_end(form) }}
{% endblock %}
For bootstrap theme error block is integrated in label.
So you need either to remove form_errors block in your template or to override form_label block.
You can use form_row (as #Adrien suggests in commentaries) as there is no form_errors call
You've explicitly added form_errors whereas error message already rendered via form_label. either you can remove form_errors or form_label.

Symfony form submit button not working

I'm trying to submit a form which is supposed to deactivate several documents. When hitting the "Submit" button nothing happens.
Here is my twig file:
{% block content %}
{{ form_start(form) }}
{{ form(form.documentlist) }}
{{ form_end(form) }}
<p>
<div class="row">
<button type="submit">Submit</button>
</div>
{% endblock content %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('js/bootstrap-multiselect.js') }}"></script>
<script>
makeMultiselectDropdown('#{{ form.vars.name }}_documentlist', 'Select Document');
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#{{ form.vars.name }}_documentlist').change(function() {
var docId = $('#{{ form.vars.name }}_documentlist').val();
$.ajax({
type: "POST",
data: {'id': docId},
});
});
});
</script>
{% endblock %}
and my Controller function:
/**
* #Route("/document/bulkdeactivate", name="documentBundle_document_bulkDeactivate")
* #Template()
*/
public function bulkDeactivateAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$selected_documents = $request->request->all();
$form = $this->createForm(DocumentDeactivationType::class);
$form->handleRequest($request);
if ($form->isValid() && $form->isSubmitted()) {
foreach($selected_documents as $document) {
$documentR = json_decode(json_encode($document), true);
dump($documentR);
for($i=0; $i<count($documentR); $i++){
$doc = $em->getRepository('DocumentBundle:Document')->findOneById($documentR[$i]);
dump($doc);
$document->setActive(false);
$em->persist($document);
$em->flush();
}
}
$this->addFlash(
'success',
'The document has been deactivated!'
);
return $this->redirectToRoute('documentBundle_document_list');
}
return $this->render('DocumentBundle:Panels:ActivationPanel.html.twig', array(
'form' => $form->createView(),
));
}
I looked in the console while hitting "Submit" and it says
Uncaught TypeError: Cannot set property 'href' of null
at window.onload (bulkdeactivate:257)
which is the line that renders the twig template. I don't know if this has anything to do with my problem, just wanted to let you know as much as I know!
Any help would be appreciated!

Symfony how do you access form fields and pass to rendered forms?

I have a form that has two rendered controllers (forms) inside it. I want to pass a variable from the main form to the rendered. I know I can do it with something like this:
{{ render(controller(
'CompanyNameofBundle:Search:shortjq', {'orgid':entity.orgId})) }}
But I am having troubles accessing the 'orgid' which is in the main form.
my.html.twig
{% extends 'CompanyNameofBundle::base.html.twig' %}
{% block body -%}
<h1>Organization Edit</h1>
{{ form_start(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}
<div class="form-group'">
<div class="col-md-2">{{ form_label(edit_form.orgName, 'Organization Name') }}</div>
<div class="col-md-4">{{ form_widget(edit_form.orgName) }}</div>
<div class="hidden">{{ form_widget(edit_form.orgId) }}</div>
<div> </div><div> </div>
</div>
<ul class="record_actions">
<li>{{ form_end(edit_form) }}</li>
<li>{{ form(delete_form) }}</li>
<li>
<a href="{{ path('org') }}">
Back to the list
</a>
</li>
</ul>
{{ render(controller(
'CompanyNameofBundle:Search:shortjq', {'orgid':entity.orgId})) }}
{% if entity.orgId is not null %}
{{ render(controller(
'CompanyNameofBundle:OrgMember:test', {'orgid':entity.orgId})) }}
{% endif %}
{% endblock %}
SearchController.php
/**
* #Route("/shortjq", name="shortjq")
* #Template()
*/
public function shortjqAction()
{
$form = $this->createForm(new JqueryType(), null, [
'action' => '',
'method' => 'POST'
]);
return array(
'form' => $form->createView(),
);
}
JqueryType.php
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->add('name', 'text')
->add('orgid', 'integer')
->add('search', 'submit')
;
}
/**
* #return string
*/
public function getName()
{
return 'companynameofbundle_jquery';
}
To access the form widgets into the views I use this:
{{ form_widget(form.orgid) }}
If you also want to add attributes to that widget you can add them like that (for example to add a class to the widget):
{{ form_widget(form.org, { 'attr': {'class': 'the-css-class-you-want'} }) }}
Here you can search for more information Twig Template Form Function and Variable Reference

Symfony forward with #Template

I have an entry point controller (editAction) which depending on a route variable ({property}) will display different templates. I am using annotation "#Template".
I tried a lots of differtings settings but none is working. The closing that I got is with the following :
Error is " The merge filter only works with arrays or hashes "
public function editAction(Request $request, $property, $id)
{
// controller logic
$form = $this->createForm('ObjectEdit'.ucfirst($property), $object);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirect($this->generateUrl('acme-demo-dashboard-show'));
}
$response = $this->forward('AcmeDemoBundle:Dashboard:edit'. ucfirst($property),
array('form' => $form)
);
return $response;
}
/**
* Edit Title property for Object
* #Template
*/
public function editTitleAction($form)
{
return array('form' => $form->createView());
}
Yaml Routing:
acme-demo-dashboard-edit:
path: /dashboard/edit/{property}/{id}
defaults: { _controller: AcmeDemoBundle:Dashboard:edit }
requirements:
acme-demo-dashboard-edit-title:
path: /dashboard/forward/edit/title/{form}
defaults: { _controller: AcmeDemoBundle:Dashboard:editTitle }
Edit: More info about the error :
at twig_array_merge (null, array('_locale' => 'en'))
Edit2: Twig Template for editTitle
{% extends "AcmeDemoBundle::layout.html.twig" %}
{% trans_default_domain "AcmeDemoBundle" %}
{% block AcmeBundleContent %}
<div class="col-sm-12">
<div class="alert alert-info">
{{ (ycRoute~'.intro') | trans }}
</div>
{{ form_start(form, { 'attr': {'class': 'form-horizontal', 'role': 'form'}}) }}
{% if form_errors(form) %}
<div class="alert alert-danger">
{{ form_errors(form) }}
</div>
{% endif %}
<div class="form-group {% if form_errors(form.title) %}has-error{% endif %}">
{{ form_label(form.title, '', {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
<div class="col-sm-9">
{{ form_widget(form.title, { 'attr': {'class': 'form-control', 'placeholder': (ycRoute~'.titre') } }) }}
<p class="help-block">{{ form_errors(form.title) }}</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
{{ form_widget(form.save, { 'attr': {'class': 'btn btn-primary'} }) }}
</div>
</div>
<div class="row">
<div class="col-sm-9 col-sm-offset-3">
(<i class="fa fa-asterisk yc-fa-sm"></i>) {{ (ycRoute~'.champRequis')|trans }}
</div>
</div>
{{ form_end(form)}}
</div>
{% endblock AcmeBundleContent %}
Did you try such syntax to render template? You will need remove #Template annotation.
return $this->render(
'AcmeDemoBundle:Dashboard:edit'. ucfirst($property),
array('form' => $form)
);
"forward" used for forwards the request to another controller.

many-to-many checkboxes not checked when they should be

I have a form with a bunch of many-to-many checkboxes. The form saves just fine (I checked the database to make sure), but then the checkboxes aren't checked in agreement with the database.
Is there something special I need to do to get the checkboxes to stay checked?
Here's my form definition:
<?php
namespace VNN\PressboxBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PreferencesSportsICareAboutType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('interestingSports', 'entity', array(
'multiple' => true,
'expanded' => true,
'property' => 'name',
'class' => 'VNN\PressboxBundle\Entity\Sport',
'query_builder' => function(\VNN\PressboxBundle\Repository\SportRepository $er) {
return $er->createQueryBuilder('s')
->orderBy('s.name', 'ASC');
},
));
}
public function getName()
{
return 'vnn_pressboxbundle_preferencessportsicareabouttype';
}
}
And here's the relevant part of my template:
{% block form %}
<h2>Sports I Care About</h2>
<form action="{{ path('user_update_preferences', { 'sectionName': sectionName }) }}" method="post" {{ form_enctype(form) }} novalidate class="clearfix">
{% for error in errors %}
error: {{ error.messageTemplate }}
{% endfor %}
{% for field in form.interestingSports %}
<li>
{{ form_widget(field) }}
{{ form_label(field) }}
</li>
{% endfor %}
{{ form_rest(form) }}
<p><button type="submit">Submit</button></p>
</form>
{% endblock %}
It looks like your Type and your Template are well defined. You should check your entity relationships.
I have already implement the same behavior but using a One-To-Many relationship.
Edit by OP:
It was the entity relationships. I needed to change this
/**
* #ORM\ManyToMany(targetEntity="UserSportInterest", inversedBy="sports")
* #JoinTable(name="user_sport_interest",
* joinColumns={#JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="sport_id", referencedColumnName="id")}
* )
*/
private $sports;
to this (note targetEntity change)
/**
* #ORM\ManyToMany(targetEntity="Sport", inversedBy="sports")
* #JoinTable(name="user_sport_interest",
* joinColumns={#JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="sport_id", referencedColumnName="id")}
* )
*/
private $sports;
After that, it worked fine.

Resources