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
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Welcome to the forums! Perhaps look at the example on http://doc.silverstripe.org/form#using_a_custom_template
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.
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?
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>
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.
Worked great.
Thanks for the fast response.
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.