1. Make sure you are creating
static event listener functions.
2. Heres a good start:
The controller for the registration form is located at
/library/XenForo/ControllerPublic/Register.php, whose
actionRegister() method is called when you submit the registration form. So you need to override this particular method only and perform your own validation before letting the registration continue as usual.
Create a new file:
/library/GeekPoint/ControllerPublic/Register.php where you'll override the
actionRegister() method and perform your own validations...
PHP:
<?php
class GeekPoint_ControllerPublic_Register extends XFCP_GeekPoint_ControllerPublic_Register
{
public function actionRegister()
{
$errors = array();
$data = $this->_input->filter(array(
'username' => XenForo_Input::STRING,
'email' => XenForo_Input::STRING,
'timezone' => XenForo_Input::STRING,
'gender' => XenForo_Input::STRING,
'dob_day' => XenForo_Input::UINT,
'dob_month' => XenForo_Input::UINT,
'dob_year' => XenForo_Input::UINT,
));
if (!$this->_validateRegistration($data))
{
// Add errors to display to the end user (See note #3)
$errors[] = "Epic Fail";
}
if ($errors)
{
// Return the invalid data along with the errors
return $this->_getRegisterFormResponse($data, $errors);
}
// Reached here? Continue the registration...
return parent::actionRegister();
}
protected function _validateRegistration($data)
{
// Do your processing & validation here
return false;
}
}
Next, we'll create a code event listener that would listen to the
load_class_controller event and hook our class accordingly. Create a file
/library/GeekPoint/EventListener/Register.php with the given code:
PHP:
<?php
class GeekPoint_EventListener_Register
{
public static function listen($class, array &$extend)
{
if ($class == 'XenForo_ControllerPublic_Register')
{
$extend[] = 'GeekPoint_ControllerPublic_Register';
}
}
}
We're done with the PHP part. All we need to do next is register our event listener via the AdminCP. Go to: AdminCP -> Development -> Code Event Listeners -> Create New Code Event Listener; and enter the following values:
Listen to event:
load_class_controller
Execute Callback:
GeekPoint_EventListener_Register ::
listen
[Save Event Listener]
Done! Visit: /your_board/register/ and submit the form to see this in effect.
Notes:
1. You don't need to follow the directory structure I gave above. You can create your file in any location you want in
/library/ as long as the autoloader is able to recognize and load it. But it's easier if you follow the conventions.
2. For the class you want to extend via any
load_class_* event, you need to
extend a non-existent proxy class with the name: XFCP_
your_own_classname, and XenForo will take care of everything else.
3. Use the inbuilt language/phrase system instead of directly embedding text in your scripts. So use:
$errors[] = new XenForo_Phrase('epic_fail'); in place of
$errors[] = "Epic Fail";. This would come handy when you want to change the text later or provide translations of your script in other languages.
Hope it helps.