Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Desperate for help creating an "edit" form

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

Page: 1
Go to End
Author Topic: 550 Views
  • Bimble
    Avatar
    Community Member
    16 Posts

    Desperate for help creating an "edit" form Link to this post

    I'm finding it inexplicably hard to create a “edit” form. The approach I’m taking is to use an argument in the URL to pass the ID of an “AutoMap” Dataobject and then load that into an edit form.

    The code below works for the AutoMap_Contoller “form” action, but not the “editform”. It returns the error that the “Call to a member function allParams() on a non-object”. However, even if I mess around with the code and replace it with a hardcode ID, the system runs out of php memory, so there must be something else fundamentally broken in my code.

    class AutoMap_Controller extends Page_Controller {
    private static $allowed_actions = array('form', 'editform');

    public function form() {
    $fields = new FieldList(
    new TextField('Name', 'Name', $fieldvalue->name),
    new TextareaField('Code', 'Your Code', $fieldvalue->code),
    new TextField('StringList', 'String List')
    );
    $actions = new FieldList(
    new FormAction('submit', 'Submit')
    );
    $validator = new RequiredFields('Name', 'Code');
    $form = new Form($this, 'Form', $fields, $actions, $validator);
    return $form;
    }

    public function editform(SS_HTTPRequest $request) {
    $arg = $request->allParams();
    $AutoMap = AutoMap::get()->byID($arg['ID']);
    $fields = new FieldList(
    new TextField('Name', 'Edit Name'),
    new TextareaField('Code', 'Edit Code'),
    new TextField('StringList', 'Edit String List')
    );
    $actions = new FieldList(
    new FormAction('submit', 'Submit')
    );
    $editform = new Form($this, 'editform', $fields, $actions);
    $editform->loadDataFrom($AutoMap);
    return array(
    'Form' => $this->editform()
    );
    return $form;
    }

    public function submit($data, $form) {
    $newAutoMap = new AutoMap();
    $newAutoMap->name = $data['Name'];
    $newAutoMap->code = $data['Code'];
    $newAutoMap->stringlist = $data['StringList'];
    $newAutoMap->enabled = "Disabled";
    $newAutoMap->enabled = "Disabled";
    $newAutoMap->write();
    return array(
    'Content' => '<p>Automatic Mapping '.$newAutoMap->ID.' Added</p>',
    'Form' => ''
    );
    }
    }

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Desperate for help creating an "edit" form Link to this post

    Underneath a very basic example of an Add/Edit form. Based on the url it either adds or edits. Please extend and sanitize as needed

    Note: make sure your page template has $Form in it...

    - add: mydomain/mypage
    - edit: mydomain/mypage/edit/3

    class AutoMap extends DataObject {
       
       private static $db = array(
        'Name' => 'Varchar(100)',
        'Code' => 'Varchar(100)'
       );
    }

    class AutoMapPage extends Page {
       
    }

    class AutoMapPage_Controller extends Page_Controller {
       
       public static $allowed_actions = array(
        'Form',
        'edit'
       );
       
       public function Form() {
          
          $action = $this->urlParams['Action'];
          $id = (int)$this->urlParams['ID'];
          $automap = ($id)? $automap = AutoMap::get()->byID($id) : false;
          $submitCaption = ($automap) ? 'Edit' : 'Add';
          
          $fields = FieldList::create(
             TextField::create('Name', 'Name'),
             TextField::create('Code', 'Code'),
             HiddenField::create('ID', 'ID')->setValue($id)
          );
          $actions = FieldList::create(
             FormAction::create('submit', $submitCaption)
          );
          $validator = RequiredFields::create('Name', 'Code');
          
          $form = Form::create($this, 'Form', $fields, $actions, $validator);
          
          if ($automap) $form->loadDataFrom($automap);
          
          return $form;
       }
       
       public function submit($data, $form) {
          
          $automap = AutoMap::create();
          $form->saveInto($automap);
          $automap->ID = $data['ID'];
          $id = $automap->write();
          
          if ($id) {
             Session::setFormMessage($form->FormName(), 'Your Automap has been saved', 'good');
          }
          else {
             Session::setFormMessage($form->FormName(), 'Something went wrong...', 'bad');
          }   
          
          $this->redirect($this->Link() . "edit/$id");
       }
    }

  • Bimble
    Avatar
    Community Member
    16 Posts

    Re: Desperate for help creating an "edit" form Link to this post

    Thanks a lot for this, just what I neededl!! I think this example would be worth posting on the "Form" documentation for other new SS users to find.

    I made a few very minor edits as the form wasnt writing.

    class AutoMap_Controller extends Page_Controller {

    public static $allowed_actions = array(
    'Form',
    'edit'
    );

    public function Form() {

    $action = $this->urlParams['Action'];
    $id = (int)$this->urlParams['ID'];
    $automap = ($id)? $automap = AutoMap::get()->byID($id) : false;
    $submitCaption = ($automap) ? 'Edit' : 'Add';

    $fields = FieldList::create(
    TextField::create('Name', 'Name', $automap->name),
    TextField::create('Code', 'Code', $automap->code),
    HiddenField::create('ID', 'ID')->setValue($id)
    );
    $actions = FieldList::create(
    FormAction::create('submit', $submitCaption)
    );
    $validator = RequiredFields::create('Name', 'Code');

    $form = Form::create($this, 'Form', $fields, $actions, $validator);

    if ($automap) $form->loadDataFrom($automap);

    return $form;
    }

    public function submit($data, $form) {

    $newAutoMap = new AutoMap();
    $newAutoMap->name = $data['Name'];
    $newAutoMap->code = $data['Code'];
    $newAutoMap->ID = $data['ID'];
    $newAutoMap->write();

    if ($newAutoMap->ID) {
    Session::setFormMessage($form->FormName(), 'Your Automap has been saved', 'good');
    }
    else {
    Session::setFormMessage($form->FormName(), 'Something went wrong...', 'bad');
    }

    $this->redirect($this->Link() . "edit/$newAutoMap->ID");
    }

    }

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Desperate for help creating an "edit" form Link to this post

    form->saveInto($automap); should really work (it did for me!) but only if your formfields are an exact match to your DataTable fieldnames.

    This doesn't seem to go for the objects ID though - even if the ID field is present, so that's why I added that one. But anyway, glad it works for you!

    Martine

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