Search code examples
phpjoomlajoomla-extensions

Joomla PHP API get error messages for logging


I am accessing the Joomla PHP API with my own scripts, and I've written this simple registration function, that is called from a mobile application.

/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();

require_once(JPATH_BASE.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php');

$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
jimport('joomla.user.helper');
$language = JFactory::getLanguage();
$language->load('com_users', JPATH_SITE);

$username = JRequest::getVar('username', '', 'method', 'username');
$name = JRequest::getVar('name', '', 'method', 'name'); 
$email = JRequest::getVar('email', '', 'method', 'email'); 
$password = JRequest::getVar('password', '', 'method', 'password');

$data = array( 
'username' => $username,
'name' => $name,
'email1' => $email,
'password1' => $password, // First password field
'password2' => $password, // Confirm password field
'block' => 0);


if($model->register($data) == "useractivate") {
    print "Registered";
    //log successfull registration data maybe

}
else {
    print "Failed to register";
    //DEFINITELY LOG WHY IT FAILED
}

Any ways to get the error while a registration can fail to happen, in the else part?

Does the $model contain any data regarding this?


Solution

  • If I am getting correctly then you are looking for getErrors function.

    // Get the validation messages.
    $errors = $model->getErrors();
    
    // Push up to three validation messages out to the user.
    for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
    {
        if ($errors[$i] instanceof Exception)
        {
            $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
        }
        else
        {
            $app->enqueueMessage($errors[$i], 'warning');
        }
    }