Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Auto Populate Fields

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

Page: 1
Go to End
Author Topic: 1001 Views
  • VictorH
    Community Member
    29 Posts

    Auto Populate Fields Link to this post

    I have the code below that is displaying a form and when a user begins filling out the "Credit Union*" field it generates a list of suggestion which they can choose from to populate the field. This is extended from the tutorial on this page:

    Everything is working thus far but I want to add some extra functionality. I want to add a hidden field, a city text field and state dropdown field but if the user uses a suggestion from the list I want it to automatically fill out the 3 new fields. Adding the fields is not a problem. I need help populating the fields based on the suggestion selected.

    If no suggestion is selected a user enters custom entry for Credit Union then the fields don't automatically populate.

    Credit Unions are a dataObject with the related information.



    class CreditUnion extends DataObject {
       static $db = array(
          'Name' => 'Text',
          'CN' => 'Text',
          'City' => 'Text',
          'State' => 'Text'
       static $has_one = array(
          'Page' => 'Page'

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push (new TextField('Name', 'Name'));
          $fields->push (new TextField('CN', 'CN'));
          $fields->push (new TextField('City', 'City'));
          $fields->push (new TextField('State', 'State'));
          return $fields;



    class FormPage extends Page {
       static $db = array('ThankYou' => 'Varchar(255)');
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new TextareaField('ThankYou', 'Thank You Message'));
          return $fields;

    class MemberSearchPage_Controller extends Page_Controller {
       static $allowed_actions = array(
       public function init() {
             jQuery(document).ready(function() {
                var options, a;
                 options = { serviceUrl:'" . $this->Link('results') . "' };
                 a = jQuery('#Form_SignUpForm_credit_union').autocomplete(options);
       public function SignUpSuccess() {
          return isset($_REQUEST['signupsuccess']) && $_REQUEST['signupsuccess'] == "1";
       public function SignUpForm() {
          // Sign Up Form Fields
          $fields = new FieldSet(
             new TextField('first_name','First Name*'),
             new TextField('last_name','Last Name*'),         
             new TextField('credit_union', 'Credit Union*', $this->getSearchQuery()),
             new EmailField('email','E-mail address*')
          // On Submit
          $actions = new FieldSet(
             new FormAction('doSignUp', 'Submit')
          // Fields to validate
          $validator = new RequiredFields('first_name', 'last_name', 'credit_union', 'email');

          return new Form($this, 'SignUpForm', $fields, $actions, $validator);

       function results() {
          if($credit_union = $this->getSearchQuery()) {
             $credit_union = Convert::raw2xml($credit_union);
             //Search for our query - Pretty basic example here
             $Results = DataObject::get('CreditUnion',
                "MATCH (Name) AGAINST ('$credit_union')
                OR Name LIKE '%$credit_union%'"

             //For AutoComplete
             if(Director::is_ajax()) {
                $Members = $Results->map('ID', 'Name');
                $Suggestions = "['" . implode("','",$Members) ."']"   ;
                return $json = "{
                   query : '$credit_union',
                   suggestions : $Suggestions
       function doSignUp($data, $form) {
          // Store in DB
          $submission = new SignUpSubmission();
          // Send Email to Admin
          $From = $data['email'];
          $To = 'email@test.com';
          $Subject = 'Subject';
          $email = new Email($From, $To, $Subject);
          // Send Email to User
          $autoFrom = 'email@test.com';
          $autoTo = $data['email'];
          $autoSubject = 'Subject';
          $autoEmail = new Email($autoFrom, $autoTo, $autoSubject);

          Director::redirect(Director::baseURL(). $this->URLSegment . "/?signupsuccess=1");

       function getSearchQuery() {
             return $this->request->getVar("query");

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.