FormIt - clearFields inside custom hook

I have a custom hook that under some conditions returns false, so that the following hooks will not be executed. But I still want to do something like clearFieldsOnSuccess (which only seems to execute if all hooks return true).

So how can I clear All fields inside from inside a hook?
I tried this with no effect:
$hook->setValues(array( 'param' => '' ));

The way to set the hook values is usually
$hook->setValue('valueName','value to set')

If you use $hook->getValues(); that will give you an array you could then loop through to set everything to blank:

$valueList = $hook->getValues();
foreach($valueList as $key => $value) {
    $hook->setValue($key,'');
}

That’s untested but may put you on the right track.

Thanks for the hint!
That’s actually what I thought, but this didn’t work as expected.

Just to see what will happen I also tried this as a preHook and renderHook.
The preHooks don’t seem to have access to the values at all.
The renderHook can access the values, and I can set them to empty strings. But the original values will be render anyway.

I’m not sure if this even makes sense, but what if your custom hook returns true but passes a flag to a “cleanup” hook just after it, that does nothing but return true if everything is good (based on the flag), and clears the fields and returns false if not.

Wouldn’t that just put the clean-up code (which doesn’t seem to work) in another hook?

My setup is smth like this
[[!FormIt?
&hooks = ‘myCustomHook, redirect’
&redirectTo = 1234
&store = 1
&clearFieldsOnSuccess = ‘1’
&validate = ‘stuff…’
]]

The custom hook either returns true, then it redirects.
Or it does some stuff, then returns false. It should clean up the values in this case.
clearFieldsOnSuccess only seems to work if validations and all hooks were successful.

I guess it would.

According to the docs, clearFieldsOnSuccess only works if there’s no redirect, so in your case it should never work, though maybe the docs are wrong.

TBH, I never use FormIt. I can usually roll my own form and snippet code faster than I can get FormIt to work.

I’ve run into your exact issue with preHook before. It would make logical sense that the preHook would be able to accept the incoming values on reload of the form, but they don’t have that ability.

I had assumed you were going to be putting this into the snippet that sometimes returned false before you exited with the false - is that where it’s positioned in the chain or somewhere else?

I put the cleanup code in the custom snippet and tried calling it as a hook, prehook and renderhook.
It only works in the hook (and renderhook I suppose, but didn’t test) if it returns true and it is the last hook. Or probably also, if all following hooks return true, too, making the chain “successful”. But since the following hook is a redirect, I didn’t test that.