/** * Validate value * * @return $this * @throws \Magento\Framework\Exception\LocalizedException * if there is no field value, search value is empty or regular expression is not valid */ public function beforeSave() { $design = clone $this->_design; // For value validations $exceptions = $this->getValue(); foreach ($exceptions as $rowKey => $row) { if ($rowKey === '__empty') { continue; } // Validate that all values have come foreach (['search', 'value'] as $fieldName) { if (!isset($row[$fieldName])) { throw new \Magento\Framework\Exception\LocalizedException(__('Exception does not contain field \'%1\'', $fieldName)); } } // Empty string (match all) is not supported, because it means setting a default theme. Remove such entries. if (!strlen($row['search'])) { unset($exceptions[$rowKey]); continue; } // Validate the theme value $design->setDesignTheme($row['value'], \Magento\Framework\App\Area::AREA_FRONTEND); // Compose regular exception pattern $exceptions[$rowKey]['regexp'] = $this->_composeRegexp($row['search']); } $this->setValue($exceptions); return parent::beforeSave(); }
/** * {@inheritdoc} */ public function beforeSave() { $expressions = $this->getValue(); foreach ($expressions as $rowKey => $row) { if ($rowKey === '__empty') { continue; } foreach (['match_expr', 'replace_expr'] as $fieldName) { if (!isset($row[$fieldName])) { throw new \Exception(__('Expression does not contain field \'%1\'', $fieldName)); } } $expressions[$rowKey]['match_expr'] = $this->composeRegexp($row['match_expr']); $expressions[$rowKey]['replace_expr'] = $this->composeRegexp($row['replace_expr']); } $this->setValue($expressions); return parent::beforeSave(); }