Jump to:

22976 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » form ajaxify

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: 1226 Views
  • Andre
    Avatar
    Community Member
    125 Posts

    form ajaxify Link to this post

    Hi there,

    I'm stuck in ajaxifying my frontend forms. I was playing a little bit and got ajax pagination to work and loading of forms inside of facebox. Also I found some hints like FormResponse or jQuery forms, but not realy found a way how to implement this.

    My Goal is to open a form inside a facebox with dynamic parameters handled by URL.

    But to really understand avery part of it, I will try a more simple approach. Therefor I was thinking about a simple usecase.

    Lets have a List of Friends which can be added and ets track how many money borrowed to or from them.

    Hopefully this full thread can serve as a nice tutorial when finished.

    I will start with the simple FriendsAdd Form and add this to the Page Controller:

    public function AddFriendForm(){

    $fields = new FieldSet(
    new TextField('Name', 'Name')
    );

    // Create validator
    $validator = new RequiredFields('Name');

    // Create actions
    $actions = new FieldSet(
    new FormAction('addFriend', 'add')
    );

    return new Form($this, 'AddFriendForm', $fields, $actions, $validator);
    }

    public function addFriend($data, $form){

    if($Friend = DataObject::get_one("Friend", "Name='". Convert::raw2sql($data['Name'])."'")){
    $form->addErrorMessage("Blurb", 'This Friend Name allready exists!', "bad");

    Director::redirectBack();
    return;
    }

    $Friend = new Friend();

    $Friend->Borrowed = 0;

    $form->saveInto($Friend);

    $Friend->write();

    Director::redirectBack();

    return;
    }

    Adding the Variable $AddFriendForm to the Template will show the form now. On Submit, the full Page reloads. What do I have to add or to do, to submit the form by ajax without reloading the page. The DataobjectManager is a nice example for what I Whant. After submitting the form there, it is cleared and the resultmessage is shown.

  • Andre
    Avatar
    Community Member
    125 Posts

    Re: form ajaxify Link to this post

    Anyone any Idea?

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: form ajaxify Link to this post

    I don't know how you're submitting your form, (load() ajax() post()), but each has his callback option that you could use to reload a different portion of the page on completion?

  • Andre
    Avatar
    Community Member
    125 Posts

    Re: form ajaxify Link to this post

    That's the point. At the moment I'm not submitting my Form by Ajax, because I don't know how to do it the typical Silverstripe Way.

    There is a hint in the old wiki which points to FormResponse, to be used on the Serverside. But this doesn't really help without any documentation on how to act on the clientside.

    If anyone can give me this small portion of code or an advice I'l be very happy.

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: form ajaxify Link to this post

    I don't think there is 'a typical SilverStripe way', there probably are many..

    I've been doing a bit of Ajax, and documented some of it on the site where I keep my SilverStripe notes. You'd be welcome to it - only I feel kind of awkward because I really don't want it to look like I'm promoting any site. Advice on proper behaviour please, anyone?

    For now - if you send me an email, I'll send you the link.

  • Andre
    Avatar
    Community Member
    125 Posts

    Re: form ajaxify Link to this post

    I'd be pleased if you mention your site here. Think there's nothing wrong on promoting helpfull stuff.

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