Jump to:

10994 Posts in 2728 Topics by 1819 members

All other Modules

SilverStripe Forums » All other Modules » Userforms adapted?

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 3088 Views
  • Max Bradbury
    Avatar
    Community Member
    7 Posts

    Re: Userforms adapted? Link to this post

    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...

  • Max Bradbury
    Avatar
    Community Member
    7 Posts

    Re: Userforms adapted? Link to this post

    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);
       }

  • Max Bradbury
    Avatar
    Community Member
    7 Posts

    Re: Userforms adapted? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Userforms adapted? Link to this post

    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.

  • Max Bradbury
    Avatar
    Community Member
    7 Posts

    Re: Userforms adapted? Link to this post

    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.

    3088 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.