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.