Symfony2 Twig extension: Creating form - forms

I wanted to have a contact-form-block that i can reuse on different pages and templates. So i decided to write a Twig extension. The problem is that i cant access the createFormBuilder() function. The second problem will be then that i cant access the request object for validation. My current code looks like this:
<?php
namespace Name\NameBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class ContactExtension extends \Twig_Extension
{
function getName() {
return 'contact_extension';
}
function getFunctions() {
return array(
'contactform' => new \Twig_Function_Method($this, 'contactform'),
);
}
function contactform() {
$form = $this->createFormBuilder()
->add('Name', 'text')
->add('Message', 'textarea')
->add('Send', 'submit')
->getForm();
return $this->render('NameBundle:forms:contactform.html.twig', array(
'form' => $form->createView(),
}
}
But i get error "Call to undefined method createFormBuilder()"...
Also i will get error if i change the function to function contactform(Request $request) { ... }
What do i need to add to use this function an object? Or maybe the twig extension is the completely wrong approach?

createFormBuilder() is a Controller helper that allows you to access the form.factory service within your controllers through the container (code below)
namespace Symfony\Bundle\FrameworkBundle\Controller;
// ...
class Controller extends ContainerAware
{
// ...
public function createFormBuilder($data = null, array $options = array())
{
return $this->container->get('form.factory')->createBuilder('form', $data, $options);
}
You're not in a "Controller context" here, so if you want to use the form.factory service within your extension you've to inject it.
BUT,
I'll not advice your to manage your contactForm this way (using a Twig Extension function). Why don't you just create a contactAction within the appropriate controller. You can then render your form in your templates using the twig render helper,
{{ render(controller('YourBundle:YourController:contactAction')) }}

If you use Symfony to make your code clear you should make Forms (src/forms) in diffrent file and just call it in your view and controller.

Related

CRUD using form: Updating

I've been getting errors when I press update.
I'm using the same code as adding.
I think it may be the picture upload that is causing the problem.
Here's the error:
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string.
You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.
My Controller:
public function updateAction(Request $request ,$id)
{
$em = $this->getDoctrine()->getManager();
$forum = $em->getRepository("ForumBundle:Forum")->find($id);
$forum->setModifiee(new \DateTime('now'));
$form = $this->createForm(ForumType::class, $forum);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form['image']->getData();
$file->move("images/", $file->getClientOriginalName());
$forum->setImage("images/" . $file->getClientOriginalName());
$em->persist($forum);
$em->flush();
return $this->redirectToRoute('forum_show');
}
return $this->render("#Forum/Sujet/Update_topic.html.twig", array("form" => $form->createView()));
}
My Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('titre')
->add('tags')
->add('image', FileType::class,array('attr' => array(
'class'=>'form-control'
//'class'=>'btn btn-default btn-file'
)))
->add('blog',TextareaType::class)
->add('Ajouter',SubmitType::class,array('attr' => array(
'class'=>'theme_button color3 wide_button'
)));
}
These are my includes:
namespace ForumBundle\Controller;
use blackknight467\StarRatingBundle\Form\RatingType;
use ForumBundle\Form\RateType;
use ForumBundle\Entity\Commentaire;
use ForumBundle\Entity\Forum;
use ForumBundle\Entity\Rating;
use ForumBundle\Form\CommentaireType;
use ForumBundle\Form\ForumType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\File;
According to the Symfony documentation : How to upload file, you have to set a File Object into Image setter, not a string. That'a why you have this error :
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string.
You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.
The rigth way to accomplish this is to set a new File instance to your image on your controller object, like this :
use Symfony\Component\HttpFoundation\File\File;
// ...
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$forum = $em->getRepository("ForumBundle:Forum")->find($id);
$forum->setModifiee(new \DateTime('now'));
$form = $this->createForm(ForumType::class, $forum);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$file = $form['image']->getData();
$file->move("images/", $file->getClientOriginalName());
$forum->setImage("images/" . $file->getClientOriginalName());
$forum->setImage(new File($this->getParameter('images_directory').'/'.$forum->getImage()));
$em->persist($forum);
$em->flush();
return $this->redirectToRoute('forum_show');
}
}
// ...
The parameter images_directory have to be set in the services.yml file, according to the symfony's documentation :
# config/services.yaml
# ...
parameters:
# depending of your symfony version
images_directory: '%kernel.project_dir%/web|public/uploads/images'
Another way is to use an uploader service or a dedicated bundle like : VichUploaderBundle or OneupUploaderBundle
Hope this help and have a nice day !!!
In symfony 2.8 in updateAction just correct define fileType for $id
I think it should look like this:
public function updateAction(Request $request ,$id) {
$id->setImage(
new File($this->getParameter('images_directory').'/'.$id->getImage()
));
...

Deprecated: ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer

I am getting below error message while using service manager.
How can i resolve this via different approach like constuct....
Deprecated: You are retrieving the service locator from within the
class Users\Controller\LoginController. Please be aware that
ServiceLocatorAwareInterface is deprecated and will be removed in
version 3.0, along with the ServiceLocatorAwareInitializer. You will
need to update your class to accept all dependencies at creation,
either via constructor arguments or setters, and use a factory to
perform the injections. in
C:\wamp64\www\ZendSkeletonApplication-master\vendor\zendframework\zend-mvc\src\Controller\AbstractController.php
on line 258
Below code i have added in module.php
public function getServiceConfig() {
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
// FORMS
'LoginForm' => function ($sm) {
$form = new \Users\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter'));
return $form;
},
)
)
}
and from login controller, index action i calling below code
$form = $this->getServiceLocator()->get('LoginForm');
$viewModel = new ViewModel(array('form' => $form));
return $viewModel;
Any help is highly appreciated.
Currently i am using Zend framework 2.5.1 Version
In Zend framework 2.3 Version it was working fine.
Update
Now i am using below code in my controller
// Add this property:
private $table;
// Add this constructor:
public function __construct(LoginForm $table) {
$this->table = $table;
}
and in module.php
// FORMS
Model\AlbumTable::class => function ($sm) {
$form = new \Users\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter'));
return Model\AlbumTable;
},
But still i am getting below error
Catchable fatal error: Argument 1 passed to
Users\Controller\LoginController::__construct() must be an instance of
Users\Form\LoginForm, none given, called in
C:\wamp64\www\ZendSkeletonApplication-master\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php
on line 252 and defined in
C:\wamp64\www\ZendSkeletonApplication-master\module\Users\src\Users\Controller\LoginController.php
on line 22
There was a lot of problem in the use of serviceLocator in ZF2, Zend tech' did a great job by removing the serviceLocatorAware from the framework, and remove the serviceManager from controllers.
Why ?
Just because some entry and experienced developpers used it in an ugly way, and way too much.
From my point of view, the serviceLocator is meant to be used only in factories.
That's why i keep advising other developper to create factories, without using anonymous function.
Here an example of a controller's factory (not the same as service's factories) : https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Factory/PostControllerFactory.php
And its config line https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/config/module.config.controllers.php#L8
And here a Service's factory
<?php
namespace Blog\Factory;
use Blog\Service\CategoryService;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class CategoryServiceFactory implements FactoryInterface
{
/**
* #param ServiceLocatorInterface $serviceLocator
* #return CategoryService
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
/** #var ObjectManager $em */
$em = $serviceLocator->get('orm_em');
return new CategoryService($em);
}
}
You can do a factory for almost all of your component, even form, you just need to declare those as factories in your config like this :
You can replace the key form_elements by :
controllers
service_manager
view_helpers
validators
It will works the same way :
'form_elements' => array(
'factories' => array(
'Application\Item\Form\Fieldset\ProfileFieldset' =>
'Application\Item\Factory\ProfileFieldsetFactory',
),
'invokables' => array(
'EntityForm' => 'Application\Entities\Form\EntityForm',
'PropertyForm' => 'Application\Item\Form\PropertyForm',
'ProfileForm' => 'Application\Item\Form\ProfileForm',
),
'initializers' => array(
'ObjectManagerInitializer' => 'Application\Initializers\ObjectManagerInitializer',
),
),
Your last error means that your controller is not correctly instanciated, you not give the LoginForm instance, maybe because you didn't create a factory ? Is your controller declared as an invokables ?
For an in depth discussion on deprecating the ServiceLocatorAwareInterface, please read this article by Matthew Weier O'Phinney. Basically, you should avoid hidden dependencies in your controllers by simply setter injecting them through factories as mentioned previously by Hooli.

CSRF field is missing when I embed my form with a requestAction in CakePHP 3

I want to embed a contact form in multiple places on my website.
I developed a contact form in a contact() function within my MessagesController.php:
// MessagesController.php
public function contact()
{
$this->set('title', 'Contact');
$message = $this->Messages->newEntity();
... // shortened for brevity
$this->set(compact('message'));
$this->set('_serialize', ['message']);
}
I loaded the CSRF component in the initialize() function of the AppController.php:
// AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('Csrf');
... // shortened for brevity
}
The form is rendered with a contact.ctp and it works fine.
I followed CakePHP's cookbook which suggests using requestAction() within an element, then echoing the element where I want it:
// contact_form.ctp
<?php
echo $this->requestAction(
['controller' => 'Messages', 'action' => 'contact']
);
?>
And:
// home.ctp
<?= $this->element('contact_form'); ?>
The problem is that the form is rendered fine, but the CSRF hidden field is missing. It should be automatically added to the form since the CSRF component is called in the AppController.php.
I guess either using an element with a requestAction() isn't the solution for this particular case, or I am doing something wrong.
Any ideas? Thanks in advance for the input!
Request parameters need to be passed manually
requestAction() uses a new \Cake\Network\Request instance, and it doesn't pass the _Token and _csrf parameters to it, so that's why things break.
While you could pass them yourself via the $extra argument, like
$this->requestAction(
['controller' => 'Messages', 'action' => 'contact'],
[
'_Token' => $this->request->param('_Token'),
'_csrf' => $this->request->param('_csrf')
]
);
Use a cell instead
I would suggest using a cell instead, which is way more lightweight than requesting an action, also it operates in the current request and thus will work with the CSRF component out of the box.
You'd pretty much just need to copy your controller action code (as far as the code is concerned that you are showing), and add a loadModel() call to load the Messages table, something like
src/View/Cell/ContactFormCell.php
namespace App\View\Cell;
use Cake\View\Cell;
class ContactFormCell extends Cell
{
public function display()
{
$this->loadModel('Messages');
$this->set('title', 'Contact');
$message = $this->Messages->newEntity();
// ... shortened for brevity
$this->set(compact('message'));
$this->set('_serialize', ['message']);
}
}
Create the form in the corresponding cell template
src/Template/Cell/ContactForm/display.ctp
<?php
echo $this->Form->create(
/* ... */,
// The URL needs to be set explicitly, as the form is being
// created in the context of the current request
['url' => ['controller' => 'Messages', 'action' => 'contact']]
);
// ...
And then wherever you want to place the form, just use <?= $this->cell('ContactForm') ?>.
See also
API > \Cake\Routing\RequestActionTrait::requestAction()
Cookbook > Views > Cells

Symfony 2 basic GET form generated URL

I have been trying to create an extremely basic symfony form (used for search functionality) with only one input. It uses GET method on submit. It seems to work as expected, however it generates an extremely ugly and unnecessarily long URL. I have been trying to 'clean' the URL up for a quite a while now, I was wondering if someone ran into the same problem and knows how to fix it?
Form
$form = $this->createFormBuilder($search)
->setMethod('GET')
->add('q', 'text')
->add('search', 'submit')
->getForm();
On submit the form generates the following URL:
search?form[q]=red+apple&form[search]=&form[_token]=bb342d7ef928e984713d8cf3eda9a63440f973f2
Desired URL:
search?q=red+apple
Thanks in advance!
To create your desired URL, you will have to set the form name by using createNamedBuilder which you'll just leave blank ''.
To remove _token you need to set csrf_protection to false. Please look into csrf protection to make sure you know what could happen if it is turned off.
Changing your code to the following should give you the results you want.
$form = $this->get('form.factory')->createNamedBuilder('', 'form', $search, array(
'csrf_protection' => false,
))->setMethod('GET')
->add('q', 'text')
->add('search', 'submit')
->getForm();
This should produce a URL like:
search?q=red+apple&search=
Edit:
If you want to get rid of &search=, one way would be to change search from submit to button.
->add('search', 'button')
This will require javascript to submit your form.
Here is simple example in jquery:
//This assumes one form and one button
$(document).ready(function(){
$('button').click(function(){
$('form').submit();
});
});
This will produce a URL like:
search?q=red+apple
To access GET vars you put something like this in your controller:
public function yourSearchAction(Request $request)
{
// your code ...
$form->handleRequest($request);
if ($form->isValid()) {
$getVars = $form->getData();
$q = $getVars['q'];
$page = $getVars['page'];
$billing = $em
//Do something
}
return //your code
}
Just to clarify if you are adding page to your URL you will need to add it to your form:
->add('page', 'text')
Old question but, for people who want to know, this does the job too (Symfony 2.8) :
<?php
// src/AppBundle/Form/SearchType.php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('GET')
->add('q', TextType::class)
->add('submit', SubmitType::class))
;
}
public function getBlockPrefix(){
return '';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
]);
}
}
In your controller :
<?php
//...
use AppBundle\Form\SearchType;
//...
public function yourSearchAction(Request $request)
{
$form = $this->createForm(SearchType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$q = $form->get('q')->getData();
// ...
}
// ...
}

How to add label into form builder (not in twig)?

I have this code, but it doesn't work:
$builder->add('name','text',array(
'label' => 'Due Date',
));
the problem i have in fosuserbundle, i have overring form
<?php
namespace Acme\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilder;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilder $builder, array $options)
{
// add your custom field
$builder->add('name','text',array(
'label' => 'Due Date',
));
parent::buildForm($builder, $options);
}
public function getName()
{
return 'acme_user_registration';
}
}
but not work, not give me any error and set the label "fos_user_registration_form_name"
You see label as fos_user_registration_form_name, because FOSUserBundle uses translations files to translate all texts in it.
You have to add your translations to file called like Resources/translations/FOSUserBundle.nb.yml (example for norwegian) or you can modify translations file coming with the bundle (copying it to Acme\UserBundle is a better way).