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.

Customising the CMS

Using FieldGroup and customizing field layout in ModelAdmin


Go to End
Reply

12 Posts   4696 Views

Avatar
Tesla

7 January 2011 at 8:14am Community Member, 9 Posts

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?

Avatar
swaiba

7 January 2011 at 1:25pm Forum Moderator, 1796 Posts

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

Avatar
Tesla

7 January 2011 at 3:09pm Community Member, 9 Posts

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.

Avatar
swaiba

7 January 2011 at 10:34pm Forum Moderator, 1796 Posts

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

Avatar
Stefdv

1 March 2011 at 6:15am Community Member, 110 Posts

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.

Avatar
swaiba

1 March 2011 at 10:37pm Forum Moderator, 1796 Posts

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.

Avatar
Stefdv

3 March 2011 at 9:10am Community Member, 110 Posts

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

Avatar
swaiba

3 March 2011 at 9:29am Forum Moderator, 1796 Posts

Maybe I should have written...

<StartField> and <EndField>

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

Go to Top