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.

Form Questions /

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

3.2 Add Classes to divs in Custom Form


Go to End


2 Posts   2494 Views

Avatar
Vix

Community Member, 60 Posts

1 December 2015 at 9:30pm

I have a custom form and in previous versions of SS if i added something like this:

Textfield::create('MyField', 'MyField')->addExtraClass('small-field')

It would add the extra class to both the input and the div holding it. Now it just adds it to the input, so I get something like:

<div class="fieldgroup-field odd">
  <div class="fieldholder-small">
  <label class="fieldholder-small-label" for="Form_MyForm_MyField">My Field</label>
  <input type="text" name="MyField" class="text small-field" id="Form_MyForm_MyField" >
  </div>
</div>

How do I get it to add the class to the first div (fieldgroup-field)?

Thanks

Avatar
Devlin

Community Member, 344 Posts

2 December 2015 at 3:16am

I usually use a custom form template for this. Copy "framework/templates/Includes/Form.ss" to your project folder and rename it to MyForm.ss and instead of looping through the FormFields, manually add them.

$form = new Form();
$form->setTemplate('MyForm');
return $form;

// MyForm.ss
<fieldset>
	<div id="MyField" class="field myCssClass">
		<div class="anotherCssClass">
			<label class="left evenAnotherCssClass" for="{$FormName}_MyField">$Fields.dataFieldByName(MyField).Title</label>
			<div class="middleColumn myCssClass">$Fields.dataFieldByName(MyField)</div>
		</div>
	</div>
	<div class="clear"><!-- --></div>
</fieldset>