Formit hook math always returns wrong result

I use formit with the hook math. Unfortunately I always get the error “Invalid value”, although the entered result is correct.
After clearing the ModX cache, the form works once. If I want to use the form again, the same error comes again.

[[!FormIt?
	&hooks=`math,spam,email,FormItAutoResponder`
	&emailSubject=`Kontaktformular - [[+name]]`
	&emailTpl=`emailSend`
	&emailTo=`xy@...`
	&emailFrom=`xy@...`
	&emailFromName=`[[+name]]`
	&emailReplyTo=`[[+email]]`
	&emailReplyToName=`[[+name]]`

	&fiarTpl=`contactSendFiar`
	&fiarSubject=`Sendebestätigung - Anfrage [[++site_name]]`
	&fiarFrom=`xy@...`
	&fiarFromName=`[[++site_name]]`

	&mathMinRange=`10`
	&mathMaxRange=`50`

&validate=`name:required,email:email:required,datenschutz:required,special:blank,math:required,text:required`

&successMessage=`[[$tplContactThanks]]`
&validationErrorMessage=`bitte füllen Sie alle Pflichtfelder korrekt aus.`
&datenschutz.vTextRequired=`Bitte akzeptieren Sie die Datenschutzerklärung`
&emailMultiSeparator=`<br>`
]]

[[!+fi.successMessage]]
[[!$tplContactForm]]

Chunk: tplContactForm

<div class="mid">
    [[!+fi.validation_error_message:notempty=`<p class="error">[[!+fi.validation_error_message]]</p>`]]
</div>
 
<form action="[[~[[*id]]]]#footer" method="post" class="form">
    <input type="hidden" name="nospam:blank" value="[[+fi.nospam]]" />
<div uk-grid class="uk-grid-medium">
 <div class="uk-width-1-1">
     <div class="mid">
    <input type="text" name="name" id="name" value="[[!+fi.name:htmlent]]" placeholder="Name" >
	[[!+fi.error.name]]
  </div>
  <hr />
  </div>
  
   <div class="uk-width-1-1">
   <div class="mid">
    <input type="email" name="email" id="email" value="[[!+fi.email:htmlent]]" placeholder="Email" >
	[[!+fi.error.email]]
  </div>
  <hr />
  </div>
 
  <div class="uk-width-1-1 fiSpecial mid">
   
    <input type="text" name="special" id="special" value="[[!+fi.special:htmlent]]" placeholder="Special" >
	[[!+fi.error.special]]
  </div>
  <div class="uk-width-1-1">
   <div class="mid">
    <textarea name="text" id="text" value="[[!+fi.text]]" placeholder="Ihre Nachricht" >[[!+fi.text:htmlent]]</textarea>
	[[!+fi.error.text]]	
	</div>
	<hr />

  </div>
  
<div class="mid uk-width-1-1">
    <div uk-grid class="uk-grid-medium">
        
<div class="uk-width-1-2@s">
<input type="checkbox" name="datenschutz[]" id="datenschutz" value="Ja" [[!+fi.datenschutz:FormItIsChecked=`Ja`]] class="css-checkbox"><label for="datenschutz" class="css-label"><span></span>
    Ich stimme der <a href="[[~id]]" uk-toggle >Datenschutzerklärung</a> zu.</label>[[!+fi.error.datenschutz]]
</div>

<div class="uk-width-1-2@s">
<label><strong>Sicherheitsfrage:</strong> Wie viel ist [[!+fi.op1]] [[!+fi.operator:is=`-`:then=`minus`:else=`plus`]] [[!+fi.op2]]?</label>
[[!+fi.error.math]]
<input type="text" name="math" value="[[!+fi.math]]" placeholder="Ergebnis hier eintragen" />
<input type="hidden" name="op1" value="[[!+fi.op1]]" />
<input type="hidden" name="op2" value="[[!+fi.op2]]" />
<input type="hidden" name="operator" value="[[!+fi.operator]]" />
</div>



  <div class="uk-width-2-3@s">
    <input type="submit" value="Anfrage senden" class="submit">
  </div>

<div class="uk-width-1-3@s uk-text-right">
    Alle Felder sind Pflichtfelder
  
  </div>
  
  </div>
</div>
  </div>
    
</form>

Modx 3.0.3
PHP 8.1
Formit 5.0.1

I can’t replicate this issue. I tested it with the same versions of MODX, PHP and FormIt and it seems to work fine.

Simplified test code:

[[!FormIt?
	&hooks=`math`
	&mathMinRange=`10`
	&mathMaxRange=`50`
    &validate=`name:required,math:required`
    &successMessage=`SUCCESS`
]]

[[!+fi.successMessage]]
[[!+fi.validation_error_message:notempty=`<p class="error">[[!+fi.validation_error_message]]</p>`]]
 
<form action="[[~[[*id]]]]#footer" method="post" class="form">
    <input type="text" name="name" id="name" value="[[!+fi.name:htmlent]]" placeholder="Name" >
	[[!+fi.error.name]]

    <label><strong>Sicherheitsfrage:</strong> Wie viel ist [[!+fi.op1]] [[!+fi.operator:is=`-`:then=`minus`:else=`plus`]] [[!+fi.op2]]?</label>
    [[!+fi.error.math]]
    <input type="text" name="math" value="[[!+fi.math]]" placeholder="Ergebnis hier eintragen" />
    <input type="hidden" name="op1" value="[[!+fi.op1]]" />
    <input type="hidden" name="op2" value="[[!+fi.op2]]" />
    <input type="hidden" name="operator" value="[[!+fi.operator]]" />

    <input type="submit" value="Anfrage senden" class="submit">
</form>

This is usually the sign that something gets cached that shouldn’t be. But I can see no error in the code you posted.

I have the exact same problem in another ModX installation.
This is also a one-pager.
Exactly the same code.
If I empty the core/cache/ folder, then it works once.

ModX 3.0.4
PHP 8.2
FormIt 5.0.1

I had the same issue that FormIt with “math” Hook always returning false. With setting opcache.revalidate_freq = 0 on the server I could resolve this. In the log file “formitMath array key not found” was an output when opcache was active.

This is the exact error message:
[2024-10-08 15:17:33] (ERROR @ /core/components/formit/src/FormIt/Request.php : 123) PHP warning: Undefined array key “formitMath”
[2024-10-08 15:17:33] (ERROR @ /core/components/formit/src/FormIt/Request.php : 123) PHP warning: Trying to access array offset on value of type null

When I disabled opcache this error went away and the form worked as expected.

1 Like