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

Why is field layout hard-wired in FormField.php and not in a template?


Go to End


4 Posts   2280 Views

Avatar
losqualo

Community Member, 17 Posts

6 September 2010 at 2:00pm

Edited: 06/09/2010 2:01pm

I've been wondering where the nested div structure surrounding form fields came from since I could find no template anywhere that specified this.

I finally found it within the FormField.php file (sapphire/forms/FormField.php), which seems a little odd... The functions 'Field', 'FieldHolder' and 'SmallFieldHolder' are all culprits in actually generating html, instead of passing data to a template.

Is there any particular reason for doing it this way? It makes customising form structure more complicated than just having a suitable template local to the site folder.

So, to get around this and regain control, what is the best route. Do I create a local copy of FormField.php as mysite/code/CustomFormField.php?

That being the case, should it extend FormField or RequestHandler, or would either one do?

Also, how and where do I control the insertion of javascript related to form fields that don't exist? For example, my search form consists of an input and an action, yet there is javascript being auto inserted that relates to select lists and textareas that aren't there... Why? Can they be safely removed and if so, how/where?

Thanks in advance.

Rob

Avatar
Willr

Forum Moderator, 5523 Posts

6 September 2010 at 6:50pm

The reason for this is because in the CMS it was including over 100 form fields per request so having them as templates slowed the CMS down significantly. The change happened over 3 years ago so with the new Zend Caching for the templates might render this point as moot but at the time the performance improvement was worth the hassle!

Avatar
losqualo

Community Member, 17 Posts

6 September 2010 at 7:42pm

Thanks Will,

If I want to apply different layout/markup on specific forms then what would be the best way of achieving this?

Is it possible to have a mysite/code/customFormField.php for this...

I'm having problems with the middleColumn div and form verification (via javascript)

Ideally I'd want the middleColumn background colour to change on error, as well as displaying any error message. The classes however change on the input field within the middleColumn div.

I either have to change the javascript to apply the 'error' class to the containing div or the middleColumn div, or change background colour on middleColumn directly. Changing the outer class would be the preferred way to go allowing styling to be setup through the stylesheet.

In either case I've not yet found where the javascript gets setup to get a handle on what or where to make changes.

I'm looking forward to the day in a few months time when all this will seem trivially easy to me... right now it's overwhelming.

Rob

Avatar
CodeGuerrilla

Community Member, 105 Posts

23 January 2012 at 4:05pm

Edited: 23/01/2012 4:06pm

I would also like to know this have tried to subclass FormField and overload the FieldHolder so I can render the form fields with custom markup.

I created a class CustomFormField extends FormField added it it to config but get error:

Fatal error: Object::add_extension() - Extension "CustomFormField" is not a subclass of Extension

Was looking at FormTransformation is there an easier way I am missing here?