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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Impossible to create custom JS validation on field?


Go to End


2 Posts   1641 Views

Avatar
arturaz

Community Member, 2 Posts

30 November 2009 at 10:56am

I'm trying to create my custom field for admin interface and ensure it validates with JS.

I have my own custom field:

<?php

/**
 * Textarea that only allows emails separated by commas.
 *
 * @author Artūras 'arturaz' Šlajus <x11@arturaz.net>
 */
class EmailAreaField extends EmailField {
  const ERROR_MESSAGE = "One of the emails in the field is invalid.";
  const EMAIL_REGEXP = '/^.+@.+\.[a-z]+$/i';

  public function validate() {
    foreach (split(",", $this->value) as $email) {
      $email = trim($email);

      if (! preg_match(self::EMAIL_REGEXP, $email)) {
        $validator->validationError(
          $this->name,
          self::ERROR_MESSAGE,
          "validation"
        );

        return false;
      }
    }

    return true;
  }

  public function jsValidation() {
    $formID = $this->form->FormName();
		$error = self::ERROR_MESSAGE;
    $regexp = self::EMAIL_REGEXP;
		$jsFunc =<<<JS
Behaviour.register({
	"#$formID": {
		validateEmailAreaField: function(fieldName) {
			var el = _CURRENT_FORM.elements[fieldName];
			if(!el || !el.value) return true;

      var parts = el.value.split(',');
      for (var index = 0; index < parts.length; index++) {
        var part = parts[index].strip();
        if (! el.value.match($regexp)) {
          validationError(el, "$error", "validation");
          return false;
        }
      }

      return true;
		}
	}
});
JS;
		//fix for the problem with more than one form on a page.
		Requirements::customScript($jsFunc, 'func_validateEmailAreaField' . '_' . $formID);

		return <<<JS
if(typeof fromAnOnBlur != 'undefined'){
	if(fromAnOnBlur.name == '$this->name')
		$('$formID').validateEmailAreaField('$this->name');
}else{
	$('$formID').validateEmailAreaField('$this->name');
}
JS;
  }
}
?>

Server validation works fine. However it seems that nobody cares about my jsValidation() method. If I just use EmailField in my form instead of this class - it works. But as soon as I switch to my class, it suddently stops working even if I do this!

<?php

/**
 * Textarea that only allows emails separated by commas.
 *
 * @author Artūras 'arturaz' Šlajus <x11@arturaz.net>
 */
class EmailAreaField extends EmailField {
  public function jsValidation() {
    return parent::jsValidation();
  }
}
?>

Any ideas what's happening? :/

Avatar
arturaz

Community Member, 2 Posts

30 November 2009 at 10:59am

Also it seems that:

<?php

/**
 * Textarea that only allows emails separated by commas.
 *
 * @author Artūras 'arturaz' Šlajus <x11@arturaz.net>
 */
class EmailAreaField extends EmailField {
  const ERROR_MESSAGE = "One of the emails in the field is invalid.";
  const EMAIL_REGEXP = '/^.+@.+\.[a-z]+$/i';

  public function validate() {
    foreach (split(",", $this->value) as $email) {
      $email = trim($email);

      if (! preg_match(self::EMAIL_REGEXP, $email)) {
        $validator->validationError(
          $this->name,
          self::ERROR_MESSAGE,
          "validation"
        );

        return false;
      }
    }

    return true;
  }

  public function jsValidation() {
    return parent::jsValidation();
  }
}
?>

fails, but

<?php

/**
 * Textarea that only allows emails separated by commas.
 *
 * @author Artūras 'arturaz' Šlajus <x11@arturaz.net>
 */
class EmailAreaField extends EmailField {
  const ERROR_MESSAGE = "One of the emails in the field is invalid.";
  const EMAIL_REGEXP = '/^.+@.+\.[a-z]+$/i';

  public function jsValidation() {
    return parent::jsValidation();
  }
}
?>

Uses EmailField JS validation logic. So removing server-side validator somehow affects my client-side validator?