Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Using FieldGroup and customizing field layout in ModelAdmin

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

Page: 1 2
Go to End
Author Topic: 4224 Views
  • Tesla
    Avatar
    Community Member
    9 Posts

    Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    I have been working with ModelAdmin and I'm having trouble getting the fields to display the way I want. When I click the create record button the fields are displayed on the tabs I have created, but some of them I want to be inside a fieldgroup.

    function getCMSFields()
       {
          $fields = parent::getCMSFields();

          $fields->addFieldToTab("Root.Main", new TextField('Name', 'Name'));   
          $fields->addFieldToTab("Root.Main", new TextField('Country', 'Country'));
          $fields->addFieldToTab("Root.Main", new TextField('Continent', 'Continent'));
          $fields->addFieldToTab("Root.Main", new FieldGroup(
             new TextField('FirstYear', 'First Year'),
             new TextField('LastYear', 'LastYear')
          ));

          return $fields;
       }

    I get this error with this code.

    Error: "collateDataFields() I noticed that a field called 'FirstYear' appears twice in your form: 'Form_AddForm'. One is a 'TextField' and the other is a 'TextField'" at line 140 of ....../sapphire/forms/CompositeField.php

    Is this not supported in ModelAdmin, or am I missing something?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Hi,

    Seems like you are adding the field again... Remember by default ModelAdmin creates your form with all the fields from the mangaed_model... try adding this after the parent:: call...

    $fields->removeByName('FirstYear');

    ... to remove the field ModelAdmin has probably created for you

  • Tesla
    Avatar
    Community Member
    9 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Hi swaiba

    Thanks for the reply.

    That took the error away but it did not move those two fields to be beside each other. This isn't a huge issue, purely aesthetics. I have 50 or more fields in this model and I'm just trying to minimize scrolling.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Couple of hints...

    1) The order of the fields specified int he DataObject are the order they are displayed in ModelAdmin form.
    2) you can replace a fields with...

    $fields->replaceField('Name',new TextField('Name'));


    (stead of add field to tab)
    3) You could remove the field from teh main tab, then add the field to another tab...

    $fields->removeByName('Name');
    $tab = $fields->findOrMakeTab('Root.NewTab');
    $tab->push(new ImageField('Name'));


    4) You can create your own pieces of HTML throughout the form by inserting before/after your own code...

    $fields->insertAfter('<hr />','ExistingFieldName');


    ...you could insertAfter or insertBefore, you could start, then stop a table or as above I've entered a HR tag...

    p.s. I've never used FieldGroup so I cannot advise on it

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Swaiba,

    Would you mind explaining this a little further.

    I searched through the whole forum about creating a nicer layout for the modeladmin form. All i find are "bits-and-pieces".

    I need to reorganize the modeladmin form to get a 'cleaner' presentation. Meaning; more fields in a row and put each field in the next row directly above/below the other fields.

    I read a lot about insertafter insertbefore etc. but i just can't get any 'grip'on it.

    I've used fieldgroups now... it works but it's not getting any nicer.

    I attached a file to explain.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    didn't I already send you this link?
    http://www.silverstripe.org/customising-the-cms/show/15655

    I encourage someone to post the correct usage of FieldGroup or more accurately answer this question - all I ever needed was a couple of fields in line.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Swaiba,

    I'm sorry...Yes you did send me that link in an other post. One question remains...how and where do i specify the strings $strStartField and $strEndField?

    Would it be like $strEndField = array() ?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using FieldGroup and customizing field layout in ModelAdmin Link to this post

    Maybe I should have written...

    <StartField> and <EndField>

    they are to be replaced by fields from your $db array

    4224 Views
Page: 1 2
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.