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 adapted?


Go to End


13 Posts   5875 Views

Avatar
Max Bradbury

Community Member, 7 Posts

8 June 2011 at 11:25pm

I managed to get this working by editing UserDefinedForm.php...

This "if" statement inside getRequiredFields():

if ($field->Required) {
    $rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
    $required->addRequiredField($field->Name);
}

became:

if ($field->Required) {
    $rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
    $required->addRequiredField($field->Name);
} else {
     if ($field->getValidation()) {
         $rules[$field->Name] = $field->getValidation();
     }
}

Not sure if that's really the best way of doing it but it seems to get the job done. Now, to figure out how to make the error messages more relevant...

Avatar
Max Bradbury

Community Member, 7 Posts

9 June 2011 at 12:29am

Sort-of-fixed my error message by adding this function to my custom numeric field.

	/**
	 * Return the error message for this field. Either uses the custom
	 * one (if provided) or the default SilverStripe message
	 *
	 * @return Varchar
	 */
	public function getErrorMessage() {
		$standard = 'Please enter a number';
        
                $errorMessage = ($this->CustomErrorMessage) ? $this->CustomErrorMessage : $standard;
		
		return DBField::create('Varchar', $errorMessage);
	}

Avatar
Max Bradbury

Community Member, 7 Posts

18 June 2011 at 12:59am

I was also having a problem with validation; if a user entered something into a numeric field and then blanked it again, the error message would remain and the form wouldn't submit unless the field was completed. The field wouldn't even attempt to validate with "required" set to "false", so I cooked something up to overcome this, which I'll post for the benefit of anyone Googling this:

inside getValidation():

        $options['number']   = true;
        $options['required'] = "function(element) {return element.val() != ''}";

You can also use "digits" instead of "number" if you want to make it integer-only.

Avatar
Willr

Forum Moderator, 5523 Posts

18 June 2011 at 3:48pm

Hey Max, Great work digging into these issues. Is there anyway you could put together a ticket on open.silverstripe.org and list everything you did (try making a diff file if you can). Sounds like this would be a good feature to include in the release.

Avatar
Max Bradbury

Community Member, 7 Posts

20 June 2011 at 11:58pm

Thanks, Will- it still needs work (i18n, non-JS validation, etc.) but I will try to put a ticket together at some point. Great module, by the way.

Go to Top