raamay Posted December 19, 2012 Share Posted December 19, 2012 (edited) 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 December 19, 2012 by raamay Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/ Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1400276 Share on other sites More sharing options...
raamay Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) 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 December 19, 2012 by raamay Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1400298 Share on other sites More sharing options...
trq Posted December 19, 2012 Share Posted December 19, 2012 You should pass this object into your method via an argument. Globals break the encapsulation provided. Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1400305 Share on other sites More sharing options...
raamay Posted December 20, 2012 Author Share Posted December 20, 2012 You should pass this object into your method via an argument. Globals break the encapsulation provided. can you cite an example please! Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1400563 Share on other sites More sharing options...
Christian F. Posted December 20, 2012 Share Posted December 20, 2012 (edited) http://php.net/manual/en/functions.arguments.php Works the same for regular functions as it does for functions (methods) within a class. Edited December 20, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1400573 Share on other sites More sharing options...
kareem3d Posted January 23, 2013 Share Posted January 23, 2013 (edited) 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 January 23, 2013 by kareem3d Quote Link to comment https://forums.phpfreaks.com/topic/272174-fatal-error-call-to-a-member-function-seterror-on-a-non-object-in/#findComment-1407754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.