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.

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.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

form template


Go to End


5 Posts   3790 Views

Avatar
leafchild

Community Member, 41 Posts

9 March 2011 at 11:31am

Edited: 09/03/2011 11:33am

Someone help! I have a problem creating form template.

I checked:
http://doc.silverstripe.org/sapphire/en/topics/forms
http://silverstripe.org/form-questions/show/8333

but I still confused all process. Here are my code:
input code is not showing, not sure where I am missing,,,,

thanks

Contact.php

<?php
class Contact extends Page { ...}

class Contact _Controller extends Page_Controller {
	static $allowed_actions = array(
		'ContactForm'
	);
	
	function ContactForm(){
	$fields = new FieldSet(
			new TextField('Name', 'name: *', '', '20'),	
			new EmailField('Email', 'email: *', '', '50')
		);
		$actions = new FieldSet(new FormAction('sendContactForm', 'Submit'));
		$validatior = new RequiredFields('Name’, 'Email');

		return new Form($this, 'ContactForm', $fields, $actions, $validatior);	
	}

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

	function SendContactForm($data, $form){ … }
}
?>

MyForm.php

<?php
class MyForm extends Form{
	function __construct($controller, $name){
		$fields = new FieldSet(
			new TextField('Name', 'name: *', 'first', '20'),	
			new EmailField('Email', 'email: *', '', '50')
		);
		$actions = new FieldSet(new FormAction('sendContactForm', 'Submit'));
		$validatior = new RequiredFields('FirstName', 'LastName', 'Email', 'Details');			
		parent::__construct($controller, $name, $fields, $actions, $validatior);
	}
	
	function forTemplate(){
		return $this->renderWith( array(
			$this->class,
			'Form'
		));	
	}
	
	function sendContactForm($data, $form){ ...	}
}	
?>

Contact.ss

…
<% include MyForm %>
...

MyForm.ss

<form $FormAttributes id="contactForm">
            <table class="contactTable" cellpadding="0" cellspacing="0">
             <tr><th> <table class="formLft" cellpadding="0" cellspacing="0">
             <tr><th><label>name <span>*</span></label></th>
 <td>
  <div class="inputField fltLft" style="margin-right:3px;">
  <div class="inputField2">
  <div class="inputField3">
                          $dataFieldByName(Name)         
 </div></div></div>
  <div class="clr"></div>
   </td> </tr>          
  <tr><th><label for="$FormName_Email">email <span>*</span></label></th>
   <td><div class="inputField"><div class="inputField2"><div class="inputField3">
                       $dataFieldByName(Email)
  </div></div></div></td>
  </tr></table>
  </form>

Avatar
Willr

Forum Moderator, 5523 Posts

9 March 2011 at 9:15pm

Have you done a ?flush=all to include the new template? Also note your custom template should be in the 'Includes' directory of your theme.

Avatar
leafchild

Community Member, 41 Posts

10 March 2011 at 6:46am

Hi Millr, thank you for the replay.

I just did ?flush=all and also my custom template is already in 'Includes' directory
but i still seen same issue.

input tag is not display at MyForm.ss so must be something to do either Contact.php or MyForm.php, or both.

Avatar
leafchild

Community Member, 41 Posts

24 March 2011 at 2:15pm


Please someone HELP!! I am still struggling same issue.

I found this page (http://pastie.org/522439) and copy and past most of the code to see if it would work.
Unfortunately the issue is still same. "<input type ....>" is not display(text box is not displayed)
What am I missing?

Contact.php

class Contact_Controller extends Page_Controller {
  function MyFirstForm(){
    return new MyForm($this, 'MyFirstForm');
  }
} 

class MyFormSubmission extends DataObject {
  static $db = array(
    'FirstName' => 'Varchar(255)',
    'Email' => 'Text'
  );  
}

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><br>
         $dataFieldByName(FirstName)
      </div>

      <div id="Email" class="field email">
         <label class="left" for="{$FormName}_Email">Email :</label><br>
         $dataFieldByName(Email)
      </div>

      $dataFieldByName(SecurityID)
   </fieldset>

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

MyForm.php

<?php
//MyForm Sub Class, file name: MyForm.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')
      );
     $validator = new RequiredFields('FirstName', 'Email');


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

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

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

}

?>

Avatar
Raver0124

Community Member, 8 Posts

16 October 2013 at 1:33pm

I know its over 2 years since the first post but I had the same issue.
It seems if I do this, it fixed it self

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

to

    public function forTemplate() {
        return $this->renderWith(array( $this->class));
    }