Symfony2 IvoryCKEditorBundle - CK Editor not showing - javascript

I am trying to display the CK Editor within my textarea form without success :(. I've already searched in github, symfony doc, IvoryCKEditorBundle etc. and still cannot figure out why it doesn't show for me.
I've installed:
Ivory\CKEditorBundle\IvoryCKEditorBundle(),
Ivory\FormExtraBundle\IvoryFormExtraBundle()
assets
Here is my config:
ivory_ck_editor:
enable: true
autoload: false
async: true
base_path: bundles/ivoryckeditor/
js_path: bundles/ivoryckeditor/ckeditor.js
default_config: ~
configs:
my_config:
toolbar: full
uiColor: "#000000"
extraPlugins: "wordcount"
name: []
plugins:
name:
path: ~
filename: ~
styles:
name: []
Here is a class with the form:
class MatchScheduleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('content', CKEditorType::class,
array('label' => 'content',
'required' => false,
'config_name' => 'my_config',
'trim' => true
)
)
->add('save', SubmitType::class, array('label' => 'saveButtonValue'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'fcHlinskoBundle\Entity\MatchSchedule',
I have a simple entity MatchSchedule..
Then i try to pass the form from controller:
class DefaultController extends Controller
{
public function indexAction()
{
$post = new MatchSchedule();
$form = $this->createForm(MatchScheduleType::class, $post, array(
'action' => $this->generateUrl('homepage'),
'method' => 'POST',
));
return $this->render('fcHlinskoBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
// ...
And display it inside a twig:
{{ form_start(form) }}
{{ form_javascript(form) }}
{{ form_end(form) }}
And i got this result in html code:
<form name="match_schedule" method="post" action="/app_dev.php/">
<script type="text/javascript">
if (CKEDITOR.instances["match_schedule_content"]) { delete CKEDITOR.instances["match_schedule_content"]; }
CKEDITOR.plugins.addExternal("name", "/", "");
if (CKEDITOR.stylesSet.get("name") === null) { CKEDITOR.stylesSet.add("name", []); }
CKEDITOR.replace("match_schedule_content", {"toolbar":[["Source","-","NewPage","Preview","Print","-","Templates"],["Cut","Copy","Paste","PasteText","PasteFromWord","-","Undo","Redo"],["Find","Replace","-","SelectAll","-","Scayt"],...... etc.
</script>
<div><label for="match_schedule_content">obsah</label> <textarea id="match_schedule_content" name="match_schedule[content]"></textarea>
</div><div><button type="submit" id="match_schedule_save" name="match_schedule[save]">odeslat</button></div><input type="hidden" id="match_schedule__token" name="match_schedule[_token]" value="E-zJ2sUyfDIxGP3gljVQGpj8FWId3a8_FIJw3rLkOYU" /></form>
All of this results just in simple default textarea with label a submit button.
No CK Editor. Can anyone please tell what I am doing wrong? I am really out of ideas, thx for every advice :)

Related

CHALLENGE: Load Gravity Forms via AJAX - Change working PHP solution to Gutenberg code

I had the need to load a Gravity Form through Ajax on a push of a button (in PHP/Wordpress), and thanks to Steven Henty, I've found his solution to fix my issue. Modified it at little to open my form in a modal (lity modal), and it works!
However.... Now I started to migrate my site to the new Gutenberg editor in Wordpress. So I need a system that can do the same from a Gutenberg block (javascript) code.
I can code some, but I am not hardcore, so would love if anyone could tell me how to implement this former (PHP based) 'system', for instance to a (javascript based) Gutenberg Button-block, that implements this code. Here is the code for the current (working) button:
button.php
// Hook up the AJAX ajctions
add_action('wp_ajax_nopriv_gf_button_get_form', 'gf_button_ajax_get_form');
add_action('wp_ajax_gf_button_get_form', 'gf_button_ajax_get_form');
// Add the "button" action to the gravityforms shortcode
// e.g. [gravityforms action="button" id=1 text="button text"]
add_filter('gform_shortcode_button', 'gf_button_shortcode', 10, 3);
function gf_button_shortcode($shortcode_string, $attributes, $content)
{
$a = shortcode_atts(array(
'id' => 0,
'text' => 'Open',
'button_class' => '',
'button_style' => ''
), $attributes);
$form_id = absint($a['id']);
$curr_lang = ICL_LANGUAGE_CODE;
if ($form_id < 1) {
return '<div>Missing the ID attribute.</div>';
}
gravity_form_enqueue_scripts($form_id, true);
$ajax_url = admin_url('admin-ajax.php');
$html = sprintf('<button id="gf_button_get_form_%d" class="gf_button_get_form %s" style="%s"><div class="gf_button_get_form-label">%s</div></button>', $form_id, $a['button_class'], $a['button_style'], $form_id, $a['text']);
$html .= "<script>
(function (SHFormLoader, $) {
$('#gf_button_get_form_{$form_id}').click(function(){
$.ajaxSetup({
beforeSend: function() {
$('.spinner_{$form_id}').addClass('active');
},
complete: function() {
$('.spinner_{$form_id}').removeClass('active');
var fieldsWithHiddenLabels = $('.gfield.hidden-label');
if (fieldsWithHiddenLabels.length) {
fieldsWithHiddenLabels.each(function(){
if($(this).hasClass('gfield_contains_required')){
$(this).find('.ginput_container label').prepend('<span class=\"gfield_required\">*</span>');
}
});
}
}
});
$.get('{$ajax_url}?lang={$curr_lang}&action=gf_button_get_form&form_id={$form_id}',function(response){
lity(response);
if(window['gformInitDatepicker']) {gformInitDatepicker();}
});
});
}(window.SHFormLoader = window.SHFormLoader || {}, jQuery));
</script>";
return $html;
}
function gf_button_ajax_get_form() {
$form_id = isset($_GET['form_id']) ? absint($_GET['form_id']) : 0;
gravity_form($form_id, true, false, false, false, true);
die();
}
I am using the excellent create-guten-blocks as boilerplate, and my template block file looks like this:
form-button.js
import './style.scss';
import './editor.scss';
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
registerBlockType( 'my-blocks/form-button', {
title: __( 'Form Button' ),
icon: heart,
category: 'common',
keywords: [
__( 'my-blocks — Form Button' )
],
edit: function( props ) {
return (
<div className={ props.className }>
<p>— Hello from the backend.</p>
<p>
CGB BLOCK: <code>configit-blocks</code> is a new Gutenberg block
</p>
<p>
It was created via{ ' ' }
<code>
<a href="https://github.com/ahmadawais/create-guten-block">
create-guten-block
</a>
</code>.
</p>
</div>
);
},
save: function( props ) {
return (
<div>
<p>— Hello from the frontend.</p>
<p>
CGB BLOCK: <code>configit-blocks</code> is a new Gutenberg block.
</p>
<p>
It was created via{ ' ' }
<code>
<a href="https://github.com/ahmadawais/create-guten-block">
create-guten-block
</a>
</code>.
</p>
</div>
);
},
} );
Hope this makes sense. This is my second post on Stackoverflow, so please let me know if you need more details ... Really hope one of you skilled people can handle this. Thanks!

Using ajax to submit multiples form(s) in Symfony 3?

I'm trying to create a dynamic 2-step form using Jquery where in "step 1", I want to submit the form data without refreshing my page so that I can hide my html division containing my form and show the other representing my step 2 using Jquery.
The problem is that I'm using a collection of forms in my controller action like this:
public function indexAction(Request $request)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('ATPlatformBundle:NoteDeFrais');
$form = $this->get('form.factory')->createBuilder(FormType::class)
->add('ndf', CollectionType::class,array(
'entry_type' => NoteDeFraisType::class,
'label' => false,
'allow_add' => true,
'allow_delete' => true,
))
->getForm();
And I'm getting the forms data submitted from like this:
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()
&& isset($_POST['next_button'])) {
$notesDeFrais = $form['ndf']->getData();
foreach ($notesDeFrais as $ndf) {
$ndf->setUser($user);
$em->persist($ndf);
}
$em->flush();
}
elseif (isset($_POST['validate_button'])) {
foreach ($listNdf as $ndf) {
$ndf->setSubmitted(true);
}
$em->flush();
}
So what I wanted to know is how to send my data via an ajax request and how to get them from my action. So far I tried to proceed like this but it (logically) doesn't work.
$("div#bloc_validation").css("display", "none");
$("#next_button").click(function(){
$(".form_ndf").each(function(){
$.post("{{ path('platform_homepage') }}",
{ndf: $(this).serialize()}, //My issue is here
function(){
alert('SUCCESS!');
}
);
});
$("div#form_bloc ").css("display", "none");
$("div#bloc_validation").css("display", "block");
});
Do you have any ideas ? Thanks in advance
The most basic approach is this:
add a javascripts block in your twig file with the content as below.
Change appbundle_blog in the first line inside the .ready() function in the name of your form. (Inspect your html to find it).
{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
<input type="submit" value="Save Changes" />
{{ form_end(edit_form) }}
{% endblock %}
{% block javascripts %}
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
$(document).ready( function() {
var form = $('form[name=appbundle_blog]');
form.submit( function(e) {
e.preventDefault();
$.ajax( {
type: "POST",
url: form.attr( 'action' ),
data: form.serialize(),
success: function( response ) {
console.log( response );
}
});
});
});
</script>
{% endblock %}
If the form has been submitted you have to answer to an AJAX request. Therefore you could render another template..
/**
* Displays a form to edit an existing blog entity.
*
* #Route("/{id}/edit", name="blog_edit")
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, Blog $blog)
{
$editForm = $this->createForm('AppBundle\Form\BlogType', $blog);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
/* render some new content */
return $this->render('blog/ajax.html.twig', array(
'blog' => $blog,
));
}
return $this->render('blog/edit.html.twig', array(
'blog' => $blog,
'edit_form' => $editForm->createView(),
));
Or answer in JSON:
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Displays a form to edit an existing blog entity.
*
* #Route("/{id}/edit", name="blog_edit")
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, Blog $blog)
{
$editForm = $this->createForm('AppBundle\Form\BlogType', $blog);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return new JsonResponse(array(
'status' => 'success',
// ...
));
}
return $this->render('blog/edit.html.twig', array(
'blog' => $blog,
'edit_form' => $editForm->createView(),
));
}
If you want you can even test if the request is an AJAX request or not:
if($request->isXmlHttpRequest()) {
// yes it is AJAX
}

Symfony check box with action

I using symfony and have template with array with some entities array, and need create in for in check box for all entity and when checked some entities and click ready go to action with ids(from all check box) - example - taskExecution.id
I don't used symfony form with type entity because taskExecutions complicated DTO, from this DTO i need only id for to send on another action
$taskExecutions = $this->getTaskExecution()
->getTaskExecutionByFilter($form->getData());
return [
'form' => $form->createView(),
'taskExecutions' => $taskExecutions
];
{% for taskExecution in taskExecutions %}
<input class="searchType" type="checkbox" name="SharingNotification" id={{ taskExecution.id }}>
<label class="searchtype2label">{{ taskExecution.id }}</label>
</input>
{% endfor %}
{% javascripts
'#EconomyBundle/Resources/public/js/check-task-executions.js'
filter='?yui_js' combine=true %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
add js
$('.searchType').click(function() {
alert($(this).attr('id'));
if(this.checked){
$.ajax({
type: "POST",
url: '/manage/outbound_invoices/task_executions/ids',
data: $(this).attr('id'), //--> send id of checked checkbox on other page
success: function(data) {
alert('it worked');
alert(data);
$('#container').html(data);
},
error: function() {
alert('it broke');
},
complete: function() {
alert('it completed');
}
});
}
});
this my action
/**
* give task executions ids for created row.
*
* #Route("/manage/outbound_invoices/task_executions/ids", name="ids_task_executions_")
* #Method({"POST", "GET"})
*/
public function getIdsTaskExecutionsAction(Request $request)
{
$ids = $request->get('ids');
}
I don't know js, help please for understand how get check box value (1 or 0) and entity id parameter and send to another action
I don't think you need javascript for that. Instead you should have a look to the Symfony doc on how to use a form without data_class
your form will looks like :
<?php
class TaskExecutionType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('taskExecution', EntityType::class, array(
'class' => 'AppBundle/TaskExecution',
'expanded' => true,
'multiple' => true
))
->add('submit', SubmitType::class)
;
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false
));
}
/**
* #return string
*/
public function getName()
{
return 'execution_task_type';
}
}
And in your controller:
<?php
/**
* give task executions ids for created row.
*
* #Route("/manage/outbound_invoices/task_executions/ids", name="ids_task_executions_")
* #Method({"POST", "GET"})
*/
public function getIdsTaskExecutionsAction(Request $request)
{
$form = $this->createForm(TaskExecutionType::class, null, array(
'method' => 'POST',
'action' => 'ids_task_executions'
));
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData(); //this will be an array of all the TaskExecution entities you selected
//your own logic
}
return $this->render('template.html.twig', array(
'form' => $form->createView()
));
}

symfony send form request without submit

I want to send request using just choice type, and without submit button, i tried with javascript but no result
My form code:
$form = $this->createFormBuilder()
->add('language', 'choice', array(
'choices' => array(
'ar' => 'Arabic',
'fr' => 'French',
'es' => 'Espagnol',
'nl' => 'Dutch',
'ja' => 'Japanese',
'en' => 'English',
'ko' => 'Korean',
'it' => 'Italian',
'ru' => 'Russian',
),
'required' => false,
'placeholder' => 'Click to Choose Language..',
'attr' => array(
'id' => 'field',
'class' => 'controls',
'data-rel' => 'chosen'
)
))
->getForm();
My twig is as follows:
<form action="{{ path('test_test') }}" method="POST" {{ form_enctype(form) }}>
{{ form_widget(form.language, { 'attr': {'class': 'select2'} }) }}
</form>
The javascript code that i used is :
<script type="text/javascript">
$('#field').change(function() {
$(this).closest('form').trigger('submit');
});
</script>
You can use Jquery submit function like this:
jQuery(function($) {
$(".select2").change(function() { //put class or id of your select input
$('#id_of_your_form').submit();
});
});
EDIT:
If you are using select2 plugin (you have examples here) :
jQuery(function($) {
$('.select2').select2()
.on("change", function(e) {
$('#id_of_your_form').submit();
})
});
And then you can get POST data sending in your Symfony Controller like this :
public function getDataAction(Request $request)
{
dump($request->request->get('name of your select input');exit;
}

Redirecting form fails

I'm having problems with my forms redirection for a specific controller. In my database table it called lists_acciones.
Model :
<?php
App::uses('AppModel', 'Model');
App::uses('CakeSession', 'Model/Datasource');
/**
* ListAccione Model
*
*/
class ListAccione extends AppModel {
public $useTable = 'lists_acciones';
Controller
<?php
App::uses('AppController', 'Controller');
/**
* ListsAcciones Controller
*
* #property Menu $Menu
*/
class ListsAccioneController extends AppController {
I can access any function from my controller and the controller is the url listsAccione
Now he was creating a form, then click the go to the index view but the controller that comes is lists_acciones
echo $this->Form->create('ListsAccione', array(
'action' => 'index',
'class' => 'form_center',
'id'=>'formulario_busqueda',
'inputDefaults' => array(
'label' => false,
'div' => false,
# define error defaults for the form
'error' => array(
'wrap' => 'span',
'class' => 'my-error-class'
)
)
));
From Javascript
var url = "<?php echo $this->Html->url(array(
'controller' => 'listsAccione',
'action' => 'index',
'admin'=>true));?>/page:"+pagina;
$('#formulario_busqueda').attr('action',url); //this works
I found the solution in the documentation CakePHP
'url' => ['controller' => 'listsAccione', 'action' => 'index'],

Categories

Resources