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   4985 Views

Avatar
Tesla

Community Member, 9 Posts

7 January 2011 at 8:14am

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

Forum Moderator, 1804 Posts

7 January 2011 at 1:25pm

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

Community Member, 9 Posts

7 January 2011 at 3:09pm

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

Forum Moderator, 1804 Posts

7 January 2011 at 10:34pm

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

Community Member, 110 Posts

1 March 2011 at 6:15am

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

Forum Moderator, 1804 Posts

1 March 2011 at 10:37pm

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

Community Member, 110 Posts

3 March 2011 at 9:10am

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

Forum Moderator, 1804 Posts

3 March 2011 at 9:29am

Maybe I should have written...

<StartField> and <EndField>

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

Go to Top