Juergen Posted July 20, 2017 Share Posted July 20, 2017 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 More sharing options...
LostKobrakai Posted July 20, 2017 Share Posted July 20, 2017 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? 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 20, 2017 Author Share Posted July 20, 2017 Thanks @LostKobrakai, I will try it like this and post my result here. Link to comment Share on other sites More sharing options...
Juergen Posted July 20, 2017 Author Share Posted July 20, 2017 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 More sharing options...
LostKobrakai Posted July 20, 2017 Share Posted July 20, 2017 Did you try to look, what $params does give you? Link to comment Share on other sites More sharing options...
Juergen Posted July 20, 2017 Author Share Posted July 20, 2017 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 More sharing options...
Recommended Posts