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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Desperate for help creating an "edit" form


Go to End


4 Posts   2771 Views

Avatar
Bimble

Community Member, 16 Posts

6 January 2014 at 3:53am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

6 January 2014 at 10:20am

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");
	}
}

Avatar
Bimble

Community Member, 16 Posts

7 January 2014 at 7:55am

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");
   }

}

Avatar
martimiz

Forum Moderator, 1391 Posts

8 January 2014 at 4:49am

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