Skip to content

Commit

Permalink
Merge branch 'craft-4' of https://github.com/verbb/tablemaker into cr…
Browse files Browse the repository at this point in the history
…aft-5

# Conflicts:
#	CHANGELOG.md
#	composer.json
#	src/fields/TableMakerField.php
  • Loading branch information
engram-design committed Aug 11, 2024
2 parents 3242ffa + 1de0099 commit 01558a4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
### Fixed
- Fix an error in Craft 4.6.0 where dropdown column options weren’t saving correctly.

## 4.0.11 - 2024-04-29

### Changed
- Update English translations.

### Fixed
- Fix an error when initializing the field in some instances.

## 4.0.10 - 2024-03-26

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Table Maker is licensed under the MIT license, meaning it will always be free an
<h2></h2>

<a href="https://verbb.io" target="_blank">
<img width="100" src="https://verbb.io/assets/img/verbb-pill.svg">
<img width="101" height="33" src="https://verbb.io/assets/img/verbb-pill.svg" alt="Verbb">
</a>
67 changes: 67 additions & 0 deletions src/fields/TableMakerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\helpers\Template;
use craft\validators\ColorValidator;
use craft\validators\HandleValidator;
use craft\validators\UrlValidator;
use craft\web\assets\tablesettings\TableSettingsAsset;

use yii\db\Schema;
Expand Down Expand Up @@ -179,6 +182,33 @@ public function serializeValue(mixed $value, ElementInterface $element = null):
return parent::serializeValue($value, $element);
}

public function getElementValidationRules(): array
{
return ['validateTableData'];
}

public function validateTableData(ElementInterface $element): void
{
$value = $element->getFieldValue($this->handle);
$rows = $value['rows'] ?? [];
$columns = $value['columns'] ?? [];

if (!empty($rows) && !empty($columns)) {
foreach ($rows as &$row) {
foreach ($columns as $colId => $col) {
if (is_string($row[$colId])) {
// Trim the value before validating
$row[$colId] = trim($row[$colId]);
}

if (!$this->_validateCellValue($col['type'], $row[$colId], $error)) {
$element->addError($this->handle, $error);
}
}
}
}
}

public function getSettingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('tablemaker/_field/settings', [
Expand All @@ -194,6 +224,7 @@ public function getContentGqlType(): Type|array
$columnType = GqlEntityRegistry::getEntity($typeName) ?: GqlEntityRegistry::createEntity($columnTypeName, new ObjectType([
'name' => $columnTypeName,
'fields' => [
'type' => Type::string(),
'heading' => Type::string(),
'width' => Type::string(),
'align' => Type::string(),
Expand Down Expand Up @@ -264,6 +295,11 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, bool $inl
// get columns from db or fall back to default
if (!empty($value['columns'])) {
foreach ($value['columns'] as $key => $val) {
// Just in case there's invalid data
if (!isset($val['heading'])) {
continue;
}

$type = $val['type'] ?? 'singleline';

$columns['col' . $key] = [
Expand Down Expand Up @@ -438,4 +474,35 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, bool $inl

return $input . $columnsField . $rowsField;
}


// Private Methods
// =========================================================================

private function _validateCellValue(string $type, mixed $value, ?string &$error = null): bool
{
if ($value === null || $value === '') {
return true;
}

switch ($type) {
case 'color':
/** @var ColorData $value */
$value = $value->getHex();
$validator = new ColorValidator();
break;
case 'url':
$validator = new UrlValidator();
break;
case 'email':
$validator = new EmailValidator();
break;
default:
return true;
}

$validator->message = str_replace('{attribute}', '{value}', $validator->message);

return $validator->validate($value, $error);
}
}

0 comments on commit 01558a4

Please sign in to comment.