Jump to content

Mailchimp and Valitron custom rules question


Juergen
 Share

Recommended Posts

Hello @ all,

I am using Mailchimp V3 and Valitron as my validation library. I am struggeling to add a custom validation rule which checks if the entered email address is registered at my Mailchimp account.

I know that there is a documentation about how to add custom rules at the Valitron page, but its not very clear to me.

Does anyone have done something similar and can point me into the right direction.

For now I am testing the email address after Valitron has validated my inputs.

The following function checks if the entered email address is registered at Mailchimp.

                function isSubscribed($emailAddress, $listId, $apikey)
                {
                    $MailChimp      = new \DrewM\MailChimp\MailChimp($apikey);
                    $subscriberHash = $MailChimp->subscriberHash($emailAddress);
                    $result         = $MailChimp->get('lists/' . $listId . '/members/' . $subscriberHash);
                    return ($MailChimp->success() && isset($result['id']));
                }
                $isRegistered = isSubscribed($subscribenewsletteremailvalue, $listid, $apikey);

This function returns true if the email adress is registered and false if not (variable = $isRegistered).

How can I integrate this function inside a custom validation rule??

Best regards

Link to comment
Share on other sites

Valitron\Validator::addRule('notSubscribed', function($field, $value, array $params, array $fields) {
    return !isSubscribed($value, $params['listID'], wire('config')->mailchimp->apiKey)
}, 'Everything you do is wrong. You fail.');

Like this somewhere before your validations?

  • Like 1
Link to comment
Share on other sites

This works:

Valitron\Validator::addRule('notSubscribed', function($field, $value, $params) {
return !isSubscribed($value, "0*******f", "50****************-us14");
}, ': '._t('Your email address is already on the newsletter list. Therefore you don`t need to subscribe once more.', 'Newsletter'));


$v->rule('notSubscribed', 'nameofmyemailfield');

But........

It only works if the list ID and the API-key are hard coded.

I have tried to use params like this in the rule:

$v->rule('notSubscribed', 'nameofmyemailfield', ["listID" => "0*******f"]);

But they will not be fetched if I use

$params['listID']

  instead of the hard coded list id value.

PHP Notice: Undefined index: listID in
Link to comment
Share on other sites

Mmmhh: This works

$apikey              = "50******************-us14";
$listid            = "0**************f";

//1) Check Mailchimp if email is not registered
Valitron\Validator::addRule('notSubscribed', function($field, $value, $params = array()) {
    $MailChimp      = new \DrewM\MailChimp\MailChimp($params[0][1]);
    $subscriberHash = $MailChimp->subscriberHash($value);
    $result         = $MailChimp->get('lists/' . $params[0][0] . '/members/' . $subscriberHash);
    return !($MailChimp->success() && isset($result['id']));
}, ': '._t('Your email address is already on the newsletter list. Therefore you don`t need to subscribe once more.', 'Newsletter'));

And then you can validate the field

$v->rule('notSubscribed', 'nameofmyemailformfield', [$listid,$apikey]);

It seems that $params doesnt accept keys, only values so I use "$params[0][0]" and "$params[0][1]" for the list ID and the API key value.

I have also integrated the complete Mailchimp code instead of the function I have used before. Place this code in _init.php and the new validation rule can be used anywhere.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...