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 /

[SOLVED] Show edit from profilepage controller method/action


Reply


5 Posts   1021 Views

Avatar
micschk

Community Member, 19 Posts

13 November 2011 at 2:28am

Hi all,

I have created a page type to show a list of Members from a specific group. I want logged in users to be able to edit their details from this page (via a controller action). But I cannot seem to find a way to display the Form by calling the 'editprofile' action on my page.

I keep getting: [User Error] Uncaught Exception: Object->__call(): the method 'handleaction' does not exist on 'Form'

What am I doing wrong?

<?php

class MembersPage extends Page {
   
   public function Showmembers( ){
      // displays a list of members
   }

}

class MembersPage_Controller extends Page_Controller {
   
   static $allowed_actions = array(
      'show',
      'editprofile'
   );

   //Displays the Panellid detail page, using Panellid_show.ss template
   function show() {
      // shows a single members' profile
   }
   
   //Displays the profile edit form, NOT...
   function editprofile() {
         $fields = new FieldSet(
            new TextField('FirstName', '<span>*</span> Voornaam'),
            new TextField('SurName', '<span>*</span> Achternaam'),
            new EmailField('Email', '<span>*</span> Email')
         );
         $actions = new FieldSet(
            new FormAction('SaveProfile', 'Opslaan')
         );
         $validator = new RequiredFields('FirstName', 'Email');

         //Create form
         $Form = new Form($this, 'editprofile', $fields, $actions, $validator);

         //Populate the form with the current members data
         $Member = Member::CurrentMember();
         $Form->loadDataFrom($Member->data());

         //Return the form, this throws the User Error...
         return $Form;
   
      }
   }
   
   //Save profile
   public function SaveProfile($data, $form) {
      // Save the profile
   }
   
}

Avatar
Willr

Forum Moderator, 5513 Posts

13 November 2011 at 9:04am

The handler should return an array of extended attributes, not the form directly. You can also use $this->customize(array( ... ))->renderWith('Page_edit') but I personally prefer this shorter version.

function edit() {
return array(
'Form' => $this->EditProfileForm()
);
}

Which will render it into your $Form variable.

You can also override built in fields like Title, Content etc from that array.

Avatar
JonoM

Community Member, 103 Posts

13 November 2011 at 11:56am

This might be useful too - http://api.silverstripe.org/2.4/sapphire/security/Member_ProfileForm.html
I'm guessing in conjunction with WillR's code you might be able to simplify your EditProfileForm function to this?

function EditProfileForm() {

   return new Member_ProfileForm($this, 'EditProfileForm', Member::CurrentMember());

}

Avatar
micschk

Community Member, 19 Posts

14 November 2011 at 6:45pm

The handler should return an array of extended attributes, not the form directly.

@Willr; Thanks! It works perfectly, but I don't fully understand why;
Is this principle specific to Sapphire or is it a more general OO principle?
If so, would you know where I could find an explanation of it (or a name)?
(I'll also look into Ingo's book if I can find it explained somewhere)

@JonoM; Thanks, sounds like I could have saved myself some time :) I should have searched first...
I'm using some many-many relations via a checkboxfields though, and Uploadify ImageUploadFields, so I probably would have ended up writing (a big) part of the form anyway.

Avatar
micschk

Community Member, 19 Posts

4 January 2012 at 6:01am

Edited: 05/01/2012 3:16am

Hi all, best wishes for 2012!

I still havehad a few unresolved issues with this profile form... The most pressing one being I cannot seem to get it saved upon submit... I have the following code, instead of updating the member with the form's values, it just reloads the form with the old data... What am I missing here? The save function doesn't ever seem to get called.

OK, as it turns out, you still need to have the function that returns the form in your $allowed_actions

class PanelledenPage_Controller extends Page_Controller {
   
   static $allowed_actions = array(
      'show',
      'edit',
      'save',
      'EditProfileForm' // <-- This action has to be allowed as well for the submit to work...
   );
   
   function EditProfileForm() {
      // Returns actual form
   }
   
   // Action that gets called for editing...
   public function edit(){
      return array(
         'Form' => $this->EditProfileForm()
      );
   }
   
   public function save($data, $form) {
      // save/email form
   }

}