Validation error when saving entry from front end form, if Title Format is {id}
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
If you are submitting entries on the frontend via entries/save-entry, you'll end up with validation errors (title cannot be blank).
One hack around this is setting the slug/id to __id__ or something, and then replacing via events:
Event::on(
Content::class,
Content::EVENT_BEFORE_SAVE_CONTENT,
function (Event $event) {
if ($event->element->title === '__id__') {
$event->element->title = $event->element->id;
}
}
);
Event::on(
Element_SiteSettings::class,
Element_SiteSettings::EVENT_BEFORE_INSERT,
function (Event $event) {
if ($event->sender->slug === '__id__') {
$event->sender->slug = $event->sender->elementId;
}
}
);
With something like this in your form:
{{ hiddenInput('title', '__id__') }}
{{ hiddenInput('slug', '__id__') }}
Originally posted by @timkelty in https://github.com/craftcms/cms/issues/6110#issuecomment-663180551
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the frontend entries/save-entry entry point and the validation path used when the title format is {id}; the issue also shows the related before-save event hooks and form inputs. Done means a frontend submission using {id} for the title and slug saves without a “title cannot be blank” validation error, without requiring the event-based workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100