contao/core

Unique flag checks before save callback

dmolineus opened this issue · 8 comments

If a field has the unique flag set the unique check is made before the save callback is triggered.

Imagine the situation where a user inserts the same alias which already exists in the database and a save callback would fix it by adding a suffix. This callback is never called.

This error occurs in contao/core 3.5.x and the latest contao/core-bundle as well:

https://github.com/contao/core-bundle/blob/08a6a6c9eab3669e8ae96deb1ea95dba9ebf2ebf/src/Resources/contao/drivers/DC_Table.php#L3049-L3053

I understand your point, however I'm not sure if such a change is backwards compatible?

As discussed in Mumble on March 15th, changing this would not only break backwards compatibility but also be wrong in terms of program flow. The save callback is executed when a value is saved not when it is validated. Validation needs to be done before saving.

Is there not another callback you can use?

I understand your arguments. However following your arguments there should be another callback which is called before validation. but as far as I know there is no other callback which is triggered here. Correct me please, if I'm wrong.

@aschempp Any idea how this could be solved without adding a validation callback?

ausi commented

By disabling the unique flag and checking the uniqueness in the save_callback itself?

By disabling the unique flag and checking the uniqueness in the save_callback itself?

That's what I actually did. I only consider that it could be a common usecase that the value might be changed before the validation should be applied.

If you think it's not worth to change anything, feel free to close.

@aschempp Any idea how this could be solved without adding a validation callback?

By disabling the unique flag and checking the uniqueness in the save_callback itself?

A unique-check would no longer be necessary if the value is automatically update to be none-unique, so the unique flag would not make sense anyway.

That's very true.