Redactor is a very nice WYSIWYG editor. In this post I
will show you how to create a redactor form type. Let’s get into the code.
First up we need to create our RedactorType class.
I have left the docblocks in that can be found in the source code.
In the method getDefaultOptions we need to setup the options. In this case, we
just define one and the default is an empty array.
NOTE We could have defined this as settings or something other than
options. We could also set the default value to null and refactor the code
Since Redactor comes with tons of options, we don’t want to define each and
everyone with the default. The way it’s setup we can use this in a form to pass
will json_encode the array to get a string. We
will use this when making the twig template.
Next up we need to register the class and let the framework know that we have a
new form type.
We need to create a form template to use with our new form type.
NOTE Make sure you have redactor.css located in the directory
src/Acme/AppBundle/Resources/public/css and redactor.js in
src/Acme/AppBundle/Resources/public/js directory. You can always have these
in your global css/js folders. Also make sure you include jQuery above the
Notice that we need to filter form.vars.redactor_settings. Symfony2 will
escape the output and break the functionality we want.
One more file to let the framework know to use the above block.
That’s all there is to it. Now start using it in your forms.