Welcome Guest, Not a member yet? Register   Sign In
ckeditor & form validation
#1

[eluser]incog03[/eluser]
Hi guys,

So what I'm doing is having users add their comment with ckeditor in a form, with a bunch of other formy options. Pretty straight forward.

Once the data is validated it will be passed and stored on a database, which also has no issues.

My issue is concerned with validation failure, for example if someone leaves part of the form empty when it's required and submits, everything still works as it should, almost!

When I try to recover what was in the ckeditor form (when the page reloads) it also plants the raw hmtl in the form as well, which really sucks. And I simply can't have code on the form, users will get confused, even frightened!

Controller sample
Code:
$this->form_validation->set_rules('MainContent', 'Content', 'trim|required');
    
$this->load->library('ckeditor', base_url() . 'ckeditor/');
$this->ckeditor->basePath = base_url(). 'ckeditor/';
$this->ckeditor->ToolbarSet = 'Standard';

View sample
Code:
echo form_label('Website: ', 'website');
            echo form_input('website', set_value('website', 'http://')); ?&gt; <br /><br />
            
            <div>
                &lt;?php $this->ckeditor->editor('MainContent', set_value('MainContent', ''));?&gt;
            </div>
            &lt;?php
            echo validation_errors('<p class="error">');
            echo form_fieldset_close();    
            echo form_submit('submit', 'Preview Post');
            echo form_close();

So the issue is with
Code:
set_value('MainContent', ''));
Are there any alternative solutions to my problem? I've spent about 5 hours searching and my brain hurts =/

Your help is very much appreciated.
#2

[eluser]fesweb[/eluser]
Disclaimer: I've never used the ckeditor helper/library, so I can't help with that aspect of things.

I use ckeditor for jquery, so you treat the textarea as a textarea and apply the CKEditor via jQuery.
Code:
[removed]
$(document).ready(function(){
    $('textarea.editor').ckeditor();
});
[removed]
&lt;textarea name="my_text" class="editor"&gt;&lt;?php echo set_value('my_text', ''); ?&gt;&lt;/textarea&gt;
#3

[eluser]incog03[/eluser]
From the source html generated it appears that it is already creating a textarea and planting the ckeditor inside, but when validation occurs and the page refreshes,
Code:
set_value('MainContent', '');
still adds the raw html, so instead of planting "this is a test" on the form, it will place "
Code:
<p>this is a test</p>
" instead.

I would have thought ckeditor would have an option to deal with html being added into the form by a user (or method), perhaps there's another way to validate just the ckeditor? Or an alternative to the set_value() method...

I don't know I'm really starting to confuse myself.

Code:
&lt;textarea name="MainContent" rows="8" cols="60"&gt;&lt;/textarea>
<s+c+r+i+p+t type="text/javascript">//&lt;![CDATA[
window.CKEDITOR_BASEPATH='http://localhost/myproject/ckeditor/';
//]]></s+c+r+i+p+t>
[removed]</s+c+r+i+p+t>
[removed]//&lt;![CDATA[
CKEDITOR.replace('MainContent');
//]]></s+c+r+i+p+t>
#4

[eluser]pickupman[/eluser]
The editor returns the form encoded using [url="http://us2.php.net/manual/en/function.htmlentities.php"]htmlentities()[/url] for security. It's a layer of protection for information going in and out of the DB, can be used for an attack. You just have to reverse it back when displaying back on the page using [url="http://us2.php.net/manual/en/function.html-entity-decode.php"]html_entity_decode()[/url].
#5

[eluser]incog03[/eluser]
would you mind providing me with an example - I tried a couple things like
Code:
&lt;?php $this->ckeditor->editor('MainContent', set_value(html_entity_decode('MainContent'), ''));?&gt;

It doesn't work though... Thanks
#6

[eluser]incog03[/eluser]
So i figured out (probably a really bad way!) how to do it and it works fantastically.

So the user enters information into the form, submits, validation fails because of some of the other text boxes or whatever.

All I needed to do was strip the tags each time validation failed, add the existing data back into the textarea and ckeditor magically restyles it how it was, don't ask me how.

Controller.php
Code:
$data['test'] = $this->input->post('MainContent');
    strip_tags($data['test']);

view.php
Code:
&lt;?php $this->ckeditor->editor('MainContent', $test );?&gt;

And the best part is, when validation is successful, the final version of the textarea data is stored nicely in the database with all tags still attached, ready to be called Big Grin Magic!


Thanks a lot, I appreciate your help guys/girls
#7

[eluser]incog03[/eluser]
-
#8

[eluser]Unknown[/eluser]
Hi,
I had the exact sam problem today and just managed to solve it. You were really close. Instead of decoding the string you send into set_value...
Code:
&lt;?php $this->ckeditor->editor('MainContent', set_value(html_entity_decode('MainContent'), ''));?&gt;

you should decode the result:
Code:
&lt;?php $this->ckeditor->editor('MainContent', html_entity_decode(set_value('MainContent', '')));?&gt;
#9

[eluser]dUspan[/eluser]
hi

where can i find and download the ckeditor for codeigniter?

thanks.




Theme © iAndrew 2016 - Forum software by © MyBB