I'd like to do some additional validation with a custom function on signup field data on the server-side, not client-side javascript. I don't see a way to do that except using the userBeforeInsert hook. But, that doesn't seem right to me. Its not clear how to add validation rules to HTML_QF2. Can anyone point me in the right direction? Thanks
You should use onValidateSavedForm event. It accepts signup form values and form itself. And it is called when signup form is being validated: $event = new Am_Event_ValidateSavedForm($this->form->getValue(), $this->form); Am_Di::getInstance()->hook->call($event); if ($errors = $event->getErrors()) { $this->form->setError($errors[0]); return false; } return true;
site.php code PHP: // note - this works for both signup and profile formsfunction myValidateSavedForm(Am_Event_ValidateSavedForm $event){ $vars = $event->getForm()->getValue(); // all form vars as array if ($vars['login'] == 'hacker') $event->addError('We do not allow haCkerZ to signup');}Am_Di::getInstance()->hook->add(Am_Event::VALIDATE_SAVED_FORM, 'myValidateSavedForm');
Thank you... Using $event->getForm()->isSignup() is the best way to know which form I'm validating? I have a situation where I only want to validate the signup form and not the profile form for fraud detection.
If you are writing your own module or enhancing another to provide post-submit form validation, this code works well. It lets you iterate through an array of disallowed characters for any of the form vars you choose. PHP: /** * Checks certain signup variables to ensure they do not contain * invalid or disallowed characters. * * @param Am_Event_ValidateSavedForm $event * @return array $err|null */ function onValidateSavedForm(Am_Event_ValidateSavedForm $event){ $vars = $event->getForm()->getValue(); // If form vars don't contain a 'pass' element, skip over validation if (!array_key_exists('pass', $vars)) { return; } $err = ''; // Check password $disallowedChars = array('&', '\'', '"', '>', '<'); foreach ($disallowedChars as $disallowed) { if(strpos($vars['pass'], $disallowed) !== false){ $err .= "<br />Password may not contain character: ".$disallowed; } } // If there is at least one error, add it so the form returns with the error if ($err) { $event->addError($err); } }