Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Dinamic Forms based upon DropdownField selection.

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: 1220 Views
  • carlossg
    Avatar
    Community Member
    13 Posts

    Dinamic Forms based upon DropdownField selection. Link to this post

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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Dinamic Forms based upon DropdownField selection. Link to this post

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

    }

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