Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » How to include a class field value in from template?

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

Page: 1
Go to End
Author Topic: 564 Views
  • sajok
    Community Member
    82 Posts

    How to include a class field value in from template? Link to this post


    I have a langing page with a form that uses in it's action attribute a form handler url from an external API. The API takes the form fields values and redirects the user on another "Thank you" page on the same site.

    I added a new text field to the LandingPageForm.php class to store the value of the "form handler url". I'm trying to call it from the form template in action attribute with <% control LandingPageForm %>$FormHandlerURL<% end_control %>, but this didn't show any value. This is the code I have:


    class LandingPageForm extends Page
       static $db = array(
          'FormHandlerURL' => 'Text' // the form handler URL

       function getCMSFields()
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Main", new TextField('FormHandlerURL', 'Form handler URL'));
          return $fields;   


    class LandingPageForm_Controller extends Page_Controller
       static $allowed_actions = array(
       //The function which generates our form
       function LandingPageForm()
       // Create fields
        $fields = new FieldSet(
           new TextField('FirstName', 'First Name'),
           new TextField('LastName', 'Last Name'),
           new EmailField('Email', 'Email')
        // Create action
        $actions = new FieldSet(
           new FormAction('SendLandingPageForm', 'Send me an intro package')
          // Create action
          $validator = new RequiredFields('FirstName', 'LastName', 'Email');
        $form = new Form($this, 'LandingPageForm', $fields, $actions, $validator);
        return $form;
       function SendLandingPageForm($data, $form) {



    <form id="Form_LandingPageForm" action="<% control LandingPageForm %>$FormHandlerURL<% end_control %>" method="post" enctype="application/x-www-form-urlencoded">

       <% 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 %>

             <label class="left" for="Form_LandingPageForm_FirstName">First Name</label>
             <input type="text" class="text" id="Form_LandingPageForm_FirstName" name="firstname" placeholder="First Name *" value="">
             <label class="left" for="Form_LandingPageForm_LastName">Last Name</label>
             <input type="text" class="text" id="Form_LandingPageForm_LastName" name="lastname" placeholder="Last Name *" value="">
             <label class="left" for="Form_LandingPageForm_Email">Email</label>
             <input type="text" class="text" id="Form_LandingPageForm_Email" placeholder="Email *" name="email" value="">
             <input class="hidden nolabel" type="hidden" id="Form_LandingPageForm_SecurityID" name="SecurityID" value="911284708">
          <div class="clear"><!-- --></div>
       <% if Actions %>
       <div class="Actions">
          <% control Actions %>
          <% end_control %>
       <% end_if %>
       <p class="notice">We will never share your private information.</p>

    I appreciate you help, thanks.


  • Willr
    Forum Moderator
    5511 Posts

    Re: How to include a class field value in from template? Link to this post

    Try <% control Controller %>$FormHandlerURL<% end_control %> as that will change your scope to the controller (where your FormHandlerURL variable is.

  • sajok
    Community Member
    82 Posts

    Re: How to include a class field value in from template? Link to this post

    Thanks it's working now..

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.