FOF30\Model\DataModel::check PHP Метод

check() публичный Метод

Check the data for validity. By default it only checks for fields declared as NOT NULL
public check ( ) : static
Результат static Self, for chaining
    public function check()
    {
        if (!$this->autoChecks) {
            return $this;
        }
        // Run a custom event
        $this->triggerEvent('onBeforeCheck');
        // Create a slug if there is a title and an empty slug
        $slugField = $this->getFieldAlias('slug');
        $titleField = $this->getFieldAlias('title');
        if ($this->hasField('title') && $this->hasField('slug') && !$this->{$slugField}) {
            $this->{$slugField} = \JApplicationHelper::stringURLSafe($this->{$titleField});
        }
        // Special handling of the ordering field
        if ($this->hasField('ordering') && is_null($this->getFieldValue('ordering'))) {
            $this->setFieldValue('ordering', 0);
        }
        foreach ($this->knownFields as $fieldName => $field) {
            // Never check the key if it's empty; an empty key is normal for new records
            if ($fieldName == $this->idFieldName) {
                continue;
            }
            $value = $this->{$fieldName};
            if ($field->Null == 'NO' && empty($value) && !is_numeric($value) && !in_array($fieldName, $this->fieldsSkipChecks)) {
                if (!is_null($field->Default)) {
                    $this->{$fieldName} = $field->Default;
                    continue;
                }
                $text = $this->container->componentName . '_' . $this->container->inflector->singularize($this->getName()) . '_ERR_' . $fieldName . '_EMPTY';
                throw new \RuntimeException(\JText::_(strtoupper($text)), 500);
            }
        }
        // Server-side form validation
        $allData = $this->getData();
        $form = $this->getForm($allData, false);
        if (is_object($form) && $form instanceof Form) {
            $serverside_validate = strtolower($form->getAttribute('serverside_validate'));
            if (in_array($serverside_validate, array('true', 'yes', '1', 'on'))) {
                $fieldset = $form->getFieldset();
                foreach ($fieldset as $nfield => $fldset) {
                    if (!array_key_exists($nfield, $allData)) {
                        $field = $form->getField($fldset->fieldname, $fldset->group);
                        $type = strtolower($field->type);
                        switch ($type) {
                            case 'checkbox':
                                $allData[$nfield] = 0;
                                break;
                            default:
                                $allData[$nfield] = '';
                                break;
                        }
                    }
                }
                try {
                    $this->validateForm($form, $allData);
                } catch (\Exception $e) {
                    throw new \RuntimeException($e->getMessage(), $e->getCode());
                }
            }
        }
        return $this;
    }

Usage Example

Пример #1
0
 public function check()
 {
     if (empty($this->published)) {
         $this->published = 0;
     }
     return parent::check();
 }
All Usage Examples Of FOF30\Model\DataModel::check