cms icon indicating copy to clipboard operation
cms copied to clipboard

"The given data was invalid" / "This URI has already been taken." when using /{id} as route

Open schwartzmj opened this issue 2 years ago • 2 comments

Bug description

Unable to create a new entry when collection route is set to /{id} with the following errors:

  • "The given data was invalid"
  • "This URI has already been taken."

This also happens if you have a page at e.g. /members and try to set the route to /members/{id}.

Setting the collection to Orderable fixes it as long as max_depth is set to 1

How to reproduce

  1. Start a fresh Statamic app.
  2. Create a new collection.
  3. Set the route to /{id}
  4. Try to create an entry, receive error
  5. Set collection to "Orderable" w/ a max depth of 1
  6. Create an entry without error

Optionally, create a page at e.g. /testing and set your collection to the route /testing/{id} and receive the same errors/solutions above.

Logs

No response

Versions

Statamic 3.2.38 Solo Laravel 8.83.4 PHP 8.1.2 No addons installed

Also tested on 3.3 beta

Installation

Fresh statamic/statamic site via CLI

Additional details

For what it's worth, one downside of the workaround is that any collections that use dates are no longer sorted by date by default (at least according to the docs).

schwartzmj avatar Mar 15 '22 04:03 schwartzmj

Maybe same issue as #4032 ? Since the ID doesn't exist when it tries to create, / (Home) already exists.

Only strange thing is the max_depth: 1 workaround.

schwartzmj avatar Mar 15 '22 04:03 schwartzmj

Oh, same issue!🙄 Took me 3 hours to find this... Thank you for the workaround!

j3ll3yfi5h avatar Aug 05 '22 16:08 j3ll3yfi5h

I forgot all about this issue because it was working.

I believe I was on 3.3.46 and this was no longer an issue. Then I updated to 3.3.47 and it was not working again.

My guess is that #6921 "broke" it again? @jasonvarga

Related: https://github.com/statamic/cms/issues/4032

schwartzmj avatar Oct 24 '22 19:10 schwartzmj

I don't think it would have been fixed since you opened the issue. The workaround also just sounds like a fluke.

The issue is like you already explained.

If you have a route like /members/{id} and you create a new entry, the url would be /members since there's no id at the point where it tries to validate it. There's a conflict with the other /members entry.

jasonvarga avatar Oct 25 '22 14:10 jasonvarga

I don't think it would have been fixed since you opened the issue. The workaround also just sounds like a fluke.

The issue is like you already explained.

If you have a route like /members/{id} and you create a new entry, the url would be /members since there's no id at the point where it tries to validate it. There's a conflict with the other /members entry.

My particular issue here, is that I'm generating a Title based on other fields in the entry, therefore I cannot use an automatically generated slug based on the title.

For example, the title is August 30th, 2022 based on a date field. The slug would then be something like august-30th-2022. If somebody creates a second entry with the same date, they won't be able to because the slug is already taken. It cannot automatically append a -2 to the slug. Because of this issue, I've been using IDs.

It was working. I don't know when or how, but it no longer works.

Ideally I'd be able to use Antlers to generate a slug, similar to how we can generate titles or routes. Then we can toggle something like "auto-increment" to append -2 or whatever.

Any workaround ideas as it currently stands? I could make the collection "manually orderable" and set the max depth to 1 like I mentioned above, but I don't really want this collection to be drag-and-drop orderable. It should be ordered by date.

schwartzmj avatar Oct 31 '22 19:10 schwartzmj

I am also confronted to this problem.

I am using title_formatto generate the title and use this title and the id in the route, with a mount page.

I have just noticed that if the route is route: '/{mount}/{title}-{id}' it does not work indeed, but as I have other collections with similar but more complicated routes using more fields variables, that DO work, I realised that if I add "whatever" before, where "whatever" does not correspond to any existing page, it works…

route: '/{mount}/{title}-{id}'route: '/{mount}/{cours}-{module}/{title}-{id}'

Maybe that can help you?

binoclard avatar Nov 09 '22 14:11 binoclard

It was working. I don't know when or how, but it no longer works.

Did you have an existing page, or was the url only created in the route? I tripped on this for hours, because when testing, I deleted the actual whatevermount page, changed the route to whatever/{id}and it worked. So the problem is not the route, is the reference to an actual existing page/entry id in the route.

binoclard avatar Nov 09 '22 14:11 binoclard

It was working. I don't know when or how, but it no longer works.

Did you have an existing page, or was the url only created in the route? I tripped on this for hours, because when testing, I deleted the actual whatevermount page, changed the route to whatever/{id}and it worked. So the problem is not the route, is the reference to an actual existing page/entry id in the route.

Yeah, my understanding is that the id variable doesn't exist until after the Entry is created, so the route is referencing whatever the path would be without the id, and it is already taken. You deleting the mount page allows it to be created because the path is not taken.

schwartzmj avatar Nov 22 '22 21:11 schwartzmj

I don't think it would have been fixed since you opened the issue. The workaround also just sounds like a fluke.

The issue is like you already explained.

If you have a route like /members/{id} and you create a new entry, the url would be /members since there's no id at the point where it tries to validate it. There's a conflict with the other /members entry.

@jasonvarga FWIW, the example in the docs at https://statamic.dev/collections#for-when-you-dont-care-about-seo doesn't work. Should we remove this from the docs until it is fixed? Is this something that can be fixed without reworking a bunch of Statamic logic? This is still a bit of an issue for me so just wondering if I should just learn to work around this.

schwartzmj avatar Nov 22 '22 21:11 schwartzmj

Oh, same issue!🙄 Took me 3 hours to find this... Thank you for the workaround!

Workaround stopped working too... :(

j3ll3yfi5h avatar Dec 01 '22 12:12 j3ll3yfi5h