hello, i have a Form Class as below:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'configuration.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
class Form
{
var $values = array();
var $errors = array();
var $num_errors;
function Form(){
if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
$this->values = $_SESSION['value_array'];
$this->errors = $_SESSION['error_array'];
$this->num_errors = count($this->errors);
unset($_SESSION['value_array']);
unset($_SESSION['error_array']);
}
else{
$this->num_errors = 0;
}
}
function setValue($field, $value){
$this->values[$field] = $value;
}
function setError($field, $errmsg){
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors);
}
function value($field){
if(array_key_exists($field,$this->values)){
return stripslashes($this->values[$field]);
}else{
return "";
}
}
function error($field){
if(array_key_exists($field,$this->errors)){
return "<font size=\"1\" color=\"#ff0000\">".$this->errors[$field]."</font>";
}else{
return "";
}
function getErrorArray(){
return $this->errors;
}
}
}
Now i am trying to use it in a joomla model for server-side validation. I have included and instantiated the class as below:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
JLoader::register('Form', dirname(__FILE__) . DS .'form.php');
include_once (JPATH_COMPONENT.DS.'models'.DS.'form.php');
if (class_exists('Form')) {
$form = new Form();
}
jimport('joomla.application.component.model');
Class JobsModelJobs extends JModel {
.
.
.
.
function validate(){
global $form;
$name = JRequest::getVar('name', 'POST');
$email = JRequest::getVar('email', 'POST');
$field = "name";
if(!$name || strlen($name = trim($name)) == 0){
$form->setError($field, "Please enter your name!");
}
$_SESSION['value_array'] = JRequest::get( 'POST' );
$_SESSION['error_array'] = $form->getErrorArray();
}
}
After this, I have tried to invoke the function setError() and getErrorArray() of the Form class in validate() function of the model but i get the error "Fatal error: Call to a member function setError() on a non-object in".
Can anybody tell me where i have gone wrong or what i am missing?
The issue is relevant to this post http://forums.phpfreaks.com/topic/175054-php-object-error-problem/. The only diffrerence is that i am not using session class and relying on joomla.