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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Auto Populate Fields

Go to End



Community Member, 29 Posts

20 October 2011 at 5:26am

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 = '';
		$Subject = 'Subject';
		$email = new Email($From, $To, $Subject);
		// Send Email to User
		$autoFrom  = '';
		$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");