Problem/Motivation
At the moment module won't allow to store an entity when there are violations. But one might want to store entity when there are typed data violations, like when required fields are empty. Would be great if there is a way to store such entity.
Proposed resolution
On the processor's advanced settings, provide an option to skip all validations. Add a warning to it that skipping validation can potentially cause PHP and SQL errors.
Ideally, there would also be a configuration option to skip specific violations. Possible solutions for these:
-
A multiselect form element that lists all possible violations.
Pro: user doesn't need to know which ones exist.
Con: list could potentially be very long, making it hard to pick the right violations to skip. -
A textarea in which the user can type the violation to skip, one per line.
Pro: form takes less time to load and we don't have to figure out how to generate a list of possible violations.
Con: not user friendly, with trial and error user would need to figure out which violations exactly to skip.
Remaining tasks
- For the config option to skip all validations, config schema needs to be added.
-
For the config option to skip all validations, a warning must be added about the negative consequences:
Warning: skipping validation can potentially cause PHP and SQL errors.
Issue fork feeds-3062232
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
hideaway commentedCrafted the following little patch to introduce the settings for bypassing the validation. Would be better to ignore only specific types of violations though.
Comment #3
megachrizNote that skipping entity validations could potentially also cause SQL errors, like when a value is longer than the maximum length of a database field. I can imagine PHP errors could be caused as well. So maybe it is useful to add an extra warning to the description? Something like this:
Or indeed, as you suggest, only skip specific types of violations. If you want it to be configurable to select which violations to skip, you could potentially get a long list of violation types to choose from.
An alternative would be to make the fields that you cannot import for, only required for users with a certain role. It looks like the Required by role module provides this functionality. Else you could use a form alter in a custom module to make a field required under some circumstances.
Comment #4
klausiI think such an option is a good start. Excluding only certain validations might be hard?
The next step here is to write a test case to make sure this works when there are validation errors.
Comment #5
hideaway commentedRe-rolling this for us to work with alpha6
Comment #6
megachrizNote: an issue like this is also a discussion for Migrate. Migrate seems to skip entity validation by default.
#3095456: [Plan] Discuss strategy for long-term use of entity validation in the migration system
Comment #7
kimberleycgmI had a similar requirement where I needed to import items via the feed even if required values are missing. I wanted to ensure only these ones are imported and other validation failures are still caught, so I've implemented the configuration side of this. Now you must select individual types of constraints to ignore.
I couldn't seem to access each $violation's constraint ID in \Drupal\feeds\Feeds\Processor\EntityProcessorBase::entityValidate() so I'm having to compare the classes. Open to suggestions if there's a cleaner way to approach that!
Comment #8
kimberleycgmSmall change to filter out unselected options.
Comment #9
socialnicheguru commentedThere are fields added via other modules that are required that are not showing up on the list of fields to skip validation.
For example, this field, Visibility (field_content_visibility), is added by another module to content types. It is not visible in the list of fields.
I get this error on the feed:
Visibility (field_content_visibility): This value should not be null.
Comment #10
alt.dev commentedThe patch from the #8 comment provokes next error for me
Error: Call to a member function __toString() on string in Drupal\feeds\Feeds\Processor\Form\DefaultEntityProcessorForm->buildConfigurationForm() (line 194 of /var/www/docroot/modules/contI updated that patch to fix this error + fixed coding standards.
Comment #11
narendragupta commentedAdded the following patch to introduce the settings for bypassing the entity validation to work with beta1.
Comment #12
nwom commentedFor anyone confused, #10 is still the most recent version of the patch. #11 only was uploaded to work on a beta version, not the dev.
Comment #13
nwom commented#10 no longer applies against the newest dev. Here is a re-roll
Comment #14
maxilein commented#12 does not apply to latest dev
Comment #15
j.lucky commentedpatch for beta2.
Comment #16
j.lucky commentedComment #17
artem_kondra commentedre-roll to work with 8.x-3.0-beta3
Comment #18
philyBesides the failed test, patch #8 woks for me using Drupal 9.5.3, Feeds 8.x-3.0-beta3 and PHP 8.1.6
Comment #19
andriy khomych commentedI think this issue requires some clarification, on what kind of approach to use.
It makes sense to configure skipping validation per field type, however, what if the user wants to skip all possible validations?
I'd like to have such a checkbox/option and when it is enabled show the skip validation types field and specify what kind of rules to apply.
Comment #20
megachriz@Andriy Khomych
I've tried to make the next steps more clear in the issue summary.
Comment #21
andriy khomych commented@MegaChriz, thank you, looks perfect, I'll see if I can help with this.
Comment #22
andriy khomych commented@MegaChriz, I'll work on this issue and prepare MR soon.
Comment #23
andriy khomych commentedComment #25
andriy khomych commented@MegaChriz, opened MR - https://git.drupalcode.org/project/feeds/-/merge_requests/119
I see some possible downsides:
Comment #31
andriy khomych commentedSorry for spamming, closed all other MRs and fixed the issue with tests here.
https://git.drupalcode.org/project/feeds/-/merge_requests/121
Duplicated:
I see some possible downsides:
Comment #32
megachriz@Andriy Khomych
Thanks for working this! I scanned the code and at first glance it looks good. Kudos on adding test coverage too! I'll ask on the Feeds meeting this week if someone wants to test it.
I think this is fixable by only saving the enabled options: in the submit handler, manipulate the form values instead of saving the form values to the feed type as is. If I'm not mistaken, I think it can be done with a
array_filter()call, or else with a foreach loop.I think that an update function for users that used a patch is not a requirement. In my opinion, update functions are usually used to update configuration or data that came with an official release. I did have made an exception to this in the past, in a situation where it was complicated to manually update from a patched version to the official released version.
Comment #33
andriy khomych commented@MegaChriz, thank you for the quick check.
Regarding:
I was thinking the same, but it looked like I should implement some custom exception in such a case, so, I skipped it.
Well, if someone can update it as well - that would be great, otherwise, I can try to do it later.
Comment #34
megachrizHm, I wonder what you mean with custom exception? But I haven't studied the code in detail yet.
Just to be sure that we're talking about the same thing, I think that the following:
should be transformed into:
Comment #35
andriy khomych commentedSorry for the unclear comment, was in a hurry.
I meant that I don't like the idea to create specific if conditions and it was like this for the submit handler.
Exactly!
Comment #36
andriy khomych commentedWell, I decided to improve the config save.
@MegaChriz thank you for pointing me in the right direction.
Comment #37
megachrizI've taken a look at the code and made some remarks. :)
One test I would add is for ignoring a required field only.
I still have to try out the code in action though, so you could wait for that if you want to make adjustments.
Comment #38
andriy khomych commentedThank you MegaChriz.
I'll try to improve it.
Comment #39
megachrizI tested the MR in the UI and I noticed the following things:
Steps
This is good.
The validation types remain selected. I think this is not good.
If you agree with point 2 that when unchecking "Skip validation", validation types should no longer be saved, it would be good to also have functional tests that covers saving the config. So that would lead to the following test cases:
Comment #40
andriy khomych commentedThank you MegaChriz for the feedback, agree with point 2 and tests for it.
Comment #41
andriy khomych commentedHey MegaChriz, feel free to review it.
As for
I've added a separate test file FeedTypeSkipValidationTest.php, I guess no need to add them in the FeedTypeTest.php
Comment #42
andriy khomych commentedIn case someone needs, provided patch from MR.
Comment #44
andriy khomych commentedMegaChriz, unfortunately, latest feed type form skip validation tests are failing.
I don't know why, on my local env they are passing.
Comment #45
megachrizWith tests now passing, I think this is ready for review? Maybe I can find the time to do that review soon.
Comment #46
megachriz@Andriy Khomych
Thanks for your work! I've looked at the code and I made the following changes to it:
Instead of:
skip_validation_types:
ConfigExists:ConfigExists
Count:Count
I think that the first one looks cleaner.
Here is an error in creating the feed type. The feed type is created programmatically.
'processor_configuration[skip_validation]'is used when supplying data for a form to submit. When using the API you have to use actual arrays, thus like this:'processor_configuration[owner_id]' => 'admin (1)',in because else you get you an error about that user 0 cannot be referenced. I don't know why this happens. Looks like a bug in the test setup.FeedTypeFormSkipValidationTest::testEnableDisableSkipValidationSkipTypesI started the code with existing 'skip validation' configuration. This we can check if configuration doesn't change when not touching these fields.copyResourceFileToDir()added to FeedsCommonTrait. That method copies a file to the public file directory that then can act as the source of a feed.SkipValidationTest::testSkipValidationTypesLength()now also checks that the validation type "NotNull" is not skipped when you configure to only skip the "Length" validation type. The CSV source need to be adjusted a bit for that and as a result also the messages that were checked during all SkipValidationTest tests.Maybe @irinaz wants to test this in the coming week. Otherwise I plan to commit it this Thursday (if tests still pass).
Comment #47
andriy khomych commented@MegaChriz, thank you, makes sense to me!
Appreciate your help.
Comment #48
irinaz commentedI created DrupalPod, set body of article to be a required field, and checked box "skip validation". Items were imported without body. This DrupalPod can be use for more through testing.
Comment #50
megachrizThanks all for contributing! I merged the code!
Comment #52
elkaro commentedThis feature is very helpful to me. It would be great to get a release some time soon. Thanks.
Comment #53
megachriz@lelkneralfaro
Yes, I hope to create a new release this month. I originally planned to release in January this year, but other things got in the way. I'm now working on the last bits for the new release.