Validate only fields in form
[MAILPOET-1828]
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user