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.

Archive /

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

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

Dinamic Forms based upon DropdownField selection.


Go to End


2 Posts   1537 Views

Avatar
carlossg

Community Member, 13 Posts

2 August 2008 at 10:26am

Edited: 02/08/2008 11:15pm

Hi all,

Need some help, i've been looking around all the docs an tutorials but wasn't able to find what i'm looking for.

Creating a site to manage a secondary school, subjects, teachers, etc.
I want to allow teachers connect to the site mysite/manageSubject and add additional information about the subject each teaches.
As there are hundreds of subjects, no sense creating a page for each one, Just one manage page with a drop down where available subjects are selectable.

First of all don't know how to retrieve the item(value) selected in the dropdownField?¿?¿.

How do I load the content stored in a DataObject, therefore in the database when user(teacher) wants to edit its subject information?

Initially i've created a form with a dropdownField(filled with subjects from database) with a submit button (can I align the submit button right beside dropdownField like search form or upload files form?). And now, upon selection, want to load from database any information if exists related with subject, edit, and save it.

I'm quite confused and stuck (two days with SS, - so many information-) and I need some advice and help.

Here is the code.

Thanks a lot.

SubjectManage.php


class SubjectManage_Controller extends Page_Controller {
	
	function SubjectDetailForm() {
      // Create fields
      
      $mySet = DataObject::get("Subject");
			
			$dropdownfield = new DropdownField(
  			'Subject',
  			'SUBJECT_LABEL',
  			$mySet->toDropDownMap('ID', 'SubjectName')
  		);
  		$fields = new FieldSet($dropdownfield);
 
      // Create actions
      $actions = new FieldSet(
         new FormAction('SubjectDetailForm', 'Ok')
      );
            
      return new Form($this, 'BrowserPollForm', $fields, $actions);
   }
   
   function doSubjectForm($data, $form) {
        Debug::show($data);
        /*How do i get the value selected in dropdownField?*/
     }
   
}

Subject.php

class Subject extends DataObject  {
 
   static $db = array(
      'SubjectName' => 'Text',
      'Calendar' => 'Text',
      'Contents' => 'Text'
      'Evaluation' => 'Text'
   );
   
   static $has_one = array(
      'MyTeacher' => 'Teacher',
      'MyDepartment' => 'Department'
   );
}

Avatar
Sean

Forum Moderator, 922 Posts

2 August 2008 at 8:53pm

Edited: 03/08/2008 4:19pm

Here's how you can save the data that was submitted. I've added some code and comments to your existing form submit handler.


function doSubjectForm($data, $form) { 
Debug::show($data); 
/*How do i get de value selected in dropdownField?*/ 

// This is the value of the subject dropdown field
Debug::show($data['Subject']);

// This is a generic way of saving any data that can be saved in the form into a DataObject
// Instead of creating a new one, you could retrieve a previously existing one using DataObject::get_one()
$obj = new SomeDataObject();
$form->saveInto($obj);

// Write the object to the database, creating a new table record
$obj->write();

}