Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions

custom form


Reply

8 Posts   930 Views

Avatar
Downing Media

25 August 2010 at 9:26am Community Member, 5 Posts

Can somebody show me a complete example of a custom form and template? Does not have to be very complex. I had it working and messed something up. Trying to get a site finished and I am low on sleep

Avatar
Willr

25 August 2010 at 11:43am Forum Moderator, 5511 Posts

Welcome to the forums! Perhaps look at the example on http://doc.silverstripe.org/form#using_a_custom_template

Avatar
Downing Media

25 August 2010 at 11:52am (Last edited: 25 August 2010 11:53am), Community Member, 5 Posts

Thanks for the welcome and the quick reply.

I used that the the link you provided the first time I had the form working. I deleted the wrong file and now I get get it setup again.

Here is what I think I need to do but it is not working. Create a php file in the mysite directory for the MyForm class and then create the template in the Include directory. I will also need to create a formholder.php so I can create the page in the admin area.

I am missing something but can't figure out what.

Avatar
Willr

25 August 2010 at 12:41pm Forum Moderator, 5511 Posts

Here is what I think I need to do but it is not working. Create a php file in the mysite directory for the MyForm class and then create the template in the Include directory. I will also need to create a formholder.php so I can create the page in the admin area.

I think the template needs to be in templates/ not templates/includes/ since its not an 'include' per se. After following the steps on the wiki page do you get an error message or is it just a blank page?

Avatar
Downing Media

25 August 2010 at 1:21pm Community Member, 5 Posts

I was getting a blank page. Since the last post I added <% include MyForm %> and now I get the labels from the MyForm.ss but not the textboxes. I am not sure if adding this was correct but I am trying everything.

Here is what I have

/mysite/code/quoteform.php

<?php

class MyForm extends Form {

function __construct($controller, $name) {
$fields = new FieldSet(
new TextField('FirstName', 'First name'),
new EmailField('Email', 'Email address')
);

$actions = new FieldSet(
new FormAction('submit', 'Submit')
);

parent::__construct($controller, $name, $fields, $actions);
}

function forTemplate() {
return $this->renderWith(array(
$this->class,
'Form'
));
}

function submit($data, $form) {
// do stuff here
}

}

/mysite/code/quotepage.php

<?php
class QuotePage extends Page {

static $db = array(
);

static $has_many = array(

);
}

class QuotePage_Controller extends Page_Controller {

}

/themes/templates/mytemplate/quotepage.ss

//code above is left out to condense//
            
         $Content

<% include MyForm %>

//code below is left out to condense//

/themes/templates/mytemplate/Includes/MyForm.ss

<form $FormAttributes>
<% if Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>

<fieldset>
<div id="FirstName" class="field text">
<label class="left" for="$FormName_FirstName">First name</label>
$dataFieldByName(FirstName)
</div>

<div id="Email" class="field email">
<label class="left" for="$FormName_Email">Email</label>
$dataFieldByName(Email)
</div>

$dataFieldByName(SecurityID)
</fieldset>

<% if Actions %>
<div class="Actions">
<% control Actions %>$Field<% end_control %>
</div>
<% end_if %>
</form>

Avatar
Willr

25 August 2010 at 1:27pm Forum Moderator, 5511 Posts

You haven't told the controller about the form. You must create an instance of your MyForm class to pass it to the template.

class QuotePage_Controller extends Page_Controller {

function MyForm() {
return new MyForm($this, 'MyForm');
}

Then in the template use $MyForm.

Avatar
Downing Media

25 August 2010 at 1:36pm Community Member, 5 Posts

Worked great.

Thanks for the fast response.

Avatar
dayer

1 September 2010 at 1:40pm Community Member, 11 Posts

Hi,

for blackcandy theme, for example, I must to change in MyForm.ss:

<div id="FirstName" class="field text">
<label class="left" for="{$FormName}_FirstName">First name</label>
</div>


for:

<div id="FirstName" class="field text">
<label class="left" for="{$FormName}_FirstName">First name</label>
<div class="middleColumn">$dataFieldByName(FirstName)</div>
</div>

Thanks.