messageManager = $messageManager; $this->escaper = $escaper; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * Validate ip addresses before save * * @return $this */ public function beforeSave() { $allowedIpsRaw = $this->escaper->escapeHtml($this->getValue()); $noticeMsgArray = []; $allowedIpsArray = []; if (empty($allowedIpsRaw)) { return parent::beforeSave(); } $dataArray = explode(',', $allowedIpsRaw); foreach ($dataArray as $data) { if (filter_var(trim($data), FILTER_VALIDATE_IP)) { $allowedIpsArray[] = $data; } else { $noticeMsgArray[] = $data; } } $noticeMsg = implode(',', $noticeMsgArray); if (!empty($noticeMsgArray)) { $this->messageManager->addNotice( __( __('The following invalid values cannot be saved: %values', ['values' => $noticeMsg]) ) ); } $this->setValue(implode(',', $allowedIpsArray)); return parent::beforeSave(); } }