Adds min/max email length in UI and backend
This commit is contained in:
@ -1,7 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
use MailPoet\Form\Util\FieldNameObfuscator;
|
use MailPoet\Form\Util\FieldNameObfuscator;
|
||||||
|
use MailPoet\Models\ModelValidator;
|
||||||
|
|
||||||
abstract class Base {
|
abstract class Base {
|
||||||
protected static function getInputValidation($block, $extra_rules = array()) {
|
protected static function getInputValidation($block, $extra_rules = array()) {
|
||||||
@ -9,6 +11,8 @@ abstract class Base {
|
|||||||
|
|
||||||
if($block['id'] === 'email') {
|
if($block['id'] === 'email') {
|
||||||
$rules['required'] = true;
|
$rules['required'] = true;
|
||||||
|
$rules['minlength'] = ModelValidator::EMAIL_MIN_LENGTH;
|
||||||
|
$rules['maxlength'] = ModelValidator::EMAIL_MAX_LENGTH;
|
||||||
$rules['error-message'] = __('Please specify a valid email address.', 'mailpoet');
|
$rules['error-message'] = __('Please specify a valid email address.', 'mailpoet');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,4 +139,4 @@ abstract class Base {
|
|||||||
}
|
}
|
||||||
return join(' ', $modifiers);
|
return join(' ', $modifiers);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,9 @@ if(!defined('ABSPATH')) exit;
|
|||||||
class ModelValidator extends \Sudzy\Engine {
|
class ModelValidator extends \Sudzy\Engine {
|
||||||
public $validators;
|
public $validators;
|
||||||
|
|
||||||
|
const EMAIL_MIN_LENGTH = 6;
|
||||||
|
const EMAIL_MAX_LENGTH = 150;
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->validators = array(
|
$this->validators = array(
|
||||||
@ -26,7 +29,9 @@ class ModelValidator extends \Sudzy\Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function validateEmail($email) {
|
function validateEmail($email) {
|
||||||
return is_email($email) !== false;
|
$permitted_length = (strlen($email) >= self::EMAIL_MIN_LENGTH && strlen($email) <= self::EMAIL_MAX_LENGTH);
|
||||||
|
$valid_email = (is_email($email) !== false);
|
||||||
|
return ($permitted_length && $valid_email);
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateRenderedNewsletterBody($newsletter_body) {
|
function validateRenderedNewsletterBody($newsletter_body) {
|
||||||
|
@ -21,8 +21,9 @@ class ModelValidatorTest extends \MailPoetTest {
|
|||||||
function testItValidatesEmail() {
|
function testItValidatesEmail() {
|
||||||
expect($this->validator->validateEmail('test'))->false();
|
expect($this->validator->validateEmail('test'))->false();
|
||||||
expect($this->validator->validateEmail('tést@éxample.com'))->false();
|
expect($this->validator->validateEmail('tést@éxample.com'))->false();
|
||||||
|
|
||||||
expect($this->validator->validateEmail('test@example.com'))->true();
|
expect($this->validator->validateEmail('test@example.com'))->true();
|
||||||
|
expect($this->validator->validateEmail('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong_email@example.com'))->false();
|
||||||
|
expect($this->validator->validateEmail('a@b.c'))->false();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItValidatesRenderedNewsletterBody() {
|
function testItValidatesRenderedNewsletterBody() {
|
||||||
@ -30,7 +31,7 @@ class ModelValidatorTest extends \MailPoetTest {
|
|||||||
expect($this->validator->validateRenderedNewsletterBody(serialize('test')))->false();
|
expect($this->validator->validateRenderedNewsletterBody(serialize('test')))->false();
|
||||||
expect($this->validator->validateRenderedNewsletterBody(array('html' => 'test', 'text' => null)))->false();
|
expect($this->validator->validateRenderedNewsletterBody(array('html' => 'test', 'text' => null)))->false();
|
||||||
expect($this->validator->validateRenderedNewsletterBody(array('html' => null, 'text' => 'test')))->false();
|
expect($this->validator->validateRenderedNewsletterBody(array('html' => null, 'text' => 'test')))->false();
|
||||||
|
|
||||||
expect($this->validator->validateRenderedNewsletterBody(null))->true();
|
expect($this->validator->validateRenderedNewsletterBody(null))->true();
|
||||||
expect($this->validator->validateRenderedNewsletterBody(serialize(null)))->true();
|
expect($this->validator->validateRenderedNewsletterBody(serialize(null)))->true();
|
||||||
expect($this->validator->validateRenderedNewsletterBody(serialize(array('html' => 'test', 'text' => 'test'))))->true();
|
expect($this->validator->validateRenderedNewsletterBody(serialize(array('html' => 'test', 'text' => 'test'))))->true();
|
||||||
|
Reference in New Issue
Block a user