14 July, 2010

SilverStripe HTMLText Ripping off HTML Tags

I dont know whether this post will be that interesting. Yet this is something which I was trying for sometime with SilverStripe.

Let me explain my problem. I think everyone knows MailChimp. I got a HTML form from mailchimp and I want to provide the client with a TextField to edit the form when ever he needs. Now as you all might guess I did a code as such

class MailchimpPage extends Page{

static $db = array(
"MailChimp" => "HTMLText"
);

function getCMSFields(){
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.MailChimp", new TextareaField("MailChimp"));
..........
return $fields;
}
.....


All looks fine. But what I enter there on the field had some empty DIV tags.
which looks like ( <div></div> ).


When I save the page with a save and publish what happened is that SilverStripe tried to make these tags self-closing. eg ( <div /> ) This is not valid at all. There is no such tag.

Now I was scratching my head for sometime and got an idea. What I did was made the HTMLText type to Text. Okay one battle is over, the HTML snippet get saved without anything being ripped off. Now the second. This appeared on the site's front as HTML, not processed. This might be because of the Convert class, anyway I dont want a piece of HTML to appear on the web page, the client will rip my head off instead :D.

Alright, I did something pretty nice there, Now though the Database field is not a HTMLText I wanted the output to be HTMLText.

I added a function similar to the following to the Controller of my page.

function HTMLForm(){
$field = new HTMLText();
$field->setValue($this->MailChimp);
return $field;
}


Now you know whats the trick there. I am wrapping the Text field with a HTMLText field, and then it worked. The Viewer doesn't know what I am doing.

Thought this might be interesting as no one on the IRC could help me out, as well as didn't find any source for this.