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

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


Go to End


3 Posts   908 Views

Avatar
Double-A-Ron

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: http://api.silverstripe.org/2.4/class-HiddenField.html

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)

Avatar
Willr

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.* - https://github.com/silverstripe/silverstripe-framework/blob/3.1/forms/FormField.php#L164. 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 - https://github.com/silverstripe/silverstripe-framework/commit/ca87b8b79496a78454d63c225f1f4f38fa55a150

Avatar
Double-A-Ron

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.

Ta
Aaron