Dallby Dallby - 1 year ago 127
PHP Question

Silverstripe: Pass URL Variable to Form Action

is there a way to pass a URL variable to a form action? I've got it working on a user details form, but when I'm trying to do it with a user file upload it won't work.

As you will see below, I have a form and a save action for saving user details. That works fine.

When I try to pass the URL variable to the User File Upload form, it doesn't work. It says that I'm trying to get a value of a non-object.

// Get Client ID from URL Parameters
public function getUser() {
if( isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID']) ) {
return $user = Member::get()->byID($this->urlParams['ID']);
} else {
return $user = $this->request->postVars();

// Edit/Save a User's details
public function EditUserDetails() {
//Include JS for updating details

$fields = new FieldList(
$leftCol = CompositeField::create(
TextField::create('FirstName', 'First Name')
TextField::create('Surname', 'Surname')
TextField::create('Address', ''),
TextField::create('Suburb', ''),
DropdownField::create('State', '', singleton('Member')->dbObject('State')->enumValues())->setFieldHolderTemplate('UserDetails_StatePostCode'),
TextField::create('PostCode', '')->setFieldHolderTemplate('UserDetails_StatePostCode')
->addExtraClass('userdetails-address wrap')
TextField::create('Phone', 'Phone')
TextField::create('Email', 'Email')

$actions = new FieldList(new FormAction('SaveUserDetails', 'Save Profile'));
$validation = new RequiredFields(array('FirstName','Surname','Email'));

$form = new Form ( $this, 'EditUserDetails', $fields, $actions, $validation);
return $form;

public function SaveUserDetails($data, $form) {
$table = Member::get()->byID($this->getUser());

$members = Member::get();
$emailExists = $members->filter(array(
'Email' => $data['Email'],
'ID:not' => $table->ID

if( $emailExists->count() > 0 ) {
$form->sessionMessage('Sorry, that email address already exists. Please try again','bad');
return $this->redirectBack();
} else {
$form->sessionMessage('You have successfully updated this user\'s details.','good');


return $this;

//User file upload function
public function UploadUserFile() {

$fields = FieldList::create(
FileField::create('UserFiles', 'Upload files')

$actions = FieldList::create(FormAction::create('SaveUserFile', 'Upload files'));

$form = Form::create($this, __FUNCTION__, $fields, $actions, null);


return $form;

//Refresh files function
public function SaveUserFile($data, $form) {

$up = new Upload();

$file = Object::create('File');
$up->loadIntoFile($data['UserFiles'], $file, 'User-Files');

if($up->isError()) {
//handle error here
}else {
//file uploaded
//$file->OwnerID = 3;

return $this;

Answer Source

OK, I managed to figure this one out...

I had to set a form action to direct the upload function to the correct URL. It appears that the ID was being removed from the URL when I clicked submit, so the "getUser" function couldn't see the value.

Here's the working code for the Upload Form function:

public function UploadUserFile() {

        $fields = FieldList::create(
            FileField::create('UserFiles', 'Upload files'),

        $actions = FieldList::create(
            FormAction::create('SaveUserFile', 'Upload files')
                ->addExtraClass('button rounded solid')

        $form = Form::create($this, 'UploadUserFile', $fields, $actions);

        return $form;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download