The module creates a notice of "Undefined index: #required" in multiple_registration_field_widget_form_alter() with numeric type elements. It seems that the structure of those elements is $element['value']['#required'] and the function searches for $element['#required']

Comments

kallado created an issue. See original summary.

kallado’s picture

Issue summary: View changes
yoftahe.addweb’s picture

Status: Active » Needs review
StatusFileSize
new858 bytes

@kallado, Please review the attached patch to resolve this error. I faced it while installing the module. Hope this patch helps you

Thanks!..

kallado’s picture

Thank you @niles.addweb. I always try to avoid using patches in modules because of the issues that a module update can arise if the patch isn't applied. I'm my situation I solved the problem with this function:

function my_module_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

    if (!isset($element['#required'])) { 
        if (isset($element['value'])) { // for numeric fields
            $element['#required'] = $element['value']['#required'];
        }
        elseif (isset($element['target_id'])) { // for reference fields
            $element['#required'] = $element['target_id']['#required'];
        }
    }
}

I don't know if it's generic enough but works for me.
I'll take a look at the patch as soon as I have time.
kallado

kallado’s picture

@niles.addweb i still get a warning of undefined index in the previous if statement
if (!$element['#required'] && !empty($field_roles_required))
because $element['#required'] doesn't exist.

Sorry but I’ve been really busy and didn't have time to look at the patch until now.

kallado

ysamoylenko’s picture

Issue tags: +epam-contrib-2018
driverok’s picture

Assigned: Unassigned » driverok
driverok’s picture

driverok’s picture

Assigned: driverok » Unassigned
+++ b/multiple_registration.module
@@ -218,9 +218,11 @@ function multiple_registration_field_widget_form_alter(&$element, FormStateInter
+      if(!empty($element['#required'])) {

Please add a space after if.

ysamoylenko’s picture

Hello everyone.

@kallado, You don't need to add the additional check as you proposed.

We already have this functionality in code:

foreach (Element::children($element) as $child) {
          $element[$child]['#required'] = $required;
}

Where each sub-element e.g. 'value' will be passed as a child and his '#required' state will be changed.

Please review the last patch and check watchdog for 'Notice' messages from the module.

alunyov’s picture

Status: Needs review » Needs work

@ysamoylenko,

Please fix typo here:
// issetIf something was selected.

Beside that patch works for me fine.

ysamoylenko’s picture

Thank you for reviewing. The patch was updated.

  • ysamoylenko committed 6279944 on 8.x-2.x
    Issue #3006731 by ysamoylenko, nilesh.addweb, kallado, driverok, alunyov...
ysamoylenko’s picture

Status: Needs work » Fixed

The patch was committed.

kallado’s picture

@ysamolyenko @alunyov Thank you guys.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kallado’s picture