I installed TRsteelCkeditorBundle, when I'm building in the view a form manually, it works.
<form method="post">
<p>
My Editor:<br>
<textarea name="editor1"><p>Initial value.</p></textarea>
<script>
CKEDITOR.replace( 'editor1' );
</script>
</p>
<p>
<input type="submit">
</p>
</form>
But when I try to use the Symfony2 form I get this error :
An exception has been thrown during the rendering of a template
("Route "route_name" does not exist.") in
lbTestBundle:Default:index.html.twig at line 8
My codes :
In the Twig template
{{ form_widget(form) }}
Ligne 8 is the form_widget
In the controller:
public function indexAction()
{
$formBuilder = $this->createFormBuilder();
$formBuilder
->add('date', 'date')
->add('title', 'text')
->add('content', 'ckeditor')
->add('author', 'text');
$form = $formBuilder->getForm();
$view['form'] = $form -> CreateView();
return $this->render('lbTestBundle:Default:index.html.twig', $view);
}
I followed step by step the indications of the Read_me.txt in he github page, and try to find answers elsewhere but found nothing. If someone can help me to solve that problem, i'm still beginner with SF2. Thanks
What is at lbTestBundle:Default:index.html.twig at line 8?
Obviously you have a route route_name in you template which is not defined in you routing.yml file.
That's all the error message says.
Same problem here. Spend all day today researching.
Finally, fixed it by replacing the TRsteelCkeditor bundle with the IvoryCKEditor bundle.
I found where the error comes from.
the route_name comes from
#config.yml for TrsteelCkeditor
filebrowser_image_browse_url:
route: route_name
route_parameters:
type: image
I suppose I have to put the route where the editor goes when you want to upload images in the editor.
I'll continue to test this budle till I got something fine and I'll give the result
Adding in comment the line
#route: route_name
In the config.yml solve the problem, this line is for uploading images on the server I think, see this link :
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)
But now I am having some issues with language, I want to put it in French, at the moment the skin seems language dependent just like the hover on the buttons.
Also if you want to configure it, you can do it either in the config.yml or the config.js in /web/bundle repository if you already install the assets.
Related
I am a little bit confused. First of all, look at my code, I guess.
public function renderTemplate($templateType, $data)
{
$layoutName = "$templateType.layout.html.twig";
$policy = new \Twig_Sandbox_SecurityPolicy(
['if', 'for', 'block', 'set', 'extends'],
['escape', 'format', 'dateformat', 'trans', 'raw', 'striptags'],
self::$allowedMethods,
self::$allowedProperties,
['gettext']
);
$sandboxExt = new \Twig_Extension_Sandbox($policy);
$intlExt = new \Twig_Extensions_Extension_Intl();
$i18nExt = new \Twig_Extensions_Extension_I18n();
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__ . "/../Resources/views/Something/", "__main__"));
$sandboxExt->enableSandbox();
$twig->addExtension($sandboxExt);
$twig->addExtension($intlExt);
$twig->addExtension($i18nExt);
try {
$result = $twig->render($layoutName, $data);
} catch (\Exception $e) {
\Doctrine\Common\Util\Debug::dump($e);die();
}
return $result;
}
Here it is the template I want to render
{% extends 'layout.html.twig' %}
{% block title %}{{ entity.id }}{% endblock %}
{% block bodyTitle %}
{{ entity.id }} {{ 'translation_key.created_at'|trans({}, 'entities', locale) }} {{ entity.createdAt|dateformat(null, locale) }}
{% endblock %}
Here, as you can see, I want to render a template according to its type. The problem is: half of the template renders just fine, and then, when it tries to render the translated string, it throws an error.
Fatal error: Call to undefined function gettext() in /home/dev/vhosts/my-project/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code on line 69
I checked if this function existed right before trying to call render method, and it was really undefined. Basically, I have 2 questions here:
Question 1
How does it work in other parts of my project, but not in this specific handler? See "Important update" below.
Question 2
Can I solve my problem in another way? For example, without using Sandbox or using Sandbox with some kind of a flag "everythingAllowed=true"?
ATTENTION! IMPORTANT UPDATE
Previously, I misunderstood my own question. I thought the error was thrown while rendering the variable, but I re-checked the situation (When Alain Tiemblo asked me for twig template code here in comments) and now 100% sure it's thrown while trying to translate smth. Also, I have translations all over my project, and it works just fine, but in this specific situation it's not. I think it worth to mention, that I also tried to render the template without using Sandbox. I tried to render it directly from Twig Engine like this
return $this->templating->render($layoutName, $data);
//$this->templating is injected in the constructor via services.yml like this
//arguments:
// - "#templating"
The result - not properly translated text. When I dumped "locale" - I got one specific language, but the text was translated in another. But at least using this method - I didn't get any errors.. Can anybody clarify this for me? Because I really don't understand how the Intl/i18n extension works and why it doesn't want to work in Sandbox or not in Sanbox?
P.S. My guess, why it doesn't work directly from Twig Engine - probably I should inject not like "#templating" or it injects just right, but the Intl or I18n extensions are not enabled? How to enable. And I have no clues why it doesn't work with Sandbox
I have inherited an application that seems to have existed before the form builder existed. The developer kind of rolled his own with twig macros. Now I want to add a file upload to some existing forms, but I get what seems to be a well known error:
There was 1 error:
myBundle\Tests\Controller\snip\DefaultControllerUnitTest::testCanStoreDocumentAtS3
Exception: Serialization of
'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
C:\apath\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DataCollector\DataCollector.php:27
C:\apath\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Profiler\Profiler.php:218
The solutions is to unmap the file field:
$builder->add('pic','file');
to this :
$builder->add('pic','file', array('mapped'=>false));
But in this case a builder is not used. Instead it looks like this:
{# file(name, value) #}
{% macro file(name, value) %}
<input type="file" name="{{ name }}" id="{{ name }}" value="{{ value }}" />
{% endmacro %}
Is there anything I can add to this macro, or do in the controller action to keep the Profiler from serializing this?
The answer was in part pilot error, but there is enough going on here that I hope I can help others avoid the same pitfall.
This error was triggered by a unit test where I was simulating a file upload along with other parameters. Initially the code that caused this error was:
$photo = new UploadedFile(
__DIR__ . '/TestReportMethod.xlsx',
'TestReportMethod.xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
123
);
....
$form['reportFile'] = $photo;
//submit
This caused the error in my original post.
After trying a few things I moved to this form instead.
$crawler = $this->client->request('POST',
$url,
array('id' => 328),
array('agencyInfoId' => 328),
array('reportFile' => $photo)
);
Which causes an InvalidArgumentException I think a little better example in the cookbook could have prevented. The correct form needs to be an array of parameter values, followed by an array of files:
$crawler = $this->client->request('POST',
$url,
array('id' => 328,
'agencyInfoId' => 328),
array('reportFile' => $photo)
);
I hope this helps somebody else!
I use Symfony2.3. I have a form like this :
<form action="{{ path("member_update") }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form.pic) }}
...
{{ form_widget(form._token) }}
</form>
and i want to upload user pictures in a directory.Then i use this in controller :
$member , $form , $dm is defined...
if ($form->isValid()) {
// Handle profile picture upload process
$uploadDir=dirname($this->container->getParameter('kernel.root_dir')) . '/web/bundles/mybundle/myfiles';
$form['pic']->getData()->move($uploadDir,$member->getId());
// End of upload
$dm->persist($member);
$dm->flush();
return $this->redirect($this->generateUrl("member_profile"));
}
It must work,but i see this error:
Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
1. in pathToMyProject...\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DataCollector\DataCollector.php line 27
2. at serialize(.....
What's the problem??!
The problem solved! I change this line in MemberType :
$builder->add('pic','file');
to this :
$builder->add('pic','file', array('mapped'=>false));
My mistake was that i must explain the "pic" field is not mapped to the Entity(or Document in Mongo,as my project). Else Symfony kernel try to put the value of "pic" in a field of Entity. And i have not any field that hold a file! I upload the picture in a directory and store only path to the picture within the entity. When i changed this,the problem solved easily! :-)
So keep in mind to explain all things clearly to Symfony!
I would like to know if there is a method to pass some variables (text from textarea) from Twig to Symfony2 controller via form.
<form action="{{ path('admin_core_save') }}" method="post">
<div id="edit-template">
{% if template.getData() is defined %}
<textarea id="template">{{ template.getData() }}</textarea>
{% endif %}
</div>
<input type="submit" value="Save" />
</form>
When I press save button it is going to saveAction() method
public function saveAction(Request $request)
{
var_dump($request);
return new Response('abc');
}
but response does not contain any textarea text. Is there a way to get this there?
I know I can build form inside the controller and send it to Twig, but I would like to know if this way is possible.
you can access POST values through request object like:
$this->get('request')->request->get('name');
I'm sure you have to learn a bit about Symfony2 Form Component. You will find that symfony already has built-in system for rendering forms handling user data posted through them.
Answering your question. There is a Request object that provides you full access to all request data, including POST variables.
To access POST values use Request::get() method:
$request->get('variable_name');
To pass any data to the twig template, use TwigEngine::renderResponse
$this->container->get('templating')->renderResponse('AcmeDemoBundle:Demo:template.twig,html',
array(
'someVar' => 'some value'
)
);
This var will be avaliable in your template as:
{{ someVar }}
How can I detect in which bundle am I?
for exemple, when I'm in web.com/participants/list, I want to read "participants".
In order to get the bundle name in the controller:
// Display "AcmeHelloBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}
In order to get the controller name in the controller:
// Display "Default"
echo $this->getRequest()->attributes->get('_template')->get('controller');
And inside a Twig template:
{{ app.request.get('_template').get('controller') }}
In order to get the action name in the controller:
// Displays "index"
echo $this->getRequest()->attributes->get('_template')->get('name');
And inside a Twig template:
{{ app.request.get('_template').get('name') }}
AFAIK it's not yet possible (at least in a easy way). You should use reflection. I wrote a quick and dirty service to do get bundle name ang guess entity/repository/form names based on my conventions. Can be buggy, take a look at: http://pastebin.com/BzeXAduH
It works only when you pass a class that inherits from Controller (Symfony2). Usage:
entity_management_guesser:
class: Acme\HelloBundle\Service\EntityManagementGuesser
In your controller:
$guesser = $this->get('entity_management_guesser')->inizialize($this);
$bundleName = $guesser->getBundleName(); // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle
Another possibility would be using kernel to get all bundles: Get a bundle name from an entity
Well you can get the controller of the current route by,
$request->attributes->get('_controller');
You can parse the bundle name from it.
You can get the bundle name in the controller simply like that:
// Display "SybioCoreBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}