Validate only fields in form
[MAILPOET-1828]
This commit is contained in:
@ -161,7 +161,7 @@ class Subscribers extends APIEndpoint {
|
|||||||
$data = $this->deobfuscateFormPayload($data);
|
$data = $this->deobfuscateFormPayload($data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->required_custom_field_validator->validate($data);
|
$this->required_custom_field_validator->validate($data, $form);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]);
|
return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Subscribers;
|
namespace MailPoet\Subscribers;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use MailPoet\Models\CustomField;
|
use MailPoet\Models\CustomField;
|
||||||
|
use MailPoet\Models\Form;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class RequiredCustomFieldValidator {
|
class RequiredCustomFieldValidator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
* @param Form|null $form
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function validate(array $data) {
|
public function validate(array $data, Form $form = null) {
|
||||||
$all_custom_fields = $this->getCustomFields();
|
$all_custom_fields = $this->getCustomFields($form);
|
||||||
foreach ($all_custom_fields as $custom_field_id => $custom_field_name) {
|
foreach ($all_custom_fields as $custom_field_id => $custom_field_name) {
|
||||||
if ($this->isCustomFieldMissing($custom_field_id, $data)) {
|
if ($this->isCustomFieldMissing($custom_field_id, $data)) {
|
||||||
throw new \Exception(
|
throw new Exception(
|
||||||
WPFunctions::get()->__(sprintf('Missing value for custom field "%s"', $custom_field_name), 'mailpoet')
|
WPFunctions::get()->__(sprintf('Missing value for custom field "%s"', $custom_field_name), 'mailpoet')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -35,10 +38,18 @@ class RequiredCustomFieldValidator {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCustomFields() {
|
private function getCustomFields(Form $form = null) {
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
$required_custom_fields = CustomField::findMany();
|
if ($form) {
|
||||||
|
$ids = $this->getFormCustomFieldIds($form);
|
||||||
|
if (!$ids) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$required_custom_fields = CustomField::whereIn('id', $ids)->findMany();
|
||||||
|
} else {
|
||||||
|
$required_custom_fields = CustomField::findMany();
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($required_custom_fields as $custom_field) {
|
foreach ($required_custom_fields as $custom_field) {
|
||||||
if (is_serialized($custom_field->params)) {
|
if (is_serialized($custom_field->params)) {
|
||||||
@ -52,4 +63,15 @@ class RequiredCustomFieldValidator {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getFormCustomFieldIds(Form $form) {
|
||||||
|
$form_fields = $form->getFieldList();
|
||||||
|
$custom_field_ids = [];
|
||||||
|
foreach ($form_fields as $field_name) {
|
||||||
|
if (strpos($field_name, 'cf_') === 0) {
|
||||||
|
$custom_field_ids[] = (int)substr($field_name, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $custom_field_ids;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -468,14 +468,26 @@ class SubscribersTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItCannotSubscribeWithoutMandatoryCustomField() {
|
function testItCannotSubscribeWithoutMandatoryCustomField() {
|
||||||
CustomField::createOrUpdate([
|
$custom_field = CustomField::createOrUpdate([
|
||||||
'name' => 'custom field',
|
'name' => 'custom field',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
'params' => ['required' => '1']
|
'params' => ['required' => '1']
|
||||||
]);
|
]);
|
||||||
|
$form = Form::createOrUpdate([
|
||||||
|
'name' => 'form',
|
||||||
|
'body' => [[
|
||||||
|
'type' => 'text',
|
||||||
|
'name' => 'mandatory',
|
||||||
|
'id' => $custom_field->id(),
|
||||||
|
'unique' => '1',
|
||||||
|
'static' => '0',
|
||||||
|
'params' => ['required' => '1'],
|
||||||
|
'position' => '0',
|
||||||
|
]],
|
||||||
|
]);
|
||||||
$response = $this->endpoint->subscribe(array(
|
$response = $this->endpoint->subscribe(array(
|
||||||
$this->obfuscatedEmail => 'toto@mailpoet.com',
|
$this->obfuscatedEmail => 'toto@mailpoet.com',
|
||||||
'form_id' => $this->form->id,
|
'form_id' => $form->id,
|
||||||
$this->obfuscatedSegments => array($this->segment_1->id, $this->segment_2->id)
|
$this->obfuscatedSegments => array($this->segment_1->id, $this->segment_2->id)
|
||||||
));
|
));
|
||||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace MailPoet\Subscribers;
|
namespace MailPoet\Subscribers;
|
||||||
|
|
||||||
use MailPoet\Models\CustomField;
|
use MailPoet\Models\CustomField;
|
||||||
|
use MailPoet\Models\Form;
|
||||||
|
|
||||||
class RequiredCustomFieldValidatorTest extends \MailPoetTest {
|
class RequiredCustomFieldValidatorTest extends \MailPoetTest {
|
||||||
|
|
||||||
@ -46,4 +47,26 @@ class RequiredCustomFieldValidatorTest extends \MailPoetTest {
|
|||||||
$validator->validate(['cf_' . $this->custom_field->id() => '']);
|
$validator->validate(['cf_' . $this->custom_field->id() => '']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItValidatesOnlyFieldPresentInForm() {
|
||||||
|
CustomField::createOrUpdate([
|
||||||
|
'name' => 'custom field 2',
|
||||||
|
'type' => 'text',
|
||||||
|
'params' => ['required' => '1']
|
||||||
|
]);
|
||||||
|
$form = Form::createOrUpdate([
|
||||||
|
'name' => 'form',
|
||||||
|
'body' => [[
|
||||||
|
'type' => 'text',
|
||||||
|
'name' => 'mandatory',
|
||||||
|
'id' => $this->custom_field->id(),
|
||||||
|
'unique' => '1',
|
||||||
|
'static' => '0',
|
||||||
|
'params' => ['required' => '1'],
|
||||||
|
'position' => '0',
|
||||||
|
]],
|
||||||
|
]);
|
||||||
|
$validator = new RequiredCustomFieldValidator();
|
||||||
|
$validator->validate(['cf_' . $this->custom_field->id() => 'value'], $form);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user