Jump to content
saschapi

required if / visible if in pagefieldset?

Recommended Posts

Hi guys,

I'm working with pagefieldset and have several fields in there. 

Every field has a accompanying checkbox. I want to only show the field, if the checkbox is checked. That works as expected if I have a rule in "visible only if" like "checkboxfield=1". 
However, I would like this field to be required (of course only if it is visible aka the checkbox is checked). The visibility thing works, but on save PW complains about the field not being filled, even if the checkbox is not checked. Usually if a field is hidden, the check for required is not executed. This does not work in "pagefieldset"? Even If I add "checkboxfield=1" to "Only required if" it does not work.

Is this because dependencies in repeaters do not work stable and pagefieldset works like a repeater?

Cheers Sascha

Share this post


Link to post
Share on other sites
On 2/5/2020 at 1:45 AM, saschapi said:

Is this because dependencies in repeaters do not work stable and pagefieldset works like a repeater?

Yes.

Some possible solutions, in order from easiest to hardest or least recommended:

1. Use FieldsetGroup instead of FieldsetPage if you have ProFields.

2. Use a normal Fieldset instead of FieldsetPage, and just bite the bullet if you have to spend some time setting up the fields in multiple templates.

3. Write some custom JS for the admin that adds/removes the HTML required attribute to the adjacent field depending on the state of the checkbox.

4. Use a hook to a processInput method to do custom validation for the required fields. So if your required field is a text field then you would hook InputfieldText::processInput. Here is an example for a text field named "text_1" that is required if a field named "checkbox_1" is checked inside the same Repeater item (or FieldsetPage item):

$wire->addHookBefore('InputfieldText::processInput', function(HookEvent $event) {
	/* @var InputfieldText $inputfield */
	$inputfield = $event->object;
	// Get field
	$field = $inputfield->hasField;
	// Only for field "text_1"
	if(!$field || $field->name !== 'text_1') return;
	// Get page
	$page = $inputfield->hasPage;
	// Only for fields in a Repeater page
	if(!$page instanceof RepeaterPage) return;
	// Repeater suffix
	$r_suffix = '_repeater' . $page->id;
	// Get checkbox field
	$wrapper = $inputfield->parent;
	$checkbox = $wrapper->getChildByName('checkbox_1' . $r_suffix);
	if(!$checkbox) return;
	// If checkbox is checked then field is required
	if($checkbox->value) {
		$inputfield->required = 1;
	} else {
		$inputfield->required = 0;
	}
});

If you have multiple required fields you'll need to adjust this to suit.

5. I posted some suggested changes to the core to support required-if inside Repeaters here: https://github.com/processwire/processwire-requests/issues/262
But making custom changes to the core is not recommended because they will be lost when you upgrade.

  • Thanks 1

Share this post


Link to post
Share on other sites

Thank you for this extensive answer. I might go back to use FieldsetGroup for this particular purpose. :) 

It is easier to loose the "use one field in several contexts" than to deal with "build some complicated solution for the conditional requirements" for this.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...