Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED] Show edit from profilepage controller method/action

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 870 Views
  • micschk
    Avatar
    Community Member
    19 Posts

    [SOLVED] Show edit from profilepage controller method/action Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: [SOLVED] Show edit from profilepage controller method/action Link to this post

    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.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: [SOLVED] Show edit from profilepage controller method/action Link to this post

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

    }

  • micschk
    Avatar
    Community Member
    19 Posts

    Re: [SOLVED] Show edit from profilepage controller method/action Link to this post

    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.

  • micschk
    Avatar
    Community Member
    19 Posts

    Re: [SOLVED] Show edit from profilepage controller method/action Link to this post

    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
       }

    }

    870 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.