Email "list" validatator

Email "list" validatator

stpstp Posts: 6Questions: 2Answers: 0

Hey

First of all, thank you for the excellent program for the datatables editor. I love this program and it is about to bring more kick to my programming.

My problem is checking the multiple email address format.
I have New Email form to put emails-addresses to multiple recipients, like CC and BCC.

I use the following validation code...

Field::inst( 'email' )
->validator( Validate::email(
ValidateOptions::inst()
->allowEmpty( false )
->optional( false )
) );

But I need checking for multiple email addresses validation so array(), and this validation check only one address.

So i need to check list, like: name1@company.fi; name2@company.fi; name3@company.fi; name4@company.fi;

I use datatables editor DataTable --> Create New entry

Thanks and sorry my poor english language...

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    What you would need to do is create a custom validator. It would split the string on the semi colon, and then use PHP's built in e-mail validation to check that each one is a valid address (in a for loop).

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited September 2018

    Thanks for this!

    I like examples. So here is one implementing Allan's suggestion. The user may enter either nothing or a comma / semi colon separated list (for the Germans) of email addresses. I only save a comma separated list etc.

    outside of the Editor instance: error messages:

    if ($lang === 'de') {     
        $msg[0] = 'Feld darf nicht leer sein.';
        $msg[1] = 'Bitte geben Sie eine Zahl ein.';
        $msg[2] = 'Bitte nur Pdf, MS Office oder Open Office Dokumente hochladen.';
        $msg[3] = 'Dateien müssen kleiner als 50MB sein.';
        $msg[4] = 'Bitte wählen Sie mindestens eine Abteilung aus.';
        $msg[5] = 'Bitte geben Sie eine oder mehrere gültige E-Mail Adressen ein.'
                . 'Trennen Sie mehrere Adressen mittels Komma oder Semikolon.';
    
    } else {
        $msg[0] = 'Field may not be empty.';
        $msg[1] = 'Please enter a number.';
        $msg[2] = 'Please upload Pdf, MS Office or Open Office documents.';
        $msg[3] = 'Files must be smaller than 50MB.';
        $msg[4] = 'Please select at least one department.';
        $msg[5] = 'Please enter one or more valid email address.'
                . 'Separate multiple addresses with commas.'; 
    }
    

    The Field instance with validator and set formatter:

    Field::inst( 'report.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[5];
                }
            }
            return true;
        } )
        ->setFormatter( function($val, $data, $opts) {
            if ( is_null($val)  || $val <= '' ) {
                return null;
            }
            $emails = str_replace(";", ",", $val);
            $emails = str_replace(" ", "", $emails);
            $emails = str_replace(",", ", ", $emails);
            return mb_strtolower($emails);
        }),
    
This discussion has been closed.