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, biapar, Willr, Ingo, swaiba, simon_w

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


Reply

3 Posts   384 Views

Avatar
Double-A-Ron

20 September 2013 at 2:18pm (Last edited: 20 September 2013 2:32pm), Community Member, 604 Posts

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

20 September 2013 at 6:17pm Forum Moderator, 5511 Posts

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

26 September 2013 at 9:59pm Community Member, 604 Posts

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

Sticking with the LiteralField in this case.

Ta
Aaron