processwire / processwire-requests

ProcessWire feature requests.
39 stars 0 forks source link

Add required-if support to Repeaters #262

Open Toutouwai opened 5 years ago

Toutouwai commented 5 years ago

Short description of the enhancement

It would be useful if we could use required-if conditions within repeaters.

I took a look at what might be needed to add support for this and came up with the following...

1. In InputfieldRepeater.module, remove the lines that set useDepedencies to false. https://github.com/processwire/processwire/blob/6ad523966655a03c60f3d76a9b38f7f3dfd2d33e/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module#L338 https://github.com/processwire/processwire/blob/6ad523966655a03c60f3d76a9b38f7f3dfd2d33e/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module#L395

2. In InputfieldWrapper::_getDelayedChildren, in addition to dealing with child items that are themselves InputfieldWrappers (as is already done), where child items are instances of InputfieldRepeater then deal with the InputfieldWrappers contained in the repeater.

Before line 1120:

if($child instanceof InputfieldRepeater) {
    foreach($child->getWrappers() as $wrapper) {
        $a = array_merge($a, $wrapper->_getDelayedChildren($clear));
    }
}

3. In InputfieldForm::selectorMatchesInputfield, get the inputfield in a way that handles inputfields that are contained within a repeater field in the form. I did this by modifying InputfieldWrapper::getChildByName by adding the following at line 1162...

} else if($child instanceof InputfieldRepeater) {
    foreach($child->getWrappers() as $wrapper) {
        $inputfield = $wrapper->getChildByName($name);
        if($inputfield) break;
    }
}

...but I expect it would be better not to change this method and instead use some solution specific to InputfieldForm::selectorMatchesInputfield.

4. Show repeater items that have a "Missing required value" notice in an open state.

Currently InputfieldRepeater sets the openIDs for empty required fields via InputfieldWrapper::getEmpty (incidentally I think this is the only place within the core that this method is used) but this method doesn't currently account for required-if conditions. So this method could be enhanced to use the same required-if evaluation that is used in InputfieldForm::processInputRequiredIf, or alternatively perhaps something could be added to InputfieldForm::processInputRequiredIf that sets repeater items to be opened when it evaluates that a required-if condition for a field within a repeater returns true.

kunago commented 4 years ago

Is there any commit I could possibly use to test it? I don't know the API that well but I would be glad to test.