A rookie question that has me a little stumped.
Requirement - be able to update some details of a number of Members, list all members that need to be edited on the same page. On the front-end. Ideally with one button to update them all in one go.
I first thought I could make one fieldList, loop through the members and add fields to the fieldList. Well that obviously didn’t work.
So approach two, is to loop in the template and create lots of individual forms for each member, passing through the ID of each member. This works, but it means I have a submit button for each, not the end of the world as I could hide them and do the form submits with Ajax for JS, and have this form vanilla as a fall-back for non-JS. Anyway, it renders the form well, but on submit, I no longer have the ID that is passed from the template. With what might be a hack, I can get the ID from the postvar on submit, but that seems like a hack - is there a better/correct way of doing this?
Here’s a stripped down version of what I have (I hope I haven’t added bugs when changing var/key names for this post). Any help much appreciated.
<% loop $MembersThatNeedEditing %>
$Top.MemberEditForm($ID)
<% end_loop %>
function MembersThatNeedEditing(){
return DataObject::get("Member", “NeedsEditing=1”);
}
function MemberEditForm($memberToEditID)
{
//this feels wrong
if(!is_numeric($memberToEditID)){
if(isset($_POST[‘MemberToEditID'])){
$memberToEdit = DataObject::get_one("Member", "ID=".$_POST['MemberToEditID']);
}
}else{
$memberToEdit = DataObject::get_one("Member", "ID=".$memberToEditID);
}
$fields = new FieldList(
);
$fields->push(new HiddenField('MemberToEditID', 'MemberToEditID', $memberToEdit->ID));
$fields->push(new LabelField(‘MemberName', $memberToEdit->getName()));
$fields->push(new TextField(‘SomeValueThatNeedsEditing’,’Edit this value’));
$actions = new FieldList(
new FormAction(‘EditMember’, 'Edit Member’)
);
$Form = new Form($this, ‘EditMemberForm’, $fields, $actions);
return $Form;
}
function EditMember($data, $form){
$memberToEdit = DataObject::get_one("Member", "ID=".$data['MemberToEditID']);
$memberToEdit-> SomeValueThatNeedsEditing = $data['SomeValueThatNeedsEditing'];
$memberToEdit->write();
return $this->redirect($this->Link('?success=1'));
}