Skip to content

Commit

Permalink
Don't allow "colX" Table column handles
Browse files Browse the repository at this point in the history
Resolves #4200
  • Loading branch information
brandonkelly committed May 13, 2019
1 parent 4a6418e commit 8f3dfa1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
28 changes: 26 additions & 2 deletions src/fields/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'),
]
]);

Expand Down Expand Up @@ -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];
}
}
Expand Down

0 comments on commit 8f3dfa1

Please sign in to comment.