Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to add form fields to admin?

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2836 Views
  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    How to add form fields to admin? Link to this post

    I've managed to add a signup form, which is connected to a newsletter. However int he admin section of SS, the fields "first name" and "surname" are still there and my new fields "Company" and "country" aren't.

    I've checked out http://doc.silverstripe.com/doku.php?id=forms&s=form but it doesn't seem to answer this question...

  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    Re: How to add form fields to admin? Link to this post

    Any news on this one? it's kind of major because we want to store a little more info on the beta testers for PlanHQ and at the moment we're missing it all...

  • Simon
    Avatar
    Core Development Team
    27 Posts

    Re: How to add form fields to admin? Link to this post

    Ok, the standard member does'nt have any of the extra fields on it, so the form handler doesn't save the fields you've got in the form.

    You'll need to do these things.
    ¤ Create a new Member class,
    ¤ Add the fields "Company" and "Country" to the member,
    ¤ Replace the getCMSfields on the member, to reference the new fields.
    ¤ Change the form to create and save into the new member.
    ¤ Finally add the code below to change the default member class in _config to use your special new class..

    Object::useCustomClass( 'Member', 'newMemberClass' );

  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    Re: How to add form fields to admin? Link to this post

    ummmm... Mark and I are trying to speak your language, but failing... here is my code based on your forum reply and the member page I found in the documentation - i dont think i am getting an error, but i am also not getting any fields in the font end - I have also added: Object::useCustomClass( 'Member', 'BetaTesterForm' ); to the _config file

    code:

    class BetaTesterForm extends Member {
       static $db = array(
          "Company" => "Text",
          "Country" => "Text",
       );
    function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->insertBefore(new TextField("Company"), "Text");
       $fields->insertBefore(new TextField("Country"), "Text");
       $fields->removeByName("Firstname");
       $fields->removeByName("Surname");
       return $fields;
    }
    function BetaTesterForm() {

    return new Form($this, "BetaTesterForm", new FieldSet(
    // List the your fields here
    new EmailField("Email", "", "Email address..."),
             new TextField("Company", "", "Company..."),
             new TextField("Country", "", "Country...")

    ), new FieldSet(
    // List the action buttons here
    new FormAction("signup", "")

    ), new RequiredFields(
    new EmailField("Email", "", "Email address..."),
             new TextField("Company", "", "Company..."),
             new TextField("Country", "", "Country...")
    ));
    }
    function signup($data, $form) {
       $member = new Member();
    $form->saveInto($BetaTesterForm);
          $member->write();
          $member->Groups()->add(2);
          Director::redirect('thanks-for-registering/');
          
    }
    }

    also... I did try to look this up in docs before asking, it's just that dummies like me dont even know that 'Members' exist, so we look up 'adding form fields to admin' which doesn't come up with any results...

  • Simon
    Avatar
    Core Development Team
    27 Posts

    Re: How to add form fields to admin? Link to this post

    Your using the wrong function on getCMSFields() - see below.

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Main",new TextField("Company"));
    $fields->addFieldToTab(Root.Content.Main",new TextField("Country"));
    return $fields;
    }

    everything else looks good

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