Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Multiple forms on front-end

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

Page: 1
Go to End
Author Topic: 321 Views
  • theade
    Avatar
    Community Member
    10 Posts

    Multiple forms on front-end Link to this post

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

       }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Multiple forms on front-end Link to this post

    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) {
    ..
    }

  • theade
    Avatar
    Community Member
    10 Posts

    Re: Multiple forms on front-end Link to this post

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

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