Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Using Forms with DataObjectDecorator

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 595 Views
  • purplespider
    Avatar
    Community Member
    80 Posts

    Using Forms with DataObjectDecorator Link to this post

    So I've got a few functions, and I'm using DataObjectDecorator to make them available for all pages, but also certain DataObjects.

    The functions contain a Form, and it's action method.

    I have the decorator working, as the form is being displayed fine on a page, however, the problems start when I try to submit the form, I'm getting the following error:

    [User Error] Uncaught Exception: Object->__call(): the method 'checkaccessaction' does not exist on 'OfferPage

    OfferPage is my page type, and the method "checkaccessaction" isn't one of mine, so what's going on?

    Here is my decorator code: http://spdr.me/lnTx

    Advice appreciated!

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    checkaccessaction() is a SilverStripe method that checks if a given action is allowed on a given controller. When your form is posted, you provide a form action in the url, and SilverStripe is now checking that for its validity. Any controller should have access to this function since it is defined in the base Controler class.

    So it looks the controller that you're using isn't really a controller. I've never really created form functionality using a decorator, but as you'd normally create the form from within the Page_Controller, I think you might want to decorate the Page_Controller instead of the Page class. In that case you should use the Extension class instead of the DataObjectDecorator, since the page_Controller isn't a DataObject...

    Hope this helps...

  • purplespider
    Avatar
    Community Member
    80 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    Ah, that makes sense, I'll give this a go tomorrow. Thanks martimiz, I'll let you know how I get on!

  • purplespider
    Avatar
    Community Member
    80 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    Ok, that's working great now on Pages, but still got a problem with it on DataObjects:

    For example I have my videos page at /videos and the URL for a video is /videos/view/34 (where 34 is the video ID). Now when I submit the comment form for a video, it's looking for the comment form method at /videos/view/34/CommentForm where it obviously doesn't exist as it's already calling the view action with an ID.

    Any ideas how I can work around this?

    Many Thanks
    James

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    Afaik that souldn't really happen. Even though you add the /view/23/ bit as $Action/$ID to the URL, SilverStripe should still recognize the original URLSegment, and submit to /videos/CommentForm. I've a similar situation here (the only difference being that my form is generated by a shortcode) and that works as it's supposed to.

    I'm using a simple Director::redirectBack(); at the end of the submission handler - and that takes me right back to the original DataObject on the page...

    So I don't know what's happening in your case - unless you made some other changes that could influence the way links are created?

  • purplespider
    Avatar
    Community Member
    80 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    Ah, so in that case it's probably because I am putting the form inside my dataobject (video) control, in the template, rather than directly on the page. When I move it outside of the control, it correctly submits to /videos/CommentForm.

    However, as this is a comment form, it needs to know the ID and Class of the current DataObject or Page within the form action. And when the CommentForm is placed outside of the dataobject control on the page, it is unable to obtain this information. What is the best way to deal with this?

    Many thanks again!
    James

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    The page controller knows the id of the video from the url. You should be able to get it (using $this->urlParams['ID'] if i recall it correctly) and send it to your template using some function like GetVideoID(), to create a hidden field...

  • purplespider
    Avatar
    Community Member
    80 Posts

    Re: Using Forms with DataObjectDecorator Link to this post

    Thanks so much martimiz, I got it all working!

    595 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.