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.

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

Giving a HiddenField (or any other field) a custom id attribute

Go to End

3 Posts   749 Views


Community Member, 607 Posts

20 September 2013 at 2:18pm

Edited: 20/09/2013 2:32pm

SS 2.4.5

Pretty simple question, but I don't see anything in the docs:

I am creating a form for a frontend page. This page also has some custom JS that does some funky stuff to the form based on field id's. So I want full control over the id of any field, in this particular case, a hidden field.

The only work around I have found is to use LiteralField and give it an HTML field:

new LiteralField('Name', '<input id="thisIsMyId" name="Name" type="hidden" />');

Is this the only way? Or is there an argument/method I am missing with HiddenField, TextField etc to change the id attribute of the generated field?

P.S. We have a custom validation model for this form. So I can't think of a reason why the form would need Silverstripe's default generated id. (FormName_FieldName)


Forum Moderator, 5523 Posts

20 September 2013 at 6:17pm

If you want full control over the ID you're a bit outta luck for 3.* - You could create a Subclass of FormField and override the ID method as normal inheritance. I've made this customisable in 3.2 (master / unreleased) along with other improvements to the Form ID generation -


Community Member, 607 Posts

26 September 2013 at 9:59pm

Cheers Will, yeah I saw that when I dove into the code.

Sticking with the LiteralField in this case.