MODX Community

[Register] advanced validation question

Hi,

I am building a website to learn languages.

I have a registration form with Register snippet and extra user fields. On registration the user must choose his native language and the language he wants to learn.

Before the registration process is completed I need to compare the languages ( the native language cannot be the same as the language he wants to learn ).

I tried with a custom validator but $hook (which contains all the values submitted in the form) is not accessible. In a custom validator I can only get the value for a single field at a time which is stored in $value

So i tried with a preHook which is better because in preHooks we have the possibilty to access all the values of the form with $hook->getValues(). In preHooks I can compare the languages and send an error if an incorrect language has been detected.

However the problem is preHooks is executed after validation which is not appropriate in my case: if the user enters an incorrect value in let’s say both the “email” and the “native language” fields, the register page will reload and display an error solely for the “email” field, because if validation fails the preHook snippet is not executed at all.

Per chance does someone have a solution for this ?

You should be able to get multiple field values in a hook using:

$hook->getValue('field_name'); 

Hi BobRay, yes that’s right but the problem is preHook is executed after validation.

If the validation fails for the “email” field, preHook will never execute and my extra fields will never get validated. In other words, there will be an error message only for the email field. The extra fields will not display errors because they have not been validated by preHooks.

$hooks is only accessible in preHook or postHook, not in a custom validator

Maybe you could access $_POST directly in the custom validator to read the second value.

Thank you :slight_smile: yes you are right, I think I will have to use POST otherwise it would require extreme tweaking of the Register snippet.

Another way to go would be to use JavaScript to do your check on submission. You can abort the submit and pop up an error message if the check fails.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.