Generates validation rules for the specified table.
public generateRules ( yii\db\TableSchema $table ) : array | ||
$table | yii\db\TableSchema | the table schema |
Résultat | array | the generated validation rules |
/** * @inheritdoc */ public function generateRules($table) { $rules = parent::generateRules($table); if ($this->includeTimestampBehavior) { // Assume same datatype for created_at/updated_at columns $this->_useDatetimeValue = isset($table->columns[$this->createdColumnName]) && $table->columns[$this->createdColumnName]->type === Schema::TYPE_DATETIME; foreach ($rules as $i => $rule) { if (strpos($rule, "'{$this->createdColumnName}'") === false && strpos($rule, "'{$this->updatedColumnName}'") === false || strpos($rule, "'exist', " !== false)) { continue; } list($ruleFields, $ruleName) = eval("return {$rule};"); if (($key = array_search($this->createdColumnName, $ruleFields)) !== false) { unset($ruleFields[$key]); } if (($key = array_search($this->updatedColumnName, $ruleFields)) !== false) { unset($ruleFields[$key]); } if (empty($ruleFields)) { unset($rules[$i]); } else { $newRuleFields = "['" . implode("', '", $ruleFields) . "']"; $rules[$i] = preg_replace('#^\\[\\[[^\\]]+\\]#', '[' . $newRuleFields, $rule); } } } return $rules; }
public function generateRules($table) { $rules = parent::generateRules($table); foreach ($rules as &$r) { $r = str_replace(' => ', '=>', $r); $posEndAttr = strpos($r, ']', 2); $attrList = substr($r, 2, $posEndAttr); $r = substr($r, 0, 2) . str_replace(', ', ',', $attrList) . substr($r, $posEndAttr + 2); } return $rules; }
/** * Generates validation rules for the specified table and add enum value validation. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $rules = []; //for enum fields create rules "in range" for all enum values $enum = $this->getEnum($table->columns); foreach ($enum as $field_name => $field_details) { $ea = array(); foreach ($field_details['values'] as $field_enum_values) { $ea[] = 'self::' . $field_enum_values['const_name']; } $rules[] = "['" . $field_name . "', 'in', 'range' => [\n " . implode(",\n ", $ea) . ",\n ]\n ]"; } return array_merge(parent::generateRules($table), $rules); }
/** Added by pafnow * Generates validation rules for the specified table. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $table_clone = clone $table; $types = []; foreach ($table_clone->columns as $key => $column) { $column_clone = clone $column; $table_clone->columns[$key] = $column_clone; $type_comment = Json::getValue($column->comment, "type", false); if (empty($type_comment)) { continue; } $types[$type_comment][] = $column->name; if (!$column->allowNull && $column->defaultValue === null) { $types['required'][] = $column->name; } //Remove the column from the table so it is not generated again by the parent function unset($table_clone->columns[$key]); } $rules = []; foreach ($types as $type => $columns) { $rules[] = "[['" . implode("', '", $columns) . "'], '{$type}']"; } return array_merge(parent::generateRules($table_clone), $rules); }
/** * Generates validation rules for the specified table and add enum value validation. * * @param \yii\db\TableSchema $table the table schema * * @return array the generated validation rules */ public function generateRules($table) { $columns = []; foreach ($table->columns as $index => $column) { $isBlameableCol = $column->name === $this->createdByColumn || $column->name === $this->updatedByColumn; $isTimestampCol = $column->name === $this->createdAtColumn || $column->name === $this->updatedAtColumn; $removeCol = $this->useBlameableBehavior && $isBlameableCol || $this->useTimestampBehavior && $isTimestampCol; if ($removeCol) { $columns[$index] = $column; unset($table->columns[$index]); } } $rules = []; //for enum fields create rules "in range" for all enum values $enum = $this->getEnum($table->columns); foreach ($enum as $field_name => $field_details) { $ea = array(); foreach ($field_details['values'] as $field_enum_values) { $ea[] = 'self::' . $field_enum_values['const_name']; } $rules[] = "['" . $field_name . "', 'in', 'range' => [\n " . implode(",\n ", $ea) . ",\n ]\n ]"; } // inject namespace for targetClass $parentRules = parent::generateRules($table); $ns = "\\{$this->ns}\\"; $match = "'targetClass' => "; $replace = $match . $ns; foreach ($parentRules as $k => $parentRule) { $parentRules[$k] = str_replace($match, $replace, $parentRule); } $rules = array_merge($parentRules, $rules); $table->columns = array_merge($table->columns, $columns); return $rules; }
public function generateRules($table) { $rules = parent::generateRules($table); if (isset($this->behaviorsType['phone']) && isset($this->behaviorsType['phone']['checked']) && $this->behaviorsType['phone']['checked']) { if ($this->behaviorType['phone']['phoneAttribute']) { $rules[] = "[['{$this->behaviorType['phone']['phoneAttribute']}'], 'borales\\extensions\\phoneInput\\PhoneInputValidator']"; } } return $rules; }
/** * Generates validation rules for the specified table. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $rules = parent::generateRules($table); foreach ($rules as $key => $rule) { $rules[$key] = preg_replace_callback("/[A-Za-z]+::className\\(\\)/", function ($matches) { $match = ArrayHelper::getValue($matches, 0); $class = ArrayHelper::getValue(explode('::', $match), 0); $fqn = ModelHelper::root($this->helperModel->getClass(ModelHelper::RK_MODEL_CM, $class)); if ($fqn) { return sprintf('%s::className()', $fqn); } return $match; }, $rule); } return $rules; }
/** * @inheritdoc * @param \yii\gii\plus\db\TableSchema $table */ public function generateRules($table) { $booleanAttributes = []; $integerAttributes = []; $uIntegerAttributes = []; $numberAttributes = []; $uNumberAttributes = []; $dateFormats = []; $matchPatterns = []; $defaultExpressions = []; $defaultValues = []; $defaultNullAttributes = []; foreach ($table->columns as $column) { if ($column->autoIncrement) { continue; } if ($column->getIsBoolean()) { $booleanAttributes[] = $column->name; } elseif ($column->getIsInteger()) { if ($column->unsigned) { $uIntegerAttributes[] = $column->name; } else { $integerAttributes[] = $column->name; } } elseif ($column->getIsNumber()) { if ($column->unsigned) { $uNumberAttributes[] = $column->name; } else { $numberAttributes[] = $column->name; } } if ($column->getHasDateFormat()) { $dateFormats[$column->getDateFormat()][] = $column->name; } if ($column->getHasPattern()) { $matchPatterns[$column->getPattern()][] = $column->name; } if (!is_null($column->defaultValue)) { if ($column->defaultValue instanceof Expression) { $this->relationUses[$table->fullName][] = 'yii\\db\\Expression'; $defaultExpressions[$column->defaultValue->expression][] = $column->name; } else { $defaultValues[$column->defaultValue][] = $column->name; } } elseif ($column->allowNull) { $defaultNullAttributes[] = $column->name; } } $rules = []; if (count($booleanAttributes)) { $rules[] = '[' . Helper::implode($booleanAttributes, 3) . ', \'filter\', \'filter\' => function ($value) {' . "\n" . ' return $value ? 1 : 0;' . "\n" . ' }, \'skipOnEmpty\' => true]'; $rules[] = '[' . Helper::implode($booleanAttributes, 3) . ', \'boolean\']'; } if (count($integerAttributes)) { $rules[] = '[' . Helper::implode($integerAttributes, 3) . ', \'integer\']'; } if (count($uIntegerAttributes)) { $rules[] = '[' . Helper::implode($uIntegerAttributes, 3) . ', \'integer\', \'min\' => 0]'; } if (count($numberAttributes)) { $rules[] = '[' . Helper::implode($numberAttributes, 3) . ', \'number\']'; } if (count($uNumberAttributes)) { $rules[] = '[' . Helper::implode($uNumberAttributes, 3) . ', \'number\', \'min\' => 0]'; } foreach ($dateFormats as $dateFormat => $attributes) { $rules[] = '[' . Helper::implode($attributes, 3) . ', \'filter\', \'filter\' => function ($value) { return is_int($value) ? date(\'' . substr($dateFormat, 4) . '\', $value) : $value; }]'; } foreach ($dateFormats as $dateFormat => $attributes) { $rules[] = '[' . Helper::implode($attributes, 3) . ', \'date\', \'format\' => \'' . $dateFormat . '\']'; } foreach ($matchPatterns as $matchPattern => $attributes) { $rules[] = '[' . Helper::implode($attributes, 3) . ', \'match\', \'pattern\' => \'' . $matchPattern . '\']'; } foreach (parent::generateRules($table) as $rule) { if (preg_match('~, \'(?:safe|boolean|integer|number)\'\\]$~', $rule)) { continue; } $rules[] = $rule; } foreach ($defaultExpressions as $defaultExpression => $attributes) { $rules[] = '[' . Helper::implode($attributes, 3) . ', \'default\', \'value\' => new Expression(\'' . $defaultExpression . '\')]'; } foreach ($defaultValues as $defaultValue => $attributes) { $rules[] = '[' . Helper::implode($attributes, 3) . ', \'default\', \'value\' => \'' . $defaultValue . '\']'; } if (count($defaultNullAttributes)) { $rules[] = '[' . Helper::implode($defaultNullAttributes, 3) . ', \'default\', \'value\' => null]'; } return $rules; }
public function generateRules($tableSchema) { $ts = $this->getTimestampFields($tableSchema); $table = $tableSchema; if ($ts) { $table = clone $tableSchema; foreach ($ts as $field) { unset($table->columns[$field]); } } $rules = parent::generateRules($table); $nulls = []; foreach ($table->columns as $column) { if ($column->allowNull && $column->defaultValue === null) { $nulls[] = $column->name; } } if ($nulls) { array_unshift($rules, "[['" . implode("', '", $nulls) . "'], 'trim']"); $rules[] = "[['" . implode("', '", $nulls) . "'], 'default', 'value' => null]"; } return $rules; }
/** * Generates validation rules for the specified table. * @param \yii\db\TableSchema $table the table schema * @return array the generated validation rules */ public function generateRules($table) { $types = []; $parentRules = parent::generateRules($table); // delete not required fields from required string. foreach ($parentRules as $i => $parentRule) { if (strpos($parentRule, "'required']") !== false) { foreach ($this->_notInsertOnRequired as $column) { $search1 = "'{$column}', "; $search2 = ", '{$column}'"; $search3 = "'{$column}'"; $parentRule = str_replace($search1, '', $parentRule); $parentRule = str_replace($search2, '', $parentRule); $parentRule = str_replace($search3, '', $parentRule); $parentRules[$i] = $parentRule; } } } foreach ($table->columns as $column) { /*TODO: (ruben) enhance the url string detection with regex (only when 'url' is the only word, or have an _ before or after the word.*/ if (strstr($column->name, 'url')) { $types['url'][] = $column->name; } } $rules = []; foreach ($types as $type => $columns) { $rules[] = "[['" . implode("', '", $columns) . "'], '{$type}']"; } $allRules = array_merge($parentRules, $rules); return $allRules; }