Silverstripe:将URL变量传递给表单操作

时间:2016-08-30 06:46:30

标签: php forms file-upload silverstripe

有没有办法将URL变量传递给表单操作?我已经将它用于用户详细信息表单,但是当我尝试使用用户文件上传时,它将无法正常工作。

正如您将在下面看到的,我有一个用于保存用户详细信息的表单和保存操作。这很好。

当我尝试将URL变量传递给“用户文件上载”表单时,它不起作用。它说我正试图得到一个非对象的值。

// 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; 
        }
    }

1 个答案:

答案 0 :(得分:1)

好的,我设法把这个想出来了......

我必须设置表单操作以将上传功能定向到正确的URL。当我点击提交时,似乎ID正从URL中删除,因此“getUser”功能无法看到该值。

以下是上传表单功能的工作代码:

k