How to use constraints in controller - symfony

I've been trying to add a edit-user page where they can change username, email address and password.
One thing I am trying to implement is they have to type in the old password to be able to change it to a new one.
I've been reading these pages:
https://symfony.com/doc/current/validation.html
https://symfony.com/doc/current/reference/constraints/UserPassword.html
but I'm really struggling on the implementation side.
Here's my Controller for the form:
<?php
namespace App\Controller\User;
use App\Entity\User;
use App\Form\User\EditUserType;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class EditController extends Controller
{
public function edit(Request $request, UserPasswordEncoderInterface $encoder)
{
$userInfo = ['username' => null, 'plainPassword' => null, 'password' => null, 'email' => null];
$form = $this->createForm(EditUserType::class, $userInfo);
$form->handleRequest($request);
$user = new User();
$oldPassword = $user->getPassword();
if ($form->isSubmitted() && $form->isValid()) {
$userInfo = $form->getData();
$username = $userInfo['username'];
$email = $userInfo['email'];
$newPass = $userInfo['plainPassword'];
$oldPass = $userInfo['password'];
$encryptOldPass = $encoder->encodePassword($user, $oldPass);
if ($oldPassword === $encryptOldPass) {
$this->addFlash('danger', $oldPass. ' ' .$encryptOldPass. ' ' .$oldPassword);
return $this->redirectToRoute('user_edit');
} else {
$this->addFlash('success', $oldPassword. '-' .$encryptOldPass);
return $this->redirectToRoute('user_edit');
}
$pass = $encoder->encodePassword($user, $newPass);
$user->setPassword($pass);
$user->setEmail($email);
$user->setUsername($username);
echo 'trey was here';
$this->addFlash('success', 'User Details Edited');
return $this->redirectToRoute('user_edit');
}
return $this->render('user/edit.html.twig', array('form' => $form->createView()));
}
}
my EditUserType file:
<?php
namespace App\Form\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class EditUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', EmailType::class)
->add('username', TextType::class)
->add('password', PasswordType::class, array())
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'New Password'),
'second_options' => array('label' => 'New Repeat Password')
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array());
}
}
my validation (file: config/validator/validation.yaml)
App\Form\User\EditUserType:
properties:
oldPassword:
- Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
message: 'Invalid Password'
my template file:
{% include 'builder/header.html.twig' %}
<div class="user-container" id="user-content">
{% block body %}
{% include 'builder/notices.html.twig' %}
<div class="user-container">
<i class="fas fa-user-edit fa-5x"></i>
</div>
<hr />
{{ form_start(form) }}
{{ form_row(form.username, { 'attr': {'class': 'form-control', 'value': app.user.username} }) }}
{{ form_row(form.email, { 'attr': {'class': 'form-control', 'value': app.user.email} }) }}
{{ form_row(form.password, { 'attr': {'class': 'form-control'} }) }}
{{ form_row(form.plainPassword.first, { 'attr': {'class': 'form-control'} }) }}
{{ form_row(form.plainPassword.second, { 'attr': {'class': 'form-control'} }) }}
<div class="register-btn-container">
<button class="btn btn-danger" id="return-to-dash-btn" type="button">Cancel!</button>
<button class="btn btn-primary" type="submit">Update!</button>
</div>
{{ form_end(form) }}
{% endblock %}
</div>
{% include 'builder/footer.html.twig' %}
Typing in any old password for the old password fields seems to get by and not update the password to the newly typed value.. so how do I validate the old password against the database so the user can update it to a new password?
Thanks

Found the solution, using cerad comment on previous (now removed) answer:
updated controller:
<?php
namespace App\Controller\User;
use App\Form\User\EditUserType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class EditController extends Controller
{
public function edit(Request $request, UserPasswordEncoderInterface $encoder)
{
$userInfo = ['username' => null, 'plainPassword' => null, 'password' => null, 'email' => null];
$form = $this->createForm(EditUserType::class, $userInfo);
$form->handleRequest($request);
$user = $this->getUser();
$entityManager = $this->getDoctrine()->getManager();
if ($form->isSubmitted() && $form->isValid()) {
$userInfo = $form->getData();
$username = $userInfo['username'];
$email = $userInfo['email'];
$newPass = $userInfo['plainPassword'];
$oldPass = $userInfo['password'];
if (!$encoder->isPasswordValid($user, $oldPass)) {
$this->addFlash('danger', 'Old password is invalid. Please try again');
return $this->redirectToRoute('user_edit');
}
$pass = $encoder->encodePassword($user, $newPass);
$user->setPassword($pass);
$user->setEmail($email);
$user->setUsername($username);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', 'User Details Edited - Please Login Again');
return $this->redirectToRoute('login');
}
return $this->render('user/edit.html.twig', array('form' => $form->createView()));
}
}
the issue was, I wasn't checking the logged in user details, and I thought persist meant insert, not insert/update - so lack of knowledge on this one.

Related

Username is changed even form input is invalid (Symfony)

Currently I am working on a recipe app in Symfony 6. So far so good. Right now I want to enable the user to change his username. The username is at the same time also the UserIdentifier.
The username can also be changed successfully. I have a constraint that sets the length of the username. However, the form with the invalid value is submitted anyway and the user is logged out.
Maybe I made a mistake somewhere, but I'm really getting desperate. Actually, the problem is quite simple, but I can't find the right solution.
Thank you very much for your help!
My Controller
`
<?php
namespace App\Controller;
use App\Form\ProfileUsernameType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ProfileChangeUsernameController extends AbstractController
{
#[Route('/profile/changeUsername')]
public function changeUsername(UserRepository $userRepository, EntityManagerInterface $entityManager, Request $request)
{
$user = $this->getUser()->getUserIdentifier();
$username = $userRepository->findOneBy(['username' => $user ]);
$form = $this->createForm(ProfileUsernameType::class, $username);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$username->setUsername($form->get('username')->getData());
$entityManager->persist($username);
$entityManager->flush();
}
return $this->render('profile/changeUsername.html.twig', [
'form' => $form,
]);
}
}
`
My Form
`
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class ProfileUsernameType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username', TextType::class,[
'data_class' => null,
'attr' => [
'placeholder' => 'Username',
'autocomplete' => 'off',
],
'constraints' => [
new NotBlank(),
new Length(['min' => 5])],
])
->add('submit', SubmitType::class, ['label' => 'Submit your username'])
->getForm();
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
`
My Twig file
`
{% extends 'base.html.twig' %}
{% form_theme form 'bootstrap_5_layout.html.twig' %}
{% block body %}
{{ form_start(form)}}
{{ form_errors(form) }}
{{ form_widget(form.username) }}
{{ form_row(form.submit) }}
{{ form_end(form) }}
{% endblock %}
`
I was facing the same problem some time ago and found this solution:
In your ProfileUsernameType.php, do it like this:
$builder
->add('username', TextType::class, [
'attr' => [
'class' => 'form-control',
'readonly' => 'readonly',
'value' =>$user['username']
],
'label' => 'Username: '
])

Some mandatory parameters are missing ("user") to generate a URL for route

My motivation is to edit values displayed in this edit form. But when I press edit button it throws out this error. I can't figure it out. Can anyone help what is missing in my code?
An exception has been thrown during the rendering of a template ("Some
mandatory parameters are missing ("user") to generate a URL for route
"sokosimu_editor_edit_editoruser".") in
SokosimuEditorBundle:User:editUser.html.twig at line 7. 500 Internal
Server Error - Twig_Error_Runtime
Router
sokosimu_editor_edit_editoruser:
path: /edit/editoruser/{user}
defaults: {_controller:SokosimuEditorBundle:Editor:editEditorUser}
requirements:
_method: GET|POST
Controller
public function editEditorUserAction(User $user,Request $request){
$form = $this->createForm(new EditUserType(),$user);
//2. handle the submit (will happen on POST)
$form->handleRequest($request);
if($form ->isValid() && $form->isSubmitted()){
}
$em = $this->get('doctrine')->getManager();
$editUser = $user ->getEditoruser();
return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
'form' => $form->createView()
));
}
View
{% block title %}Edit User{% endblock%}
{% block body %}
<form action="{{ path('sokosimu_editor_edit_editoruser') }}" method="post" {{ form_enctype(form) }} class="formedit">
{{ form_errors(form) }}
{{ form_row(form.alias)}}
{{ form_row(form.email) }}
{#{{ form_row(form.password) }}#}
{{ form_row(form.mobile) }}
{{ form_row(form.submit) }}
{{ form_rest(form) }}
</form>
{% endblock %}
Form
<?php
namespace Sokosimu\EditorBundle\Form\Type;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EditUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('alias','text',array('required'=>false));
$builder->add('email', 'email',array('required'=>true));
// $builder->add('password','password',array('required'=>true));
$builder->add('mobile','text',array('required'=>false));
$builder->add('submit', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sokosimu\UserBundle\Entity\User'
// 'data_class' => NULL
));
}
public function getName()
{
return 'editUser';
}
}
You need to pass user in the path:
<form action="{{ path('sokosimu_editor_edit_editoruser', {'user': user}) }}" method="post" {{ form_enctype(form) }} class="formedit">
And in controller render the twig with user:
return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
'form' => $form->createView(),
'user' => $user
));
Fix route to
sokosimu_editor_edit_editoruser:
path: /edit/editoruser/{userId}
defaults: {_controller:SokosimuEditorBundle:Editor:editEditorUser}
requirements:
_method: GET|POST
Fix controller to
public function editEditorUserAction(Request $request, $userId)
{
$user = $this->getDoctrine()->getRepository('SokosimuEditorBundle:User')->find($userId);
$form = $this->createForm(new EditUserType(), $user);
//2. handle the submit (will happen on POST)
$form->handleRequest($request);
if ($form->isValid() && $form->isSubmitted()) {
$em = $this->get('doctrine')->getManager();
$editUser = $user->getEditoruser();
}
return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
'form' => $form->createView()
));
}
With the help from #panche14, I have modified the code a bit.Answer from #panche14 returned object but the form #param expected to be string or integer.
return $this->render('SokosimuEditorBundle:User:editUser.html.twig',
array('form' => $form->createView(),
'user' => $user ->getId();
));
Also add this in twig file:
{'user': user}
as specified by #panche14
Now, the edit button works fine as desired.

Symfony function search wih query builder

I would like to create a function to search for a movie through the query builder
I have a table Movie:
1. Id
2. Titre
3. Content
And i have class MovieRepository :
class MovieRepository extends EntityRepository
{
public function myFindAll()
{
return $this->createQueryBuilder('a')
->getQuery()
->getResult();
}
public function getSearchMovies($movie){
$qb = $this->createQueryBuilder('m')
->where('m.title LIKE :title')
->setParameter('title', '%' . $movie->getTitle() . '%')
->orderBy('m.title', 'DESC')
->getQuery();
}
}
Also i have MovieController :
public function indexAction()
{
$movie = new Movie;
$form = $this->createForm(new SearchMovieType(), $movie);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
$movies = $this->getDoctrine()
->getManager()
->getRepository('AreaDownloadBundle:Movie')
->getSearchUsers($movie);
return $this->render('AreaDownloadBundle:Download:index.html.twig', array('form' => $form->createView(),array('movies' => $movies)));
} else {
$movies = $this->getDoctrine()
->getManager()
->getRepository('AreaDownloadBundle:Movie')
->myFindAll();
return $this->render('AreaDownloadBundle:Download:index.html.twig',array('form' => $form->createView(), 'movies' => $movies));
}
}
SearchMovieType :
class SearchMovieType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title','text', array('required' => false, ))
;
}
And i have index.hml.twig, which can display movies with a search bar :
{% extends "::template.html.twig" %}
{% block body %}
<form action="{{ path('area_download_index') }}" method="post">
<div id="bar">
{{ form_widget(form.title) }}
<input type="submit" value="Chercher">
{{ form_rest(form) }}
</div>
</form>
{% for movie in movies %}
{{ movie.title }}
{{ movie.content }}
{% endfor %}
{% endblock %}
when I seized a title of a movie he sends me this error
Variable "movies" does not exist in AreaDownloadBundle:Download:index.html.twig at line 12
Instead of posting it as a comment, it should have been posted as an answer in the correct formatting; like so:
return $this->render(
'AreaDownloadBundle:Download:index.html.twig',
array(
'form' => $form->createView(),
'movies' => $movies
)
);
This definitely should fix the problem!

Render Individual Form Element in Symfony 2

I'm having trouble rendering individual form fields in a twig template from a Symfony 2 form. In the example below, I'm simply trying to render the first_name form field.
Also, form_errors never renders anything.
Here is my Form object:
namespace ABC\WebsiteBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class LoanApplication extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('first_name', 'text', array('required' => true));
}
public function getName()
{
return 'LoanApplication';
}
public function getDefaultOptions(array $options)
{
return array(
//'data_class' => 'Acme\TaskBundle\Entity\Task',
'csrf_protection' => true,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
'intention' => 'loan_application',
);
}
}
Here is the controller:
namespace ABC\WebsiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JML\WebsiteBundle\Form\LoanApplication;
use JML\WebsiteBundle\QuickBase as QuickBase;
class ApplyController extends Controller
{
public function indexAction()
{
$form = $this->createForm(new LoanApplication());
return $this->render('ABCWebsiteBundle:Apply:index.html.twig', array(
'form' => $form->createView(),
));
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// perform some action, such as saving the task to the database
//return $this->redirect($this->generateUrl('task_success'));
}
}
}
}
And the twig:
<form action="{{ path('apply') }}" method="post" {{ form_enctype(form) }}>
<pre>
{{ dump(form_errors(form)) }} // always empty
</pre>
<div>
{{ form_label(form.first_name) }} // this renders
{{ form_widget(form.first_name) }} //empty
{{ form_row(form.first_name) }} //empty
</div>
<input type="submit" formnovalidate/>
</form>

Forms symfony2, display my created form in Symfony2

i need your help please , I want to display my created form in Symfony2. I want to display my created form 92 times becouse i have 92 numbers in my database(every number is a form) , i didn't know how to do it here is my code:
controller:
class DefaultController extends Controller
{
public function QuestionsAction(Request $request)
{
$questions = $this->getDoctrine()->getEntityManager()
->getRepository('Tests\TestsPhpBundle\Entity\Question')
->findAll();
$task = new Question();
$forms = $this->createForm(new QuestionType(), $task);
if ($request->getMethod() == 'POST') {
$forms->bindRequest($request);
if ($forms->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($task);
$em->flush();
}
}
{
return $this->render('TestsTestsPhpBundle:Default:index.html.twig', array(
'questions' => $questions,
'forms' => $forms->createView()
));
}
}
}
my form file:
class QuestionType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('categories', null, array('required' => false,
))
->add('text', 'entity', array(
'class' => 'TestsTestsPhpBundle:Question',
'query_builder' => function($repository) {
return $repository->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
'property' => 'text'))
;
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Tests\TestsPhpBundle\Entity\Question',);
}
public function getName()
{
return 'question';
}
}
my twig file:
{% block content %}
<h2>Questions</h2>
{% for question in questions %}
<dl>
<dt>Number</dt>
<dd>{{ question.number }}<dd>
{% for form in forms %}
{{ form_row(forms.categories) }}
{{ form_row(forms.text) }}
</dl>
{% endfor %}
<hr />
{% endfor %}
{% endblock %}
I recommend to read capter: Embedding Controller
http://symfony.com/doc/2.0/book/templating.html
<div id="sidebar">
{% render "AcmeArticleBundle:Article:recentArticles" with {'max': 3} %}
</div>
You can make a for loop within Twig Template and call an action (with parameter if needed) where you render the form. -> QuestionsAction in your case.

Resources