We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18174
    • 116 Posts
    In a custom validation hook I try to overrule field values by $hook->setValue(), but the field values won't be overwritten.
    When returning to the form (return false;) all field values remain. What am I doing wrong?
    regards
    manu
    • What you want is not a validator action.

      Use preHooks or postHooks instead.
      With &preHooks=`YourSnippetName` you can set initial values of the fields
      And with &hooks=`YourSnippetName` you can set values after the submit and validation rules.
        MODX Ambassador (NL) & Professional MODX developer
        Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
        MODX e-commerce solution SimpleCart
        • 18174
        • 116 Posts
        Thanks for your reply bertoost.
        For sure I did it in a hook snippet.
        My intention is to have a extended validation in a (post)hook and overwrite the field values. The validation is a complex relational validation over several fields, too complicated to handle in a simple validation rule.
        Out of the hook I would like to overwrite a certain field value and let that value show in the input field, doing this by returning false.
        regards
        manu
        • I think that causes because your hook is only being used for passing data.. When you do setValue() in your hook snippet it will set an extra key=>value to the fields list (or overwrite another fields value) but it's not being used in the form again, but it will be passed to the next hook or email placeholders etc.

          I did some little test with the same setup, but with validation, and that's probably how it works.

          What I see in FormIt's request handler class;

          ...
          /* process form */
          if ($this->formit->postHooks->hasErrors()) {
          	$success = false;
          	$this->formit->postHooks->processErrors();
          } else {
          	/* assign new values from postHooks */
          	$this->dictionary->fromArray($this->formit->postHooks->fields);
          }
          ...


          But that means that you shouldn't set an error and probably also shouldn't return false to get into that else part. That postHooks->hasErrors() part is about all your hooks, so that would help too.. I tried to set a second snippet hook with the error, and the first one is just setting the other value, but that doesn't matter.

          I also tried to make it as custom validator.. but you don't have a setValue() in there, and your even don't have the possibility to reset the value back in the fields array which is available there...

          Pfiew.. you make it difficult ;-)
          Why not just passing the new value to the next step? ;-)
            MODX Ambassador (NL) & Professional MODX developer
            Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
            MODX e-commerce solution SimpleCart