diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index a05abdb12ec..368900008e7 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -8,6 +8,7 @@ ### Changed - Craft no longer shows the status menu for element sources that define a status. ([#4249](https://github.com/craftcms/cms/issues/4249)) - Element URI formats can now conditionally output an empty string, opting the element out of getting its own system URI. ([#4254](https://github.com/craftcms/cms/issues/4254)) +- Table fields now get validation errors if any column handles are entered in the format of “colX”. ### Fixed - Fixed a bug where rebuilding the project config could set an incorrect value for the user field layout. @@ -18,6 +19,7 @@ - Fixed a bug that could occur when Craft generated URLs with multi-byte characters in the query string. - Fixed a bug where you could get some character encoding issues in some environments when using PHP 7.3. - Fixed a bug where Craft wasn’t attempting to set a unique URI on duplicated elements. ([#4253](https://github.com/craftcms/cms/issues/4253)) +- Fixed a bug where Table fields could copy cell values to other cells if a column had a handle in the format of “colX”. ([#4200](https://github.com/craftcms/cms/issues/4200)) ## 3.1.26 - 2019-05-08 diff --git a/src/fields/Table.php b/src/fields/Table.php index cf8e416a7f2..059104bdc70 100644 --- a/src/fields/Table.php +++ b/src/fields/Table.php @@ -125,9 +125,32 @@ public function rules() $rules[] = [['minRows'], 'compare', 'compareAttribute' => 'maxRows', 'operator' => '<=', 'type' => 'number', 'when' => [$this, 'hasMaxRows']]; $rules[] = [['maxRows'], 'compare', 'compareAttribute' => 'minRows', 'operator' => '>=', 'type' => 'number', 'when' => [$this, 'hasMinRows']]; $rules[] = [['minRows', 'maxRows'], 'integer', 'min' => 0]; + $rules[] = [['columns'], 'validateColumns']; return $rules; } + /** + * Validatse the column configs. + */ + public function validateColumns() + { + $hasErrors = false; + foreach ($this->columns as &$col) { + if ($col['handle'] && preg_match('/^col\d+$/', $col['handle'])) { + $col['handle'] = [ + 'value' => $col['handle'], + 'hasErrors' => true, + ]; + $hasErrors = true; + } + } + if ($hasErrors) { + $this->addError('columns', Craft::t('app', 'Column handles can’t be in the format “{format}”.', [ + 'format' => 'colX', + ])); + } + } + /** * @return bool whether minRows was set */ @@ -217,7 +240,8 @@ public function getSettingsHtml() 'cols' => $columnSettings, 'rows' => $this->columns, 'addRowLabel' => Craft::t('app', 'Add a column'), - 'initJs' => false + 'initJs' => false, + 'errors' => $this->getErrors('columns'), ] ]); @@ -297,7 +321,7 @@ public function normalizeValue($value, ElementInterface $element = null) foreach ($value as &$row) { foreach ($this->columns as $colId => $col) { $row[$colId] = $this->_normalizeCellValue($col['type'], $row[$colId] ?? null); - if ($col['handle']) { + if ($col['handle'] && !isset($this->columns[$col['handle']])) { $row[$col['handle']] = $row[$colId]; } }