Jump to content

Fatal Error: Call To A Member Function Seterror() On A Non-Object In


raamay

Recommended Posts

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.

Edited by raamay
Link to comment
Share on other sites

try putting an else on the if(class_exists('Form')) that will warn you the class hasn't been found. see if that warning comes up.

i have tried to use

echo $form->num_errors;
$form->setError("name", "Enter name");
echo $form->error("name");

inside the class_exists() and i get the "0" and "Enter name" as the output which means the class is working before it is used inside the model class.

 

i tried the else statement also but it doesn't get executed. class_exists() returns true.

Edited by raamay
Link to comment
Share on other sites

  • 1 month later...

What trq meant is to modify the method validate to accept a $form argument like this

function validate($form)

and remove the line 'global $form;'

 

And when calling the validate method pass the $form object to it.

Edited by kareem3d
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.