Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Impossible to create custom JS validation on field?

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

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

Page: 1
Go to End
Author Topic: 1147 Views
  • arturaz
    Avatar
    Community Member
    2 Posts

    Impossible to create custom JS validation on field? Link to this post

    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? :/

  • arturaz
    Avatar
    Community Member
    2 Posts

    Re: Impossible to create custom JS validation on field? Link to this post

    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?

    1147 Views
Page: 1
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.