Dallby Dallby - 2 months ago 24
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
Requirements::javascript('module-memberprofiles/javascript/MemberProfileUpdate.js');
Requirements::set_force_js_to_bottom(true);

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

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

$form = new Form ( $this, 'EditUserDetails', $fields, $actions, $validation);
$form->loadDataFrom($this->getUser());
$form->setTemplate('MemberProfilePage_UserDetailsForm');
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');
}

$form->saveInto($table);
$table->write();
$this->redirectBack();

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);

$form->loadDataFrom($this->getUser());

return $form;
}

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

$up = new Upload();

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

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

//$this->redirectBack();
return $this;
}
}

Answer

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'),
            HiddenField::create('ID','',$this->getUser()->ID)
        );

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

        $form = Form::create($this, 'UploadUserFile', $fields, $actions);
        $form->setFormAction($this->Link().'UploadUserFile/'.$this->getUser()->ID);

        return $form;
    }