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.

Form Questions /

SilverStripe documentation badly needs a complex and complete form example!


Reply


4 Posts   2512 Views

Avatar
Codetapper

Community Member, 8 Posts

18 August 2009 at 4:55pm

Edited: 18/08/2009 5:06pm

I've read a lot of posts in the form section, and the same sorts of questions keep coming up. I think a complete form tutorial that does more than just "this field requires some text" would really solve a lot of problems and eliminate a lot of forum questions!

Please note: I would happily write one if I knew how to get the form working as I want!

I think most users would appreciate a tutorial that does the following:

Sets up a form
Styles it (an asterisk beside required fields), perhaps lay it out in columns rather than the name with the field underneath it.
Allows you to change the "Sorry this field is required" type message for each field (if that's possible)
Optional parts that if a field is set to a certain value, it brings up another section.
A radio button list that has "Other" at the bottom, and if the radio button is set to that, a text field with "Other" appears (or is enabled, or whatever) allowing you to type in the text.
Checkbox behaviour similar to the above radio buttons

I think it REALLY needs to have client and server validation options too:
If a particular value is set in a field, a message appears.
If the user submits the form and parts are invalid, the server can return the form again with "this bit is invalid" on a certain field.

Has anyone managed to do anything like the above? I would love to see any code dealing with forms in this manner. In ASP/PHP on my own forms, I can do all sorts of stuff and I was hoping it would be just as easy in Silverstripe, but I get stuck and limited to validation along the lines of "this field must have a value in" and that's about it!

Some developer or SS legend - please help us!

PS: I have tried the UserDefinedForm which seems to have some great promise, but it doesn't work properly in Silverstripe (2.3.1 and I tried with 2.3.3 aswell) - loads of errors appear and I got fed up with it!

Avatar
Ingo

Forum Moderator, 801 Posts

23 August 2009 at 5:51pm

I think this is more a case of revising the validation API (both PHP and JS) in SilverStripe, making it more pluggable and configurable. We know about these problems, but currently can't devote significant time to it. See our (slightly outdated) initial brainstorms at http://open.silverstripe.com/wiki/development/validation. We're looking for external developers who can significantly contribute here, so anybody taking the charge here is welcome.

> Styles it (an asterisk beside required fields), perhaps lay it out in columns rather than the name with the field underneath it.

We usually add simple HTML markup:
new TextField('MyText', 'MyText <span class="required">*</span>')
You can automate this a little bit further by iterating through all fields and append this only to required ones:
foreach($myForm->Fields() as $field) {
if($field->Required()) $field->setTitle($field->Title() + <span class="required">*</span>');
}

> Allows you to change the "Sorry this field is required" type message for each field (if that's possible)

Currently this is difficult due to Validator.js javascript implementation. I recommend disabling built-in javascript validation,
and using a more fully featured library like jquery.validate.

> Optional parts that if a field is set to a certain value, it brings up another section.
A radio button list that has "Other" at the bottom, and if the radio button is set to that, a text field with "Other" appears (or is enabled, or whatever) allowing you to type in the text.

Both pretty custom features (dependent dropdowns etc.), seems like a case for custom javascript or a small jQuery plugin, rather than FormField PHP core.

Avatar
Codetapper

Community Member, 8 Posts

24 August 2009 at 9:00am

Thanks for those suggestions!

How exactly do you disable the javascript validation and use jquery instead? This is where a working example doing some of those things would be so useful - and probably a lot easier than changing the sapphire core!

Avatar
Ingo

Forum Moderator, 801 Posts

24 August 2009 at 10:11am

I've extended the docs with this information: http://doc.silverstripe.com/doku.php?id=validator#disable_javascript_validation