Validate only fields in form

[MAILPOET-1828]
This commit is contained in:
Pavel Dohnal
2019-04-03 13:12:47 +02:00
committed by M. Shull
parent 66663331c5
commit 6493f7ceb4
4 changed files with 66 additions and 9 deletions

View File

@ -161,7 +161,7 @@ class Subscribers extends APIEndpoint {
$data = $this->deobfuscateFormPayload($data);
try {
$this->required_custom_field_validator->validate($data);
$this->required_custom_field_validator->validate($data, $form);
} catch (\Exception $e) {
return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]);
}

View File

@ -1,21 +1,24 @@
<?php
namespace MailPoet\Subscribers;
use Exception;
use MailPoet\Models\CustomField;
use MailPoet\Models\Form;
use MailPoet\WP\Functions as WPFunctions;
class RequiredCustomFieldValidator {
/**
* @param array $data
* @param Form|null $form
*
* @throws \Exception
* @throws Exception
*/
public function validate(array $data) {
$all_custom_fields = $this->getCustomFields();
public function validate(array $data, Form $form = null) {
$all_custom_fields = $this->getCustomFields($form);
foreach ($all_custom_fields as $custom_field_id => $custom_field_name) {
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')
);
}
@ -35,10 +38,18 @@ class RequiredCustomFieldValidator {
return false;
}
private function getCustomFields() {
private function getCustomFields(Form $form = null) {
$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) {
if (is_serialized($custom_field->params)) {
@ -52,4 +63,15 @@ class RequiredCustomFieldValidator {
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;
}
}

View File

@ -468,14 +468,26 @@ class SubscribersTest extends \MailPoetTest {
}
function testItCannotSubscribeWithoutMandatoryCustomField() {
CustomField::createOrUpdate([
$custom_field = CustomField::createOrUpdate([
'name' => 'custom field',
'type' => 'text',
'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(
$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)
));
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);

View File

@ -3,6 +3,7 @@
namespace MailPoet\Subscribers;
use MailPoet\Models\CustomField;
use MailPoet\Models\Form;
class RequiredCustomFieldValidatorTest extends \MailPoetTest {
@ -46,4 +47,26 @@ class RequiredCustomFieldValidatorTest extends \MailPoetTest {
$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);
}
}