php forms file-upload silverstripe

php - Silverstripe: Pase variable de URL para formar acción



forms file-upload (1)

¿Hay alguna manera de pasar una variable de URL a una acción de formulario? Lo tengo trabajando en un formulario de detalles del usuario, pero cuando intento hacerlo con un archivo de usuario, no funciona.

Como verá a continuación, tengo un formulario y una acción de guardado para guardar los detalles del usuario. Eso funciona bien

Cuando trato de pasar la variable URL al formulario Carga de archivos de usuario, no funciona. Dice que estoy tratando de obtener un valor de un no objeto.

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


OK, me las arreglé para resolver esto ...

Tuve que establecer una acción de formulario para dirigir la función de carga a la URL correcta. Parece que la ID se eliminó de la URL cuando hice clic en enviar, por lo que la función "getUser" no pudo ver el valor.

Aquí está el código de trabajo para la función Cargar formulario:

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