Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Let's convert the form validation logic of comments to validation constraints + test that. We can keep the existing form validation in place until we leverage the entity validation for forms and remove it then.
Comment | File | Size | Author |
---|---|---|---|
#71 | 2002158-71.patch | 11.01 KB | andypost |
#71 | interdiff.txt | 5.21 KB | andypost |
Comments
Comment #1
fagoComment #2
vlad.dancerJust temporary patch. Still need working under validate() in Comment.php. I'm going to make researching on entity validation deeper. So proposals about patch are welcome!
Comment #3
podarokcode indenting error
Comment #4
vlad.dancerCode cleanup
Comment #6
fagoWe should not invoke validate from save(). Please see for the patch in #2002152: Implement entity validation based on symfony validator for a working example and implement it analogously.
Note, that #2002152: Implement entity validation based on symfony validator contains the foundational constraints you'll need, so best just base any work on this on the patch over there.
Comment #7
vlad.dancer@fago. Many thanks for information. I'll look this and re-wtite code according to your advices.
Comment #8
klausiFirst patch that adds some length constraints and a test case.
I also tried to convert the comment email field to the email_field type. That showed a very strange bug where the isEmpty() method returns TRUE although an email value has been set. I tried to debug this and as soon as I copy over the exact code from ItemList::isEmpty() to Field::isEmpty() the test passes.
So I think that there is some underlying bug with setting values and isEmpty(), or I'm doing something very wrong here.
Comment #10
klausiOk, so let's ignore the email field for now, that should be investigated in a different issue.
Added a CommentName validation constraint to check that the anonymous author name does not collide with a registered user name on the site. And a lot of other length constraints.
Comment #11
klausiHa, I think I found the problem in the setValue() method of FieldItemBase. Why are we unsetting the values here? Anyway, let's see what else breaks with this change.
Comment #13
klausiOk, so that was a bad idea. Turns out we need to fix the isEmpty() method of EmailItem. Actually that fix deserves its own unit test, but I could not reproduce it with typed data alone:
Anyway, this should fix the email validation for comments now.
Comment #15
klausiLooks like there is a hidden dependency to email.module in EntityStorageControllerBase::invokeFieldItemPrepareCache(), so we simple check if the module key even exists in the typed data definition before reading from it.
Comment #16
klausiAdded a test case for invalid comment homepage now that #2039961: uri_field should use uri type as value is committed.
Comment #17
klausiDoes not apply anymore, rerolled.
Comment #19
klausiSo suddenly the CommentTranslationUITest fails, does anyone have a clue why?
When looking at the test case I found out that in ContentTranslationUITest::assertBasicTranslation() after line 74 a translation for French already exists. That's why the POST request on line 80 fails, because it tries to add a translation which already exists, so it gets an access denied on /fr/comment/1/translations/add/en/fr.
BTW: never start your test methods with "assert", that is a special name where Simpletest does not show the correct line number, which makes the test case harder to debug. Example: use checkBasicTranslation() instead of assertBasicTranslation().
Comment #20
klausiSo it looks like the comment homepage uri_field was set to the empty string, which triggered a constraint violation (because empty strings are not valid URIs). So I fixed the PrimitiveTypeConstraintValidator to ignore empty string values, is that the correct approach? Or should we unset() entity fields if the form API gives us an empty string for a field?
Comment #21
klausi#20: comment-validation-2002158-20.patch queued for re-testing.
Comment #22
moshe weitzman CreditAttribution: moshe weitzman commented#20: comment-validation-2002158-20.patch queued for re-testing.
Comment #24
klausiThe change for EmailItem is not necessary anymore, removed that.
Comment #25
moshe weitzman CreditAttribution: moshe weitzman commentedReally? Thats what Drupal has become?
Also, we are simply validating string length here. Do we really need to keep creating new constraint classes each time. This seems extraordinarily verbose.
Comment #26
klausiYeah, retrieving the comment object from the validation context is a bit long, but I guess we can't do much about that at this point.
No, we don't have to create length constraints every time, we can simply reuse them. But we have to create a constraint for the comment author name, because we have to run a custom query that does not already exist in another constraint.
Comment #27
Berdir#24: comment-validation-2002158-24.patch queued for re-testing.
Comment #29
klausiRerolled and remove a @todo that does not make sense, because CommentNewValue is a computed field anyway.
Comment #30
Wim Leersklausi:
CommentNewValue
is going away in #1991684: Node history markers (comment & node "new" indicator, "x new comments" links) forces render caching to be per user :)Comment #31
klausiPatch does not apply anymore, rerolled. No other changes.
Comment #32
jibran#31: comment-validation-2002158-31.patch queued for re-testing.
Comment #34
klausiRerolled, no other changes.
Comment #36
klausiComments are fields now, so had to fix the test case.
Comment #37
effulgentsia CreditAttribution: effulgentsia commented#2110345: Simplify validation constraint implementations for fields improves that.
Comment #37.0
effulgentsia CreditAttribution: effulgentsia commentedUpdated issue summary.
Comment #38
effulgentsia CreditAttribution: effulgentsia commentedThere are a couple changes I'd like to make here. Starting with a reversion of the change to PrimitiveTypeConstraintValidator. Curious about what currently fails if we do that.
Comment #40
effulgentsia CreditAttribution: effulgentsia commentedThis moves the constraint from the context-less $value to a context-full $field_item per #2110345-4: Simplify validation constraint implementations for fields, and also converts the db query to a EFQ to match what is done in HEAD's CommentFormController::validate().
Comment #41
effulgentsia CreditAttribution: effulgentsia commentedI think this is good to go, but since I made changes, I'm not eligible to RTBC. Anyone else care to?
Comment #42
moshe weitzman CreditAttribution: moshe weitzman commentedshould we use id() method here?
It is a bit awkward IMO that our violations make referene to the value instead of actually telling you 'foo' is already a user on this site.
Same. Tell us the value that is invalid.
Same. What value? "Primitive type" is a very odd error message as well.
Comment #43
effulgentsia CreditAttribution: effulgentsia commentedChild issue of #1696648: [META] Untie content entity validation from form validation, therefore critical.
Comment #44
klausiRerolled.
@moshe: there is a @todo in the code to fix accessing the comment author, so leaving that as is for now. And regarding the messages: those are generic messages most of the time, we have the property path to indicate which variable should be fixed. I agree that "This value should be of the correct primitive type." is a terrible message, but that should be fixed in a separate issue against PrimitiveTypeConstraint and PrimitiveTypeConstraintValidator.
Comment #45
fagoReasonable, but then this constraint should be default in the field type?
that's part of the field type already - or better should be. It has a weird max_length setting that does not seem to be applied. It should have a constraint by default though, as UuidItem.
Again, should be the default.
These used should as an entity manager, created during setup.
Let's add a todo linking the issue to fix that message?
Agreed with moshe - this really should tell me what is already registered.
Comment #46
klausiFixed most of that things.
I don't think we can establish a generic max length for URIs, since no such thing is defined. The field schema does not even use "varchar" but rather "text", so it looks like we are prepared for long values here.
Comment #47
klausiAnd opened #2171539: Improve PrimitiveTypeConstraint validation messages.
Comment #48
fagoThat seems weird to me as it violates the inheritance principle. I've not idea why there is an unused max_length setting on the string field - why not just use the length constraint if wanted?
"%name belongs to a registered user."
?
Needs an update ;-)
Comment #49
fago>I've not idea why there is an unused max_length setting on the string field
oh - it's used to generate the schema.
Comment #50
fagooh, we can simply fix uuid-item instead to provide a new default for max_length and we are good.
Also discussed with yched on IRC on settings vs. constraint. Having the setting even if not strictly necessary is fine and is more consistent with what some configurable fields *have to* do. Also, I think the DX is better that way as well :-)
Comment #51
Github sync CreditAttribution: Github sync commentedklausi opened a new pull request for this issue.
Comment #52
klausiFixed fago's remarks. Now also using max_length for UriItem, where we need to specify a default. I went with 2048 which seems to be the limit of older browsers and search engines, see http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-...
Comment #54
Github sync CreditAttribution: Github sync commentedSome commits were pushed to the pull request.
Comment #55
Github sync CreditAttribution: Github sync commentedklausi pushed some commits to the pull request.
Comment #56
klausiRerolled after #2002168: Convert form validation of terms to entity validation went in.
Comment #57
andypostLooks RTBC, except 2 small nitpicks (3,4)
This needs follow-up issue to fix, maybe allover core
But it should not stop this issue
Original message is 'The name you used belongs to a registered user.'
But I think it's fine now, would be unified in #2002180: Entity forms skip validation of fields that are edited without widgets
Just a small chance that random will be the same as root_user, but better extend comment here or make
needs blank line between
Comment #58
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #59
klausiFixed issue 3) and 4) mentioned by andypost, leaving 1) and 2) as is because fixing other URI fields is out of scope for this issue and the validation error message has already been bike-shedded above.
Comment #60
andypostAwesome!
Comment #61
dawehnerIs there any reason beside lazyness that we don't inject dependencies for new code?
Comment #62
andypostSeems
ConstraintManager
does not allows to inject containerComment #63
andypostFiled follow-up #2197029: Allow to inject dependencies into validation constraints
And fixed following
This already fixed
Comment #64
dawehner+1
Comment #65
alexpott63: 2002158-63.patch queued for re-testing.
Comment #67
andypostAfter #2181593: Convert entity bundle base fields to entity reference we need properly populate 'field_name' and this field should exists
Comment #68
andypostPostponed on #2197723: Comment::getFieldId() should return actual field ID
That interdiff fixes the test, but includes hack from [2197723-1]
Comment #69
andypostNo need to postpone critical, so just revert the part of #2181593-26: Convert entity bundle base fields to entity reference and re-open #2149859: Convert the 'field_id' base field on comment entities to an entity reference field
Also comment should be created by passing 'field_name' and field with instance should exists.
Comment #70
Wim LeersOnly stupid nitpicks:
Missing newline before closing curly brace.
Why these parentheses?
Why no newline before the code comment, but one between two method calls?
Similar strange newline here.
Comment #71
andypostFixed 1-4 + doc-block
Comment #72
andypostRelated issue explains why comment
field_id
can't be used as entity referenceComment #73
dawehnerAll the newlines got adressed
Comment #74
catchCommitted/pushed to 8.x, thanks!
Comment #76
fagoPosted a follow-up here: #2403485: Complete conversion of comment form validation to entity validation