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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

form ajaxify


Go to End


6 Posts   1772 Views

Avatar
Andre

Community Member, 146 Posts

11 April 2011 at 8:32am

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.

Avatar
Andre

Community Member, 146 Posts

12 April 2011 at 8:22am

Anyone any Idea?

Avatar
martimiz

Forum Moderator, 1391 Posts

13 April 2011 at 3:00am

Edited: 13/04/2011 3:00am

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?

Avatar
Andre

Community Member, 146 Posts

13 April 2011 at 3:06am

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.

Avatar
martimiz

Forum Moderator, 1391 Posts

13 April 2011 at 8:29am

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.

Avatar
Andre

Community Member, 146 Posts

13 April 2011 at 8:48am

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