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

Form Validation: Require that one item of a SelectionGroup is selected


Go to End


1564 Views

Avatar
NicolasLeuenberger

Community Member, 11 Posts

29 September 2017 at 2:52am

I want customers to rate a product from 1-4. I've made a form with a SelectionGroup containing 4 SelectionGroup_Item fields. I'm trying to validate if the customer has made a selection. Server-side it seems to work, the form only goes through when an option is selected. But no error-message is shown if none are selected, so the customer is left in the dark as to why their form wasn't sent.

Any ideas? Thanks very much!

$gradeBoxes = array(
    new SelectionGroup_Item($this->Note4, '', $this->Note4),
    new SelectionGroup_Item($this->Note3, '', $this->Note3),
    new SelectionGroup_Item($this->Note2, '', $this->Note2),
    new SelectionGroup_Item($this->Note1, '', $this->Note1),
);
$gradeField = new SelectionGroup('Grade', $gradeBoxes);
$gradeField->setCustomValidationMessage('Please select one.');
$fields->push($gradeField);
$required = new RequiredFields(array('Grade'));
$form = new Form($this, 'FeedbackForm', $fields, $actions, $required);