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.

Form Questions /

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

Sign Up Form that upon submition adds and entry in a custom Dataobject


Go to End
Reply


3 Posts   155 Views

Avatar
SilverstripeLearner

Community Member, 15 Posts

31 May 2017 at 11:05pm

Edited: 31/05/2017 11:16pm

Hello,

I have the following Form:

public function SignUpForm() {
		$form = Form::create(
			$this,
			__FUNCTION__,
			FieldList::create(
				TextareaField::create('FirstName', '')
					->setAttribute('placeholder', 'First Name')
					->addExtraClass('form-control'),
				TextareaField::create('LastName', '')
					->setAttribute('placeholder', 'Last Name')
					->addExtraClass('form-control'),
				EmailField::create('Email', '')
					->setAttribute('placeholder', 'Email Address')
					->addExtraClass('form-control')
			),
			FieldList::create(
				FormAction::create('SendSignUpForm', 'Submit')
			),
			
			RequiredFields::create('FirstName', 'LastName', 'Email')
		);
		
		return $form;
	}
	
	public function SendSignUpForm($data, $form) {
		$data_FirstName   = $data['FirstName'];
		$data_LastName    = $data['LastName'];
		$data_Email 	  = $data['Email'];
		new SignUpper();
		
		return $this->redirect($this->Link('?name=' . $data_FirstName . ' ' . $data_LastName . '&email=' . $data_Email));
	}

SignUpper is a custom object. Everytime someone completes the form, I want a Signupper entry to be added. I don't really know how to continue this. And also
later I would like to create a ModelAdmin to see all those entries.

I'll post here the SignUpper.php file as well:

class SignUpper extends Dataobject {
	public static $db = array(
		'FirstName' => 'Varchar(100)',
		'LastName'	=> 'Varchar(120)',
		'Email'	=> 'Varchar(120)'
	);
}

EDIT: A custom SQL query would work but I would like that solution to be a last resort situation. I want to do this the right way

Avatar
Michael J James

Community Member, 8 Posts

1 June 2017 at 12:17am

Edited: 01/06/2017 12:18am

Hi @SilverstripeLearner

You can do this two ways

First:

public function SendSignUpForm($data, $form) {
        
        $data_FirstName = $data['FirstName'];
        $data_LastName = $data['LastName'];
        $data_Email = $data['Email'];
	
        $signup = new SignUpper();
        $signup->FirstName = $data_FirstName;
        $signup->LastName = $data_LastName;
        $signup->Email = $data_Email;
        $signup->write();
		
        return $this->redirect($this->Link('?name=' . $data_FirstName . ' ' . $data_LastName . '&email=' . $data_Email));
}

Second:

The Second option will only work if the field names are the same as the object db fields

public function SendSignUpForm($data, $form) {
        
        $data_FirstName = $data['FirstName'];
        $data_LastName = $data['LastName'];
        $data_Email = $data['Email'];
	
        $signup = new SignUpper();
        $form->saveInto($signup);
        $signup->write();

        return $this->redirect($this->Link('?name=' . $data_FirstName . ' ' . $data_LastName . '&email=' . $data_Email));
}

Thanks
Michael

Avatar
SilverstripeLearner

Community Member, 15 Posts

1 June 2017 at 2:24am

Hi @Michael J James,

Thank you very much for your response. This has been very helpful, it works.

Just for my curiosity:
I was looking in the DataObject class to find how does a DataObject adds entries when you add the from the CMS and use the same php code. Is it by chance the second way you said ?

Have a great day Michael !