Zend Framework: Accessing Dojo Editor Contents

| 2 Comments | 2 TrackBacks

There have been other articles around about the usage of the Dojo editor element in Zend Framework. The solution by Weier O'Phinney has been to write a view helper and use the editor in this way.

I have also started to use the Dojo components for my application and have been wondering how to access the editor, since I wanted an editor for my article bodies. So I looked a little bit around but found only the solutions which required to write own Javascript-functions and needed additionally hidden textareas to store the content respectively to access the content.

DijitEditor.png


But I was not happy with this solution. First I am not the Javascript-Guru, which means I have not written that much in JS yet. Second I did not want to have a second hidden textarea included in my forms, because there was one, I just needed to access it in the right way.

So this weekend I downloaded the new version of Zend Framework 1.7.2 (in 1.7.1 I have not figured out how it would work, but maybe the same way) and updated my complete application.
Then I had a look at the source of the page where the editor resided. Since O'Phinney writes that Dojo "ends up storing content outside the form". I am not sure how it was before 1.7.2, but now you find in your page source something like this:

<dt><label for="body" class="optional">Editor</label></dt> <dd> <input id="body" name="body" value="" type="hidden"><textarea id="body-Editor" name="body[Editor]" type="text" inheritWidth="1" dojoType="dijit.Editor"></textarea> </dd>

As you can notice, there is a hidden input containing a textarea, that has the specified name of your field (as done in my case with "body") and "-Editor" attached to it. I thought that this content element must be accessible by the Zend Framework and I therefore can store the contents of the field, as any other in a database.

In the next step I had a look what the "normal" editor-field returned, when I used a standard method from ZF to access the values from the form. And it returned array. Which is consistent with was has been reported here.

Somewhere in the array there must the content of the field be! That was the beginning of some experiments. And I ended with the following solution:

// some code left out here

$values = $form->getValues();
		$table = new Articles;
		$data = array(
			'article_title' => $values['title'],
			'article_body' => $values['body']['Editor'],
			'article_lead' => $values['lead'],
		);
		$table->insert($data);

// other code of method left out here

As you can see can you access the contents of the editor, within the returned array. You just need to add the second key for the array.

For the loading there is no problem. You can load the contents directly into the (in this case) "body" field and Dojo will display it correctly within the editor.

What remains to do for me is, that the editor returns the chars directly, so that I have to transform the HTML-Special-Chars before saving into the database.

2 TrackBacks

Inserting images in the Dojo Editor in Zend Framework is not that clear when you start to work with it. I had to look a little bit around to find the solution. Read More

MyNetFaves : Public Faves Tagged Contents from MyNetFaves : Web 2.0 Social Bookmarking on February 22, 2009 3:48 AM

Marked your site as contents at MyNetFaves! Read More

2 Comments

I use Zend Framework 1.7.5 and in this version the content is automatically pulled from the editor to the hidden field with the right name on submit. You don't even notice that there are two fields.

I use Zend Framework 1.7.8 and find that Firefox returns the contents automatically, but IE, Chrome and Safari post the contents in the 'Editor' array. I could not get this to work using form->getValues for some reason I don't understand but was able to get it using getRequest()->getParams(). To work around the FF issue I test whether the content of the 'body' is an array.

Leave a comment