我使用自己的脚本访问Joomla PHP API,并且我已经编写了这个简单的注册函数,该函数是从移动应用程序调用的。
/* 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
}
在注册可能无法发生错误的任何方法中,在else部分?
$ model是否包含有关此内容的任何数据?
答案 0 :(得分:1)
如果我正确,那么你正在寻找getErrors功能。
// 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');
}
}