473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

error even after instantiating the class

107 100+
hello everyone, i have a class for form validation as below:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. defined( '_JEXEC' ) or die( 'Restricted access' );
  3.  
  4. require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  5. require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
  6. require_once ( JPATH_BASE .DS.'configuration.php' );
  7.  
  8. $mainframe =& JFactory::getApplication('site');
  9. $mainframe->initialise();
  10.  
  11. class Form
  12. {
  13.    var $values = array();
  14.    var $errors = array(); 
  15.    var $num_errors;  
  16.  
  17.    function Form(){
  18.  
  19.       if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
  20.  
  21.          $this->values = $_SESSION['value_array'];
  22.          $this->errors = $_SESSION['error_array'];
  23.          $this->num_errors = count($this->errors);
  24.  
  25.          unset($_SESSION['value_array']);
  26.          unset($_SESSION['error_array']);
  27.       }
  28.       else{
  29.  
  30.          $this->num_errors = 0;
  31.       }
  32.    }
  33.  
  34.    function setValue($field, $value){
  35.  
  36.       $this->values[$field] = $value;
  37.  
  38.    }
  39.  
  40.    function setError($field, $errmsg){
  41.  
  42.       $this->errors[$field] = $errmsg;
  43.  
  44.       $this->num_errors = count($this->errors);
  45.  
  46.    }
  47.  
  48.    function value($field){
  49.  
  50.       if(array_key_exists($field,$this->values)){
  51.  
  52.          return stripslashes($this->values[$field]);
  53.  
  54.       }else{
  55.  
  56.          return "";
  57.  
  58.       }
  59.  
  60.    }
  61.  
  62.    function error($field){
  63.  
  64.       if(array_key_exists($field,$this->errors)){
  65.  
  66.          return "<font size=\"1\" color=\"#ff0000\">".$this->errors[$field]."</font>";
  67.  
  68.       }else{
  69.  
  70.          return "";
  71.  
  72.       } 
  73.  
  74.    function getErrorArray(){
  75.  
  76.       return $this->errors;
  77.  
  78.    }
  79. }
  80. }
  81.  
Now i have instantiated the class in joomla model like below:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. defined( '_JEXEC' ) or die( 'Restricted access' );
  3. JLoader::register('Form', dirname(__FILE__) . DS .'form.php');
  4.  
  5. if (class_exists('Form')) {
  6.     $form = new Form();
  7.     }
  8. jimport('joomla.application.component.model');
  9.  
  10. Class JobsModelJobs extends JModel {
  11. .
  12. .
  13. .
  14. .
  15. function validate(){
  16.       global $form;
  17.  
  18.       $name = JRequest::getVar('name', 'POST');
  19.       $email = JRequest::getVar('email', 'POST');
  20.  
  21.       $field = "name";
  22.       if(!$name || strlen($name = trim($name)) == 0){
  23.          $form->setError($field, "Please enter your name!");
  24.  
  25.       }
  26.       $_SESSION['value_array'] = JRequest::get( 'POST' );
  27.       $_SESSION['error_array'] = $form->getErrorArray();
  28.  
  29.    }       
  30. }
  31.  
I have tried to invoke the function setError() and getErrorArray() of the form class in my current function but i get the error "Fatal error: Call to a member function setError() on a non-object in".

Please tell me why the function is not recognized by the current class even after instantiating?
Dec 19 '12 #1
1 1623
Rabbit
12,516 Expert Mod 8TB
It might be because you redeclare your form variable in your validate function.
Dec 19 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Richard | last post by:
Hello, Where can I transmit an internal error in Crimson? I have written a simple java class that transforms an XML file and writes it in a file. It throws an error I don't understand. I...
17
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment...
1
by: Nita Rai | last post by:
I am getting 'error: too few template-parameter-lists' (GCC -3.4.2 ) for instantiating static data members of template class. When I prefixed template <> for the instantiation GCC accepted it.But...
2
by: Stick | last post by:
I have defined to classes like this: namespace PanelColors.Helpers { public class PanelColor { public PanelColor() { r = 0; g = 0; b = 0; }
5
by: nmb3000 | last post by:
Java 1.5 I'm not sure if this is possible, but I'd like to get the class name of the class that instantiated my class. For example: public class Foo { public static void main(String args) {...
18
by: RB | last post by:
Hi guys (and gals!), I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits from "TypesafeConstant", and adds no new functionality. All "Color" does is to instantiate some class...
2
by: yeah | last post by:
Hi friends, i am new to c++ programming,when i compile c++ program got the error as " not a class or struct name .I added all the headers files it asked when compiling, but still getting error....
8
by: Snaggy | last post by:
Hi, this is my first day using c# and I must say it's cool but data types.. I'm not use to them (php...) they're driving me crazy. I need to declare a class variable, but I don't know it's type...
6
by: Patient Guy | last post by:
I am a newcomer to using PHP but not to programming (C, C++, Javascript). I am playing around with classes and wanted to make a function that has a method simply for producing either plain text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.