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 Rendering


Reply


5 Posts   7305 Views

Avatar
8mike8

Community Member, 2 Posts

9 June 2009 at 2:28am

Hi!

What i like to know is: How is it possible to create custom forms?
I like to display a custom form, but it seems that the form attributes wouldn't be parsed...

So, here is my setup:
1. I have my custom Page (KMForm_Page) / PageController (KMFormPage_Controller)
The Controller looks like this:
class KMFormPage_Controller extends Page_Controller
{
function KMForm()
{
return new KMForm($this,'KMForm');

}

}

2. I do of course have a custom Form (KMForm) as used above:
class KMForm extends Form
{
function __construct($controller, $name)
{
$fields = new FieldSet(
new EmailField('email', 'E-Mail-Adresse'),
new TextField('telefon', 'Telefonnummer')
);

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

$requiredFields = new RequiredFields('email');

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

function forTemplate()
{
// Erwartet Template mit Namen 'KMForm'
return $this->renderWith(array(
$this->class,
'Form'
));
}

function submit($data, $form)
{
print_r($data);
}
}

3. I have a appropriate Template KMForm.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="email" class="field email">
<label for="$Form_email">E-Mail-Adresse</label>
$dataFieldByName(email)
</div>
<div id="telefonnummer" class="field telefon">
<label for="$FormName_telefon">Telefonnummer</label>
$dataFieldByName(telefon)
</div>
$dataFieldByName(SecurityID)
</fieldset>

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

</form>

Site displays, but:
- The Rendering of the Variables $FormAttributes, $FormName, $Form_email etc. doesn't happen at all
- site displays only the non-dynamic items of KMForm.ss

So, as usual the failure sits in front of the PC...
Any hints, what i'm doing wrong?

Thanks for your time!

Avatar
jahbini

Community Member, 13 Posts

19 June 2009 at 8:48am

I've gotten past the point you have described in this posting (but am stumped by not activating the submit routine, got any hints on that?).

Question: Do you have a $KMForm in the page layout template for KMFormPage_Controller?

I would also advise putting a <% debug %> in both 'ss' files to see what methods are available. Most likely the 'ss' file is being activated on the wrong class object.

Avatar
Ben_W

Community Member, 80 Posts

24 June 2009 at 12:32pm

Edited: 24/06/2009 12:34pm

Not 100% sure, however you may try following two points:
1. make sure KMForm.ss is in 'templates/Includes' directory.
2. it seems that you form function and form class has the same name, what I did recently is given the form subclass a different name. ie MyForm extends Form, then in your page controller, do

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

In your case, maybe it is better to change sub class name, because you need KMForm.ss match the function name.

Hope it will help

Avatar
leafchild

Community Member, 41 Posts

25 March 2011 at 9:24am

I have similar issue, and I need help!!!

My code is pretty similar to what she posted
(http://silverstripe.org/form-questions/show/16121#post301015)
Also I smpled this page: http://pastie.org/522439

#1. MyForm.ss is in templates/Includes directory
#2. In my page controller, I just put this

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

MyForm.ss is display fine but "$dataFieldByName()" is NOT working.

I have this in my MyForm.php

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


so I have

$dataFieldByName(FirstName)
$dataFieldByName(Email)


in my MyForm.ss but they are not displaying anything.
It supposed to show

<input type='text' ..... >

right?

What is wrong?

Avatar
TDNP

Community Member, 19 Posts

12 January 2012 at 1:27pm

I was having similar issues. Check this forum post. It was my answer:
http://doc.silverstripe.org/sapphire/en/topics/forms#using_a_custom_template