Skip to content

Commit

Permalink
Merge pull request #15760 from craftcms/bugfix/15746-sections-create-…
Browse files Browse the repository at this point in the history
…new-entry-type-validation

validate handle of the new entry type
  • Loading branch information
brandonkelly authored Sep 20, 2024
2 parents a6845a0 + b37df5a commit c7e4990
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fixed a bug where custom fields’ `required` properties were always `false`. ([#15752](https://github.com/craftcms/cms/issues/15752))
- Fixed a bug where it wasn’t possible to save nested entries via the `entries/save-entry` controller action. ([#15737](https://github.com/craftcms/cms/issues/15737))
- Fixed a bug where hyperlinks in Link field inputs could wrap unnecessarily. ([#15738](https://github.com/craftcms/cms/issues/15738))
- Fixed an error that occurred when running the `entrify/global-set` command. ([#15746](https://github.com/craftcms/cms/issues/15746))

## 5.4.4 - 2024-09-14

Expand Down
15 changes: 8 additions & 7 deletions src/console/controllers/SectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ public function actionCreate(): int
'previewTargets' => [],
]);

$validateAttribute = function($attributes, ?string &$error = null): bool {
$section = new Section($attributes);
$validateAttribute = function($attributes, ?string &$error = null, string $class = Section::class): bool {
$model = new $class($attributes);
$attributeNames = array_keys($attributes);
if (!$section->validate($attributeNames)) {
$error = $section->getFirstError($attributeNames[0]);
if (!$model->validate($attributeNames)) {
$error = $model->getFirstError($attributeNames[0]);
return false;
}
return true;
};

$getDefaultAttribute = function(string $attribute, string $value) use ($validateAttribute): ?string {
if ($validateAttribute([$attribute => $value])) {
$getDefaultAttribute = function(string $attribute, string $value, string $class = Section::class) use ($validateAttribute): ?string {
if ($validateAttribute([$attribute => $value], class: $class)) {
return $value;
}
return null;
Expand Down Expand Up @@ -292,7 +292,8 @@ public function actionCreate(): int
'default' => Inflector::singularize($section->name),
]);
$entryType->handle = $this->prompt('Entry type handle:', [
'default' => StringHelper::toHandle($entryType->name),
'validator' => fn(string $handle, ?string & $error = null) => $validateAttribute(compact('handle'), $error, EntryType::class),
'default' => $getDefaultAttribute('handle', StringHelper::toHandle($entryType->name), EntryType::class),
]);
$saveEntryType = true;
}
Expand Down

0 comments on commit c7e4990

Please sign in to comment.