Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate handle of the new entry type #15760

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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