Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Framework\Validator\NotEmptyFactory;
12 
13 class Validator extends \Magento\Framework\Validator\AbstractValidator
14 {
18  private $notEmpty;
19 
23  public function __construct(NotEmptyFactory $notEmptyFactory)
24  {
25  $this->notEmpty = $notEmptyFactory->create(['options' => NotEmpty::ALL]);
26  }
27 
33  public function isValid($value)
34  {
36 
37  return !$this->hasMessages();
38  }
39 
46  protected function validateRequiredFields($value)
47  {
48  $messages = [];
49  $requiredFields = [
50  'title' => $value->getTitle(),
51  'type' => $value->getType()
52  ];
53  foreach ($requiredFields as $requiredField => $requiredValue) {
54  if (!$this->notEmpty->isValid(trim($requiredValue))) {
55  $messages[$requiredField] =
56  __('"%fieldName" is required. Enter and try again.', ['fieldName' => $requiredField]);
57  }
58  }
59  $this->_addMessages($messages);
60  }
61 }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(NotEmptyFactory $notEmptyFactory)
Definition: Validator.php:23