Editor 1.9 - email validation not working

Editor 1.9 - email validation not working

rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
edited August 2019 in Free community support

This built in Editor validator checks the validity of an email address but it does not check if the email field is filled at all. Looks like a bug. It should produce an error message for empty email fields:

Field::inst( 'user.email' )
    ->validator( 'Validate::email', array( 'required' => true,
                                           'message' => $msg[1]) ),

I replaced it with a custom PHP validator and now it works

Field::inst( 'user.email' )
    ->validator( function ( $val, $data, $opts ) use ( $msg ) {            
        if ( ! filter_var(trim($val), FILTER_VALIDATE_EMAIL) ) {
            return $msg[1];
        }
        return true;
    } ),

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586
    Answer ✓

    Hi @rf1234 ,

    Yep, agreed. I've raised it internally (DD-1021 for my reference) and we'll report back here when there's an update.

    Cheers,

    Colin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Thanks Colin!

    I see I used an older version of the built in validator anyway. The current version will probably work?!

    Quick question:
    I built this to validate a string of comma or semi-colon separated email addresses and save it as a comma separated string of email addresses in a text field. Is there a built-in Editor validator for this as well?

    if ($lang === 'de') {     
        $msg[2] = 'Bitte geben Sie eine oder mehrere gültige E-Mail Adressen ein.'
                . 'Trennen Sie mehrere Adressen mittels Komma oder Semikolon.';
    } else {
        $msg[2] = 'Please enter one or more valid email address.'
                . 'Separate multiple addresses with commas.'; 
    }
    
    Field::inst( 'ctr_event.email_addresses' )
        ->validator( function ( $val, $data, $opts ) use ( $msg ) {
            if ( is_null($val)  || $val <= '' ) {
                return true;
            }
            $emailArray = explode(',', $val);
            if ( count($emailArray) <= 1 ) {
               $emailArray = explode(';', $val); 
            }
            foreach ( $emailArray as $val ) {
                if ( ! filter_var(trim($val), FILTER_VALIDATE_EMAIL) ) {
                    return $msg[2];
                }
            }
            return true;
        } )
        ->setFormatter( function($val, $data, $opts) {
            if ( is_null($val)  || $val <= '' ) {
                return '';
            }
            $emails = str_replace(";", ",", $val);
            $emails = str_replace(" ", "", $emails);
            $emails = str_replace(",", ", ", $emails);
            return mb_strtolower($emails);
        }),
    
    
    
  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin
    Answer ✓

    Yes, I believe it should work with the latest libraries. I've just tried it and it appears to work as expected. I can't find the commit that fixed it is the only thing, but then the PHP libraries were split into a public Github repo with the 1.8 release. What version were you using?

    Is there a built-in Editor validator for this as well?

    No. A custom validator would be the way to do that.

    Allan

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    I use Editor 1.9 Javascript and PHP libraries downloaded from your site.

This discussion has been closed.