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.

Form Questions /

Multiple forms on front-end


Reply


3 Posts   405 Views

Avatar
theade

Community Member, 10 Posts

31 January 2014 at 12:41pm

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'));      

   }

Avatar
Willr

Forum Moderator, 5513 Posts

3 February 2014 at 6:42pm

You're better to have 1 single form for all the members and simply namespace the fields to the members ID

foreach($members as $member) {
$fields->push(new TextField("Name[$member->ID]", "Name"));
..
}

Then in your submit method you can iterate through all the names as this will give you an array of ID of the members to the value

foreach($data['Name'] as $member => $value) {
..
}

Avatar
theade

Community Member, 10 Posts

12 August 2014 at 1:07am

Yep, this fixed what I was trying to do. Thanks :)