Jump to content

Dynamic Inputfield requirements .. inside of a FieldsetPage .. with hooks.


Recommended Posts

 Hi, I'm working on a process module which hooks after ProcessPageEdit::buildForm.

// Inside the module's init() method:
wire()->addHookAfter('ProcessPageEdit::buildForm', $this, 'renderFieldsAccordingToStop');

Using a lot of variables, the script decides then which fields to render and which ones to hide. It also dynamically sets the 'required' flag to only the visible fields, making sure the proper warnings get shown but only when these fields are visible. It's all quite straight-forward, when it comes to the regular InputFields:

	/**
	 * Render the form, and hide all the fields that are not part of the currently active step
	 * Note: These fields are hidden with the css class "hideme", as hiding them with collapse logic gave issues in combination with the dynamic requirements 
	 *
	 * @param  mixed $event
	 * @return void
	 */
	public function renderFieldsAccordingToStop(HookEvent $event)
	{

		$return = $event->return;

		// Get the page that is currently being loaded
		$page = $event->object->getPage();
		$input  = wire("input");
		$form = $event->arguments(0);
		$step = $this->getProgress($page);

		// Add some class to hide field when needed
		$html = "<style>.hideme{display: none;}</style>";
		$form->prependMarkup($html);

		// Get all the fields inside the form
        $inputfields = $form->getAll()->not("id=submit_save");
		
		// Set all inputfields according to step
		foreach ($inputfields as $key => $inputfield) {
		
			// Run some logic to decide if the field should be invisible (using CSS to hide, as the inputfield->collapse gave issues)
			if ( ... ) {
				$inputfield->wrapClass("hideme");
			}
			// Else make sure to require it. 
			else {
				$inputfield->required(true);
			}
		}
	}

This works fine for most field.

However, this does not work for fields that are embedded inside a FieldsetPage. The FieldsetPage itself does get set to required correctly, but the fields inside are not, and as a result a user can now skip these fields while I wish them to be required (but only when visible). 

I've tried a bunch of different techniques, but I can't get the fields inside of the FieldsetPage to become required. In fact, I can't figure out how to address these from inside the hook at all. 

The furthest I've gotten is access the names of the fields inside this FieldsetPage:

$field = $inputfield->hasField;

if ($field->type == "FieldtypeFieldsetPage") {

    // Get the inner fields
    foreach ($field->repeaterFields as $f) {

        $repeaterFieldName = wire()->fields->get($f);
        $subfieldName = "{$field}->{$repeaterFieldName}";

        self::log("The current field inside the FieldsetPage is: $subfieldName"); //--> logs "myfieldset->myfield"
    }
}

But how on earth do I use that information to update the settings of the Inputfield for myfieldset->myfield?
Especially how do I set the required property, but also in a more of a general question. 

(In addition, I also tried hooking into the Inputfield::render but to no avail. )

Thanks in advance!

Link to comment
Share on other sites

A FieldsetPage field is a kind of Repeater, and conditionally setting the required status of inputfields within a Repeater is difficult. AFAIK you can't do this from a ProcessPageEdit::buildForm hook. And what's worse is that you'd need to hook one method to conditionally show the required asterisk in Page Edit and a different method to actually enforce the required status when the input is processed for each inputfield type within the FieldsetPage. So you'd end up needing to apply your logic in three different places.

Long story short, you'll make things much simpler if you have Profields and can change your FieldsetPage to a FieldsetGroup, or failing that a standard Fieldset.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thank you for you insightful reply @Robin S! I've decided to step away from the FieldstePage method, and just creating simple fields with longer names. Takes some time to copy over all existing data, but it makes it all a lot simpler to deal with.

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...