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.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[userforms] RequiredFields highlight?


Go to End


9 Posts   4969 Views

Avatar
dio5

Community Member, 501 Posts

4 May 2009 at 10:09pm

I'm probably missing out on something but is there an easy way to make required fields stand out with userforms?

Like adding a css-classname to it or a '*'?

I've always been using customforms before where you can add an extra class or check if it's required.

Don't feel much for subclassing all the fields...

Avatar
Double-A-Ron

Community Member, 607 Posts

10 July 2009 at 3:40pm

Subscribe.

I can't see any way a user can differentiate a required field from a optional one until the validation tells them so.

Nor is there any way to even color them via css as the fields are all the same class.

Cheers
Aaron Cooper

Avatar
Double-A-Ron

Community Member, 607 Posts

13 July 2009 at 3:25pm

Did you ever get something sorted for this at all Dio?

I see another older post from you asking a similar question with no answer apart from yourself linking to the doku with setExtraClass method on it.

A better question really is: "Isn't this a pretty basic expectation to assume from a system that creates forms?". In fact, I could have sworn that this is exactly what it did prior to be being split from core.

I can't even see anything in the Bug Traca for this, but surely someone has it working due to lack of complaint.

Cheers
Aaron

Avatar
Thermalzombie

Community Member, 27 Posts

13 July 2009 at 8:03pm

Edited: 13/07/2009 8:04pm

How about just writing (Required) after the tittle such as.

Name (Required)

Have seen websites do that before.

Avatar
Double-A-Ron

Community Member, 607 Posts

13 July 2009 at 8:56pm

Yes, you can do that. However for the sake of clarity for the end user (clients). This should really be automated since the system allows them to select a field as required using a single checkbox in the admin.

The module builds these labels and fields based on selections made in the CMS. Which means that it really shouldn't be hard to apply a default "required identifyer" (*) and/or css extra class to the label at the same time.

I've dug into userforms and simply cannot find it.

Cheers
Aaron

Avatar
Willr

Forum Moderator, 5523 Posts

13 July 2009 at 10:25pm

Double-A-Ron - make an enhancement ticket for userforms 0.2 on open.silverstripe.com. I will get this into the stable 0.2 release for you. I think the implementation of it would just be an extra class to the containing div. Adding * to the labels automatically may confuse some clients who think - hey its not there it in the cms, why is it there sorta thing so I would avoid that. But added a required class would be a acceptable solution.

If you want to undertake this work yourself then to add the class to the form field go line line 211 of UserDefinedForm.php

if($field->Required) {
...
$fieldToAdd->addExtraClass('required'); // add required class
$fieldToAdd->setLeftTitle($fieldToAdd->LeftTitle() . ' *'); // add a * to the label
}

Avatar
Double-A-Ron

Community Member, 607 Posts

14 July 2009 at 11:46am

Thanks Will,

That's one of the blocks I played with earlier to no avail, although I wasn't attempting to addExtraClass to $fieldToAdd.

So that works. I do notice that this extra class is applied to the encapsulating div of the entire field (label and input). Like so:

<div id="EditableTextField355" class="field text  required">
<label class="left" for="Form_Form_EditableTextField355">First Name</label>
<div class="middleColumn">
<input type="text" class="text required" id="Form_Form_EditableTextField355" name="EditableTextField355" value="" maxlength="32" size="30" />
</div>
</div>

So it's not applying the required class to the label itself (as you stated). This is fine. I just set my css like this to make all required labels red:

.field.text.required label.left{
	color:#F00;
}

This probably makes sense (rather than adding the class to the label) as some people might want to do CSS things to the input on required fields too.

The line $fieldToAdd->setLeftTitle($fieldToAdd->LeftTitle() . ' *'); doesn't appear to have any effect what-soever sorry.

Would you be open to the idea of adding a field to the form CMS setup where users can enter their own "Required identifyer"? e.g. Some clients might choose to have a "*", some might want "(required)". This can be blank by default and if not, be appeneded one &nbsp; from the label in it's own <span> so CSS tricks can be applied to this. This one isn't a biggy for me, but it does appeal.

Ticket created for the CSS class: http://open.silverstripe.com/ticket/4379
Ticket created for adding the "Required Identifyer" field to the CMS seperately, as you may not be keen on the idea. http://open.silverstripe.com/ticket/4380

Cheers
Aaron

Cheers
Aaron

Avatar
RichardMortimer

Community Member, 15 Posts

25 September 2009 at 7:18pm

Expanding on willr's comment, this worked for me:

if($field->Required) {
...
   $fieldToAdd->setTitle($fieldToAdd->Title()." <span style='color:#F00'>*</span>");
}

Cheers

Richard

Go to Top