Symfony - Form redirect - symfony

I am building simple car renting app in Symfony for a programming class at my university.
On URL /search I show a form to user. Right now when the form is submitted user his /search again but different template is rendered
This is form code:
class SearchQueryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('pickupCity', TextType::class)
->add('returnCity', TextType::class)
->add('pickupDateTime', DateTimeType::class, array(
'years' => range(2016,2017),
'error_bubbling' => true,
))
->add('returnDateTime', DateTimeType::class, array(
'years' => range(2016,2017),
'error_bubbling' => true,
))
->add('save', SubmitType::class, array(
'label' => 'Find Car',
'attr' => array(
'class' => 'btn-secondary btn-lg'
)))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\SearchQuery',
));
}
}
And controller for this route:
class SearchController extends Controller
{
public function searchAction(Request $request)
{
$query = new SearchQuery();
$form = $this->createForm(SearchQueryType::class, $query);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/* Some logic here */
return $this->render('AppBundle:default:results.html.twig', array(
'form' => $form->createView()
));
}
return $this->render('AppBundle:default:search.html.twig', array(
'form' => $form->createView()
));
}
}
However when the form is submitted I want to redirect to URL /results?SUBMITTED-PARMS-HERE, so user can send this link to someone and receive the same results. On /results again I wand to render form, this time filled with search params submitted and below render available car.
I don't know if its the best way to handle this problem, current solution works but link can't be send to someone else.
EDIT:
I change the code to fallowing:
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$session = $this->get('session');
$session->set('pickupCity', $data->getPickupCity());
$session->set('returnCity', $data->getReturnCity());
$session->set('pickupDateTime', $data->getPickupDateTime());
$session->set('returnDateTime', $data->getReturnDateTime());
return $this->redirectToRoute('results', array(
'pickupCity' => $data->getPickupCity(),
'returnCity' => $data->getReturnCity(),
'pickupDate' => $data->getPickupDateTime()->format('d-m-Y-H-i'),
'returnDate' => $data->getReturnDateTime()->format('d-m-Y-H-i'),
), 301);
}
So as a result I receive URL ./results/Paris/Berlin/10-02-2016-10-00/17-02-2016-10-00
And in routing.yml:
results:
path: /results/{pickupCity}/{returnCity}/{pickupDate}/{returnDate}
defaults: { _controller: AppBundle:Search:result}
methods: [POST]
Because I only want to receive POST request on this route and now when I submit form I get error page saying:
No route found for "GET /results/Paris/Berling/01-07-2016-00-00/01-11-2016-00-00": Method Not Allowed (Allow: POST)
Any idea how to fix it?
EDIT 2:
Never mind, my reasoning was bad.

Not sure, but maybe using this example.
$this->redirect($this->generateUrl('default', array('pickupCity' => $pickupCity, 'returnCity' => $returnCity, 'pickupDateTime' => $pickupDateTimepickupDateTime)));
With your own parameter, which you can get where you put your comment
/* Some logic here */

You can use getQueryString() function.
Sample:
if ($form->isSubmitted() && $form->isValid()) {
return $this->redirect('results?'.$request->getQueryString());
}

Related

Symfony4: Password Changing Issues

I am stuck on how to change passwords by form submission in symfony - and I have no idea how to solve it, since it seems to behave differently.
So first of all - I am building a custom form, where a user enters their old password, and a new one twice. This is my FormType:
UserPasswordType.php
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
class UserPasswordType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//basically just a unmapped password field
->add('password_current', PasswordType::class, [
'label' => 'user.password.current',
'mapped' => false,
/*'constraints' => new UserPassword()*/
])
//RepeatedType to get a confirm password field
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'first_name' => 'new',
'first_options' => ['label' => 'user.password.new'],
'second_name' => 'confirm',
'second_options' => ['label' => 'user.password.confirm']
])
->add('save', SubmitType::class, ['label' => 'app.save']);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class
]);
}
}
And here is my controller:
/**
* #Route("/user/settings", name="app_user_settings", defaults={"_locale":"%locale%"})
* #IsGranted("ROLE_USER")
*/
public function user_settings(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$user = $this->getUser();
$form = $this->createForm(UserPasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
//earlier: $current = $request->get('password_current'); - but does not work anymore?
$current = $form['password']->getData();
if ($passwordEncoder->isPasswordValid($user, $current)) { //fails always?
$confirm = $form['password_confirm']->getData();
$user->setPassword($passwordEncoder->encodePassword($user, $confirm));
$em->persist($user);
$em->flush();
$this->addFlash('success', 'status.success.text');
return $this->redirectToRoute('app_user');
}
$this->addFlash('danger', 'status.danger.text');
}
return $this->render('user/settings/index.html.twig', ['form' => $form->createView()]);
}
So here are my problems:
for some reason $current = $request->get('password_current') stopped working - it always returns null
$passwordEncoder->isPasswordValid($user, $current) also stopped working - was fine earlier now it literally always returns false
the constraint UserPassword() in my formtype on password_current also keeps failing validation, even though the password is correct
the logged-in user who tries to change their password keeps getting kicked (logged) out when trying to change the password (the form gets reloaded first, then after any action he returns to the login) - but just when I check the old password - without checking the old password the user stays logged in and gets the success flash message
Sadly I am really confused right now, because most of my stuff worked yesterday. Anyone knows how to approach those issues? Thanks
Well, I finally found the solution. I mapped the form type to the entity. So whenever I enter a new password, my "old" password actually is the new one. This makes every validation fail.
So just make a unmapped form type like this:
FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('password_current', PasswordType::class, ['label' => 'user.password.current', 'constraints' => new UserPassword()])
->add('password', RepeatedType::class, ['type' => PasswordType::class, 'first_name' => 'new', 'first_options' => ['label' => 'user.password.new'], 'second_name' => 'confirm', 'second_options' => ['label' => 'user.password.confirm']])
->add('save', SubmitType::class, ['label' => 'app.save']);
}
/* Remove entire function
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class
]);
}*/
Controller
//remove $user in createForm function
//$form = $this->createForm(UserPasswordType::class, $user);
$form = $this->createForm(UserPasswordType::class);
Funny side-effect. This also removes all other issues I had.

twig how to render a twig template as a variable

I'm trying to render twig template as variable, using symfony. I have a 'sendAction' Controller, which uses the mailgun API to send emails to one or more mailing lists. Here is my code for the Controller:
public function sendAction(Request $request, Newsletter $newsletter, MailgunManager $mailgunManager) {
$form = $this->createForm(SendForm::class);
$form->handleRequest($request);
$formData = array();
if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData();
$mailingLists = $formData['mailingLists'];
foreach ($mailingLists as $list) {
$mailgunManager->sendMail($list->getAddress(), $newsletter->getSubject(), 'test', $newsletter->getHtmlContent());
return $this->render('webapp/newsletter/sent.html.twig');
}
}
return $this->render('webapp/newsletter/send.html.twig', array(
'newsletter' => $newsletter,
'form' => $form->createView()
));
}
}
And here's my sendMail (mailgun) function:
public function sendMail($mailingList, $subject, $textBody, $htmlBody) {
$mgClient = new Mailgun($this::APIKEY);
# Make the call to the client.
$mgClient->sendMessage($this::DOMAIN, array(
'from' => $this::SENDER,
'to' => $mailingList,
'subject' => $subject,
'text' => $textBody,
'html' => $htmlBody
));
}
I want my ' $newsletter->getHtmlContent()' to render template called 'newsletter.twig.html'. can anyone help me or point me in the right direction as to what I can do or Where I can find Tutorials or notes on what I am trying to do. the symfony documentation is quite vague.
You can use getContent() chained to your render function.
return $this->render('webapp/newsletter/send.html.twig', array(
'newsletter' => $newsletter,
'form' => $form->createView()
))->getContent();
Simply inject an instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface into your action, and you’ll be able to use Twig directly:
public function sendAction(Request $request, EngineInterface $tplEngine, Newsletter $newsletter, MailgunManager $mailgunManager)
{
// ... other code
$html = $tplEngine->render('webapp/newsletter/send.html.twig', [
'newsletter' => $newsletter,
'form' => $form->createView()
]);
}
Note that $this->render() (in the controller action) will return an instance of Symfony\Component\HttpFoundation\Response, while $tplEngine->render() returns a HTML string.

Can a Symfony 2 Action be made initially accessible only from another Action?

I struggled to express what I mean in the title of the question! I'll do my best to make more sense here...
Symfony 2.7
I have a Form, which when it is submitted and successfully validated I would like to feed into a second, independent Form, for further user activity. I would like initial values in the second form to be provided by the first, but that second form then to be independent, e.g. it can go through its own separate submission/validation steps.
I do not want:
it to be possible for a user to go straight to the second form
to have to pass values for the second form as querystring parameters
to use Javascript to achieve this
to persist data to a DB in the first form and pick it up in the second
Conceptually, I would like to be able to validate the first form, and then in the Controller to pass the data received to a different Action, which would show the new Form to the user. Further user submissions would then be handled by the new Action. I feel like this should be possible, I'm just not sure how to make it happen! In a way I'd like the second Action to be private, but it does need to be public so that the second form can be submitted. I'd like to be able to pass data to the second Action directly using an object, but I don't want to expose that entry point as a standard Route.
Thanks in advance.
Sorry for any lack of clarity in the question. Here's how I solved it (I'd still be interested in any different/better solutions):
I created a separate FormType (ReportConfirmType) and Action (ConfirmAction) for the second step. ReportConfirmType has the type of Data Class, and essentially all the same fields as the original FormType (ReportType), but with them all marked readonly. The route is very similar. I also created a private method to act as the "glue" between the first and second steps.
When I'm finished with my first step, I then call the private method, passing it the validated data from the first step (which can be used unchanged). This method sets up the second form and returns the second view. The action of the form needs to be changed to that of the second route.
All subsequent submissions will go to the new route, and when the second form validates I can carry out the final activities of the process.
Here's some example code to illustrate further:
ReportType
class ReportType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('completedBy', 'text')
->add('comments', 'textarea', ['required' => false])
->add('format', 'choice', ['choices' => ['pdf' => 'PDF', 'word' => 'MS Word'] ])
->add('save', 'submit', ['label' => 'Submit', 'attr' => ['class' => 'btn btn-primary']])
->getForm();
}
...
ReportConfirmType
class ReportConfirmType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', ['attr' => ['readonly' => 'readonly']])
->add('completedBy', 'text', ['attr' => ['readonly' => 'readonly']])
->add('comments', 'textarea', ['required' => false, 'attr' => ['readonly' => 'readonly']])
->add('format', 'choice', ['choices' => ['pdf' => 'PDF', 'word' => 'MS Word'], 'attr' => ['readonly' => 'readonly'] ])
->add('agree', 'checkbox', ['mapped' => false, 'label' => 'I agree', 'constraints' => [new IsTrue()]])
->add('save', 'submit', ['label' => 'Submit', 'attr' => ['class' => 'btn btn-primary']])
->getForm();
}
...
ReportController
class ReportController extends Controller
{
public function indexAction(Request $request, $id)
{
$form = $this->createForm(new ReportType(), new ReportDetails() );
$form->handleRequest($request);
if ($form->isValid()) {
return $this->confirmPseudoAction($id, $form);
}
return $this->render('Bundle:Report:index.html.twig', ['form'=> $form->createView()]);
}
private function confirmPseudoAction($id, \Symfony\Component\Form\Form $form)
{
$action = $this->generateUrl('form_confirm_report', ['id' => $id]);
$confirmForm = $this->createForm(new ReportConfirmType(), $form->getData(), ['action' => $action]);
return $this->render('Bundle:Report:confirm.html.twig', ['form'=> $confirmForm->createView()]);
}
public function confirmAction(Request $request, $id)
{
$form = $this->createForm(new ReportConfirmType(), new ReportDetails() );
$form->handleRequest($request);
if ($form->isValid()) {
return $this->generateReport($id, $form->getData());
}
return $this->render('Bundle:Report:confirm.html.twig', ['form'=> $form->createView()]);
}
...
routing.yml
form_report:
path: /form/{id}/report
defaults: { _controller: Bundle:Report:index }
requirements:
id: \d+
form_confirm_report:
path: /form/{id}/reportConfirm
defaults: { _controller: Bundle:Report:confirm }
requirements:
id: \d+
And this does what I want! There may be an easier way, but I've done it now...
I believe you can use dynamic generation for submitted forms.
It allows you to customize the form specific to the data that was submitted by the user

Symfony2 form filter for ManyToMany entity relation

I have 2 entities:
class Exercise
{
//entity
}
and
class Question
{
//entity
}
Their relationship is ManyToMany.
The question is: How I can filter (on the exercise form) the Question entities?
I have this form for Exercise:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('questions', null, array(
'property' => 'name',
'multiple' => true,
'expanded' => false,
'query_builder' => function(EntityRepository $er) use ($options) {
return $er->createQueryBuilder('u')
->where('u.level = :level')
->setParameter('level',$options['level'])
->orderBy('u.dateR', 'ASC');},
))
//other fields
->add('Save','submit')
;
}
I know that I can filter with the query_builder but, how I can pass the var $search?
Should I create another form?
Any help would be appreciated.
Best regards
EDIT: I have found the solution that I want. I put 2 forms on the Controller Action, one form for the entity and one form for filter.
//ExerciseController.php
public function FormAction($level=null)
{
$request = $this->getRequest();
$exercise = new Exercise();
//Exercise form
$form = $this->createForm(new ExerciseType(), $exercise,array('level' => $level));
//create filter form
$filterForm = $this->createFormBuilder(null)
->add('level', 'choice', array('choices' => array('1' => '1','2' => '2','3' => '3')))
->add('filter','submit')
->getForm();
//Manage forms
if($request->getMethod() == 'POST'){
$form->bind($request);
$filterForm->bind($request);
//If exercise form is received, save it.
if ($form->isValid() && $form->get('Save')->isClicked()) {
$em = $this->getDoctrine()->getManager();
$em->persist($exercise);
$em->flush();
return $this->redirect($this->generateUrl('mybundle_exercise_id', array('id' => $exercise->getId())));
}
//If filter form is received, filter and call again the main form.
if ($filterForm->isValid()) {
$filterData = $formFiltro->getData();
$form = $this->createForm(new ExerciseType(), $exercise,array('level' => $filterData['level']));
return $this->render('MyBundle:Exercise:crear.html.twig', array(
'ejercicio' => $ejercicio,
'form' => $form->createView(),
'formFiltro' => $formFiltro->createView(),
));
}
}
return $this->render('juanluisromanCslmBundle:Ejercicio:form.html.twig', array(
'exercise' => $exercise,
'form' => $form->createView(),
'formFiltro' => $filterForm->createView(),
));
}
Templating the forms
On the form.html.twig
{{ form_start(filterForm) }}
{{ form_errors(filterForm) }}
{{ form_end(filterForm) }}
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_end(form) }}
It works for me and it is that i was looking for.
What you probably want to do here, is to make use of the form builder:
$search = [...]
$form = $this->createForm(new AbstractType(), $bindedEntityOrNull, array(
'search' => $search,
));
here you can provide any list of arguments to the builder method of your AbstractType.
public function buildForm(FormBuilderInterface $builder, array $options)
as you may have guessed at this point you may access the options array throu the $option variable.
As a side note remember to provide a fallback inside the default option array. In your AbstractType you can do something like this:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'search' => 'some_valid_default_value_if_search_is_not_provided'
));
}
hope it helps, regards.

Symfony2 FormBuilder with Entity class

I have a form that works well, there is just one issue with it and I'm hoping that I'll get an answer on how to do what I need to do.
<?php
namespace ADS\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\SecurityContext;
class UserType extends AbstractType {
private $type;
public function __construct($type) {
$this->type = $type;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('firstName', 'text', array('required' => true))
->add('lastName', 'text', array('required' => true));
$builder->add('email', 'email', array('required' => true));
$builder->add('parentCompany', 'entity', array(
'class' => 'ADSUserBundle:Company',
'expanded' => false,
'empty_value' => 'CHOOSE ONE',
'required' => false,
'property' => 'companyName'
))
->add('enabled', 'choice', array('choices' => array('1' => 'Enabled', '0' => 'Disabled')))
->add('roles', 'entity', array(
'class' => 'ADSUserBundle:Roles',
'required' => true,
'property' => 'displayName',
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array('data_class' => 'ADS\UserBundle\Entity\User'));
}
public function getName() { return 'ads_userbundle_user'; }
}
I have this form, the portion I am looking at is the 'roles' portion... Right now it created a multiple select box ( as I expect it to ), though the value is sequentially ie: 0,1,2,3,4...
What I really need is to figure out how to take this entity, and make the property to be the displayName ( as it is now ) and get the value to be the corresponding internalName This way it'll give me an array like:
array('ROLE_EXAMPLE' => 'EXAMPLE', 'ROLE_EXAMPLE1' => 'EXAMPLE1')
Any ideas how to accomplish this is greatly appreciated.
Kamil Adryjanek is correct, it is going to be much easier if you change it from an entity to a choice field. I've done some testing, both with FOSUserBundle and without the bundle - in both cases I hit some interesting road blocks.
First, I tried to run it through QueryBuilder in a repository, that didn't work out as it should have. The reason being, the fact that you wanted to be returning an array instead of a ORM object causes an error.
So next, I started looking at creating the choice field. All the guides, say to use the fieldname role instead of roles so I tried that, but I then had to duplicate the UserInterface from FOSUserBundle - I didn't want to do that -- so here I am stressed, and trying to figure it out.
Here is what I ended up doing, which works well.
private $normalRoles = array();
then in the __construct I add: $this->normalRoles = $roles;
Here is the builder:
$builder
->add('roles', 'choice', array(
'multiple' => true,
'choices' => $this->normalRoles
))
;
Originally, I left the multiple part out, figuring that it'd at least let me see an option box. I ended up getting an Array to String conversion error. So, adding the 'multiple' => true in, fixes that error.
Then, in my repository I created a function called normalizeRoles
public function normalizeRoles() {
$data = array();
$qb = $this->getEntityManager();
$query = $qb->createQuery(
"SELECT r.internalName, r.displayName FROM AcmeUserBundle:Roles r"
)->getArrayResult();
foreach ($query as $k => $v) {
$data[$v['internalName']] = $v['displayName'];
}
return $data;
}
From here, we just have to make some small edits in the DefaultController of the UserBundle in the newAction and editAction ( both are the same changes )
So, first off is to put into your Controller use Acme/UserBundle/Entity/Roles in order to avoid any errors and be able to get that repository.
Next, right before you create the form you run the normalizeRoles() function
$roles = $em->getRepository('AcmeUserBundle:Roles')->normalizeRoles()
Then, you pass it through the construct via: new UserType($roles)
full line for that would look like this:
$form = $this->createForm(new UserType($roles), $entity, array(
'action' => $this->generateUrl('acmd.user.edit', array(
'id' => $id)
)
));
or for new:
$form = $this->createForm(new UserType($roles), $entity, array(
'action' => $this->generateUrl('acmd.user.new')
)
));
At this point -- You'll have a working system that will allow you to dynamically add roles into a database table, and then associate those with a new or current user.
You can try do it via query_builder attribute:
$builder->add('roles', 'entity', array(
'class' => 'ADSUserBundle:Roles',
'required' => true,
'property' => 'displayName',
'query_builder' => function (RolesRepository $queryBuilder) {
return $queryBuilder->someMethod() // some method in this repository that return correct query to db.
},
));
In this case it would be better to use choice field Type (http://symfony.com/doc/current/reference/forms/types/choice.html) instead of entity and pass some role choices as option to form because entity field Type get entity id as key for choices:
public function buildForm(FormBuilderInterface $builder, array $options) {
...
$builder->add('roles', 'choice', array(
'choices' => $options['role_choices']
));
...
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'ADS\UserBundle\Entity\User',
'role_choices' => array()
));
}
Notice: it's recommended to pass variables to form through options parameter, not in constructor.
if I understand your question correctly, you need a data transformers. They help you to show data in form as you want.
Documentation: http://symfony.com/doc/current/cookbook/form/data_transformers.html

Resources