I have action, but when i send data, i have
Request-URI Too Long. The requested URL's length exceeds the capacity limit for this server
public function addAction(Request $request)
{
$productGallery = new ProductGallery();
$product = new Product();
$productGallery->addProductgalleryToProduct($product);
$form = $this->createForm(new ProductGalleryType(), $productGallery);
if($request->isMethod('POST'))
{
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($productGallery);
$em->persist($product);
$em->flush();
return $this->redirectToRoute('addAction', array('form' => $form->createView()));
}
}
return array(
'form' => $form->createView()
);
}
How i can fixed it? What i do wrong?
p.s my form collection
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('productgallery_to_product', 'collection', array(
'type' => new ProductType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true
))
;
}
NEW INFO
Method 'POST'
In my url
http://trololo.com/app_dev.php/add?form%5Bvars%5D%5Bid%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bname%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bfull_name%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bdisabled%5D=0&form%5Bvars%5D%5Bmultipart%5D=1&form%5Bvars%5D%5Bblock_prefixes%5D%5B0%5D=form&form%5Bvars%5D%5Bblock_prefixes%5D%5B1%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bblock_prefixes%5D%5B2%5D=_games_modelbundle_productgallery&form%5Bvars%5D%5Bunique_block_prefix%5D=_games_modelbundle_productgallery&form%5Bvars%5D%5Bcache_key%5D=_games_modelbundle_productgallery_games_modelbundle_productgallery&form%5Bvars%5D%5Bread_only%5D=0&form%5Bvars%5D%5Bvalid%5D=1&form%5Bvars%5D%5Brequired%5D=1&form%5Bvars%5D%5Bcompound%5D=1&form%5Bvars%5D%5Bmethod%5D=POST&form%5Bvars%5D%5Baction%5D=&form%5Bvars%5D%5Bsubmitted%5D=1&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bid%5D=games_modelbundle_productgallery_productgallery_to_product&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bname%5D=productgallery_to_product&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bfull_name%5D=games_modelbundle_productgallery%5Bproductgallery_to_product%5D&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bdisabled%5D=0&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bmultipart%5D=1&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bblock_prefixes%5D%5B0%5D=form&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bblock_prefixes%5D%5B1%5D=collection&form%5Bchildren%5D%5Bprodu.....
You're passing a whole form view object in the URL:
$this->redirectToRoute('addAction', array('form' => $form->createView()));
The second argument to redirectToRoute() is a list of GET parameters to send with the request.
This makes your URL VERY long. It exceeds the web server limit, which in turn refuses to handle the request.
Your call should look more like this:
$this->redirectToRoute('addAction');
Also, the first argument to the redirectToRoute() method is the route name, not the action method name. Replace it unless your route name is "addAction".
Read more in the Controller chapter of the documentation.
Related
How can I validate (server side) a form created and submitted from Vue?
I have a simple contact form, and I want to validate it both client and server side. For the client mode I'm using vee-validate (and it works), for the server side mode I want to use the Symfony's form validation.
So in this "special" case, the form is not rendered using the methods from Symfony, the form in this case is used only for validation. I have already created the form (it isn't linked to an Entity object), but when I send a http post request from my Vue's component, the validation on server side doesn't work. Seems that doesn't "read" the constraints created in the FormTypeclass.
My function:
public function sendContactUsEmailAction(Request $request, Mailer $mailer, TranslatorInterface $translator)
{
try {
$form = $this->createForm(ContactUsType::class);
$form->submit($request->request->all());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$emailParams = new ContactUsParams();
$emailParams->setName($data['name']);
$emailParams->setEmail($data['email']);
$emailParams->setSubject($data['subject']);
$emailParams->setMessage($data['message']);
$email = new ContactUsMail($mailer, $emailParams);
$email->send();
return new JsonResponse($translator->trans('send_contact_us_email_response'));
}
return new JsonResponse($this->getFirstFormError($form), 400);
} catch (\Exception $e) {
return new JsonResponse($e->getMessage(), 500);
}
}
Edited
This is the dump of my request data:
array:4 [
"name" => "Example"
"email" => "foo#example.com"
"subject" => "Test subject"
"message" => "Test message"
]
This is my FormType class. To test it I added for the name property the constraint "Email", but from the request I didn't send a valid email.
class ContactUsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'constraints' => array(
new NotBlank(),
new Email(),
),
))
->add('email', EmailType::class, array(
'constraints' => array(
new NotBlank(),
new Email(),
),
))
->add('subject', TextType::class, array(
'constraints' => new NotBlank(),
))
->add('message', TextareaType::class, array(
'constraints' => new NotBlank(),
));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
'validation_groups' => false,
]);
}
}
I've found the solution. The problem was caused by the option "validation_groups" setted to false. I removed it and now works!
I'm beginner on symfony and I have to use the collection form to make a site to reserve tickets for a show.
The interface is simple, The user select the number of tickets he wants, then it display as much form prototype (name and surname input) as tickets required.
This part works well for me.
I have 2 table "reservations" and "ticket" in mysql and I would like to get this render after submiting the form:
Table reservation :
Reservation id = 1
Table ticket:
Ticket id="1" name="thomas", surname="good", reservation_id="1"
Ticket id="2" name="laura", surname="senior", reservation_id="1"
But when I submit my form, I get +1 reservation in my table but no data in my ticket table.
class AdvertController extends Controller
{
public function formAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$reservation = new Reservations();
$form = $this->createForm(new ReservationsType(), $reservation, array(
'action' => $this->generateUrl('louvre_pages_homepage'),
'method' => 'POST',
));
$form->handleRequest($request);
$em->persist($reservation);
$em->flush();
return $this->render('LouvrePagesBundle:Pages:index.html.twig', array(
'form' => $form->createView(),
));
}
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tickets', CollectionType::class, array(
'entry_type' => TicketType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
)
)
->add('save', 'submit', array(
'attr' => array(
'class' => 'btn btn-lg btn-success'
)
));
}
public function getName()
{
return 'reservations';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Louvre\PagesBundle\Entity\Reservations',
));
}
Is my controller right ?
I see 2 problems in the formAction:
there is no call to $em->flush(); anywhere
you are always calling $em->persist($reservation); regardless of the form validation state
Try doing it this way:
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($reservation);
$em->flush();
// now redirect, add flash message, etc.
}
See http://symfony.com/doc/current/book/forms.html#handling-form-submissions
I have contact Entity, making post call using:
postContactAction(Request $request)
Generated form using app/console generate:doctrine:form BundleName:Contact
In post function trying to get request and set entity to request parameters like name, email and etc:
$contact = new Contact();
$form = $this->createForm(new ContactType(), $contact, array(
'method' => 'POST'));
$form->handleRequest($request);
And while checking, var_dump($contact); returns fields with null value.
What can be the problem?
Using postman to send post request and it worked without form.
In ContactType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('email', 'text', array('required' => false))
->add('phone', 'integer', array('required' => false))
->add('text', 'textarea')
->add('subject', 'text')
->add('createdAt', 'datetime', array('required' => false))
;
}
In ContactController:
public function postContactAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$this->initErrorContainer();
$validator = $this->get('validator');
$contactDetails = $this->getRequest()->request->all();
// check if email and phone exists or not. One is enough
if (!array_key_exists('email', $contactDetails) && !array_key_exists('phone', $contactDetails)) {
$this->errorContainer->createAndAdd('email', $this->errorContainer->MESSAGE_REQUIRED);
$this->errorContainer->createAndAdd('phone', $this->errorContainer->MESSAGE_REQUIRED);
return $this->getView();
}
$contact = new Contact();
// handle form
$form = $this->createForm(new ContactType(), $contact, array(
'method' => 'POST'));
$form->handleRequest($request);
// set default values
var_dump($contact);
die;
Solved. As Entity uses different types than request, you need to create simply model of this entity, set request fields to that entity type using handleRequest, and then you can use it.
Is weird, but can you try to get the form after call createForm to see if that has something related, namely:
you have:
$form = $this->createForm(new ContactType(), $contact, array('method' => 'POST'));
Then just add:
$form = $this->createForm(new ContactType(), $contact, array('method' => 'POST'))->getForm();
I'm trying to write a simple restful controller for user management in Symfony using FosRest and Symfony forms. My application is backed by Amazon DynamoDB, although I don't think it matters.
I have DELETE, GET and POST (new user) all working perfectly.
I've now come to writing the PUT action (edit user) which doesn't seem to work. I've spent ages banging my head against a brick wall and I just can't work it out.
In order to create the PUT, I essentially copied the POST action but modified it to load the old object first.
In the POST, the User object automatically gets populated by the line $form->handleRequest($request);
This doesn't seem to be working in the PUT action, the user object doesn't get populated/modified. I've checked the $_REQUEST array and the data is being submitted. Because of the lack of browser support for PUT, I'm calling the action by doing a POST of the data with the query parameter _method=PUT (which works fine for DELETE, and it is routing to the correct place).
Here is my POST action that works:
public function postUsersAction(Request $request)
{
$user = new User();
$user->setTable($this->getTable());
$formBuilder = $this->createFormBuilder($user, array(
'validation_groups' => array('registration', '')))
->add('username', 'text')
->add('password', 'password')
->setAction($this->generateUrl('post_users'))
->setMethod('POST')
->setAttribute('validation_groups', array('registration'));
$roles = $this->getFlattenedRoles($this->getRoles());
$formBuilder->add('roles', 'choice', array(
'choices' => $roles,
'multiple' => true,
'expanded' => true
));
$form = $formBuilder->add('save', 'submit')->getForm();
$form->handleRequest($request);
if ($form->isValid())
{
$user->save();
$params = array('user' => $user);
$view = $this->view($params, 200)
->setTemplate("MyRestBundle:User:newconfirm.html.twig");
return $this->handleView($view);
}
$params = array('form' => $form, 'user' => $user);
$view = $this->view($params, 400)
->setTemplate("MyRestBundle:User:new.html.twig");
return $this->handleView($view);
}
Here is my PUT controller that doesn't:
public function putUserAction($slug, Request $request)
{
$table = $this->getTable();
$user = $table->load($slug);
$formBuilder = $this->createFormBuilder($user)
->add('password', 'password')
->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
->setMethod('POST');
$roles = $this->getFlattenedRoles($this->getRoles());
$formBuilder->add('roles', 'choice', array(
'choices' => $roles,
'multiple' => true,
'expanded' => true
));
$form = $formBuilder->add('save', 'submit')->getForm();
$form->handleRequest($request);
if ($form->isValid())
{
$user->save();
$params = array('user' => $user);
$view = $this->view($params, 200)
->setTemplate("MyRestBundle:User:newconfirm.html.twig");
return $this->handleView($view);
}
$params = array('form' => $form, 'user' => $user);
$view = $this->view($params, 400)
->setTemplate("MyRestBundle:User:new.html.twig");
return $this->handleView($view);
}
Any help would be greatly appreciated.
Okay, after some help in the Symfony IRC channel, it turns out the reason this isn't working is this part of the code:
$formBuilder = $this->createFormBuilder($user)
->add('password', 'password')
->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
->setMethod('POST');
Since PUT doesn't work in many browsers, I was trying to set the method as POST and pass _method=PUT as a query parameter to override the real HTTP method. It turns out there is no need to do this, and Symfony will handle it all for you. The above code is now just:
$formBuilder = $this->createFormBuilder($user)
->add('password', 'password')
->setAction($this->generateUrl('put_user', array('slug' => $slug))
->setMethod('PUT');
By doing this, Symfony actually renders a POST form with a hidden _method field - there's no need to do anything manually.
So I have read that I can set the default value of a field in symfony2 formbuilder by doing this
->add('myfield', 'text', array(
'label' => 'Field',
'data' => 'Default value'
))
Now I want to manipulate the value that would be here by default, in order to know what to set, I need to know what was originally passed, so can I get the default value like
myfield.default_value
so that i can later manipulate it and set the result of my manipulation as the real default value?
Addition:
I tried
->add('username', 'text', array('label' => 'Användarnamn ', 'data' => 'my_own_defaultvalue'))
That didn't work for me (seen it in a bunch of forums though...).
Then I realized that probably the easiest way is to just edit the object before I pass it to the formBuilder, like this:
public function editUserAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVUserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Container entity.');
}
//run some action to modify one of the variables of $entity
$entity->setUsername($username_stripped); //put my edited variable into the object, now we're ready to pass my new and improved $entity to the formBuilder...
$editForm = $this->createForm(new editUserType(), $entity);
return $this->render('BizTVUserBundle:Default:editUser.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}