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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to add css classes and ID's to form elements from the controller


Go to End


4 Posts   1346 Views

Avatar
Bagzli

Community Member, 71 Posts

18 January 2015 at 11:32am

How can I add a class name or assign an ID to a form element that I can use to reference to it on my css files.

Avatar
nzstephenf

Community Member, 63 Posts

19 January 2015 at 1:32am

Edited: 19/01/2015 1:33am

Hey Bagzli,
It's a piece of cake to do.

Just have a look at the sample I made below.
Just adapt it to your own code and you're good to go!

function Form(){
  $actions = new FormAction('submit', 'Submit Form');

  $textfield = new TextField('Text');
  $textfield->setAttribute('class', 'your-class-name goes-here you-can-have-as-much-as-you-want');
 
  $emailfield = new EmailField('Email');
  $emailfield->setAttribute('id', 'enter-your-id-here');

  $fields = new FieldList(
      $textfield,
      $emailfield
  );

  $form = new Form($this, 'Form', $actions, $fields);
  new $form;
}

Just make sure you have the CSS linked up to the page with the respective form on it and you'll be sussed out in no time! :)

Let me know how you go,
Stephen

Avatar
Bagzli

Community Member, 71 Posts

19 January 2015 at 6:08am

Thank you very much!

Avatar
votuduc

Community Member, 1 Post

19 January 2015 at 6:19pm

Thank you very much.

<a href="http://Cookie-Jar.net">Quan ao tre em</a>
<a href="http://Cookie-Jar.net/shop">Quan ao tre em</a>