- Issue created by @michaellenahan
- π©πͺGermany michaellenahan
Digging further, it appears that the problem is unrelated to ECA.
If we disable the ECA, and try to set `['#required'] = TRUE` in code, we get the same results:
- validation error is thrown for cardinality 1 or unlimited
- validation error is not thrown for cardinality > 1 (that is, 2, 3, or some other number)Tested with the following code:
function mymodule_form_alter(&$form, $form_state, $form_id): void { if ($form_id == 'node_page_form') { $form_object = $form_state->getFormObject(); $entity = $form_object->getEntity(); foreach ($entity->getFieldDefinitions() as $field_name => $field_definition) { if ($field_definition->getName() == 'field_file') { // Set the field as required $form[$field_name]['widget'][0]['#required'] = TRUE; } } } }
- Merge request !11883Issue #3519279: Programmatically setting form field as required does not work... β (Open) created by michaellenahan
- π©πͺGermany michaellenahan
Thank you to @tstoeckler and @rodrigoaguilera for your help at Drupal Dev Days Leuven.
Tests are now green, though perhaps more tests are needed. - πΊπΈUnited States smustgrave
Since the fix appears to be in the file module can test be moved there too
Thanks
- π³πΏNew Zealand quietone
Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies β .
- π©πͺGermany tstoeckler Essen, Germany
Nice work finding #3011744: File/image widget validation is inconsistent based on cardinality/visibility β . I think it makes sense to keep this open separately as a distinct bugfix (which is also not caused by #2715859: ImageWidget::validateRequiredFields() produces an error message if an image field is hidden with hook_entity_field_access() β which #3011744: File/image widget validation is inconsistent based on cardinality/visibility β is a follow-up of). Then that issue can take care of the issue of hidden widgets and the cardinality issue can be dealt with here.
Re #23, I think the existing test coverage is fine in image module, but I do agree that this needs some explicit test coverage in file module. I looked around a bit and I think it would make sense to add some assertions to
\Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testMultiValuedWidget
. There we are already creating two fields with cardinality 3, so we would only need to submit the form and assert that the respective error messages are displayed before going on with the rest of the test. Tagging accordingly.Otherwise looks great, nice find and nice fix! ποΈ