Jump to:

2011 Posts in 1436 Topics by 620 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: 554 Views
  • sajok
    Avatar
    Community Member
    82 Posts

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

    Hello,

    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:

    LandingPageForm.php

    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(
          'LandingPageForm'
       );
       
       //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);
        $form->setTemplate('LandingForm');
        return $form;
       }
       
       function SendLandingPageForm($data, $form) {
           ....
       }

    }

    landingForm.ss

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

    <fieldset>
             <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>
       </fieldset>
       
       <% if Actions %>
       <div class="Actions">
          <% control Actions %>
             $Field
          <% end_control %>
       </div>
       <% end_if %>
       
       <p class="notice">We will never share your private information.</p>
       
    </form>

    I appreciate you help, thanks.

    sajok

  • Willr
    Avatar
    Forum Moderator
    5508 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
    Avatar
    Community Member
    82 Posts

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

    Thanks it's working now..

    554 Views
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.