public function __construct($metadata, $model, $prefix = '', $prepopulate = array(), $exclude = array(), $disable_groups = false, $disable_widgets = false, $extra_settings = null) { $class_name = $metadata->name; $model_id = $model->id; $this->table_name = $metadata->table['name']; $this->prepopulate = \Arr::merge(\Input::get(), $prepopulate); $this->exclude = $exclude; $this->disable_groups = $disable_groups; $this->disable_widgets = $disable_widgets; $this->title = $model_id && method_exists($model, 'getFormTitle') ? $model->getFormTitle() : $class_name::singular(); if (\Input::param('alias', false) !== false) { $this->icon = 'link'; $this->plural = 'Links'; $this->singular = 'Link'; } else { $this->icon = $class_name::icon(); $this->plural = $class_name::plural(); $this->singular = $class_name::singular(); } // Tabs, Groups, Fields $this->tabs = $class_name::tabs(); $this->groups = $class_name::groups(); $this->default_tab = $class_name::defaultTab(); $this->default_group = $class_name::defaultGroup(); // Merge in extra field settings $this->fields = \Admin::getFieldSettings($class_name); if ($extra_settings !== null && is_array($extra_settings)) { $this->fields = \Arr::merge($this->fields, $extra_settings); } $this->validator_meta = \D::validator()->getMetadataFactory()->getMetadataFor($class_name); // Merge any DB settings into the mix... $model_settings = $model->settings; if (is_array($model_settings)) { $_model_settings = array(); foreach ($model_settings as $key => $value) { if (is_array($value) && ($metadata->hasField($key) || $metadata->hasAssociation($key))) { $_model_settings[$key] = $value; } } $this->fields = \Arr::merge($this->fields, $_model_settings); } // The field data $this->processFieldSettings($metadata, $model, $prefix); // The group data $this->processGroups(); // The form structure $this->processFormStructure(); $this->assets['js'] = array_unique($this->assets['js']); $this->assets['css'] = array_unique($this->assets['css']); }
/** * Validates the model's properties and association with the Symfony Validator. * Stores any errors in the model's $errors property. * * @param array|null $groups The validator groups to use for validating * @param array|null $fields Field names to validate. All fields will be validated if omitted. * @param array|null $fields Field names to exclude from validation * * @return bool Whether the model has passed validation */ public function validate($groups = null, $fields = null, $exclude_fields = null, $exclude_types = null) { $metadata = $this->_metadata(); $result = \D::validator()->validate($this, $groups); $this->errors = array(); foreach ($result->getIterator() as $violation) { $prop = $violation->getPropertyPath(); if ($prop == 'id' || $fields !== null && !in_array($prop, $fields) || $exclude_fields !== null && in_array($prop, $exclude_fields)) { continue; } $msg = $violation->getMessage(); $this->addErrorForField($prop, $msg); } return count($this->errors) === 0; }