Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Namespaces and fieldset
  • I wanted to add a few fieldset functions of my own, so I extended the core class Fieldset as below (which doesn't do much as shown) and added the appropriate line to Autoloader::add_classes() in bootstrap.php
    class Fieldset extends \Fuel\Core\Fieldset
    {
     public function my_show_errors()
     {
      $html = parent::show_errors();
      return $html;
     }
    }
    
    After doing this, I receive the following error:
    ErrorException [ 4096 ]: Argument 1 passed to Model_News::set_form_fields() must be an instance of Fieldset, instance of Fuel\Core\Fieldset given And the function it's referring to looks like:
    public static function set_form_fields(Fieldset $form, $news_id = null) If I attempt to fix this by:
    public static function set_form_fields(\Fuel\Core\Fieldset $form, $news_id = null) Then the error occurs at other places such as the Fieldset constructor. Anyone know what I'm doing wrong?
  • This should have been a bugreport, but I fixed it: looks like there were a couple of mistakes in the class calls in the core. If you pull/download the latest commits from fuel/core develop branch this should work just fine.
  • Thanks for reviewing this. I would have filed a bug report, but being such a Fuel novice, I expect that most of the time it's me doing something stupid and not an actual bug.
  • Hmm, I downloaded the new fuel.php file. The first thing I noticed was that the constant DEVELOPMENT was changed from 'dev' to 'development'. After fixing that, I'm still getting the same error: ErrorException [ 4096 ]: Argument 1 passed to Model_News::set_form_fields() must be an instance of Fieldset, instance of Fuel\Core\Fieldset given
  • Hmm that's weird, could you show how you create the fieldset and where you assign it? It should create an instance of Fieldset and not \Fuel\Core\Fieldset...
  • This goes in app/classes/fieldset.php:
    class Fieldset extends \Fuel\Core\Fieldset
    {
     public function my_show_errors()
     {
      $format = array(
       'open_list'    => '<div class="form-errors error"><p class="form-error-heading">There were errors in your form:</p><ol>',
       'close_list'   => '</ol></div>',
       'open_error'   => '<li>',
       'close_error'  => '</li>',
       'no_errors'    => '',
      );
    
      $html = parent::show_errors($format);
      return $html;
     }
    }
    
    This goes in app/bootstrap.php:
    Autoloader::add_classes(array(
     // Add classes you want to override here
     // Example: 'View' => APPPATH.'classes/view.php',
     'Security' => APPPATH.'classes/security.php',
     'Session' => APPPATH.'classes/session.php',
     'Fieldset' => APPPATH.'classes/fieldset.php',
    ));
    
    This goes in app/classes/mode/news.php:
    public static function set_form_fields(Fieldset $form, $news_id = null)
    &#123;
        // Calls to $form->add() go here
    }
    
    Let me know if you need any further info.
  • I noted that error too, running RC2.1 right now.
    When you extend the Fieldset class, you'll also need to override the factory method.
    Actually, after copying the same factory method from the super-class (Fuel\Core\Fieldset), everything worked like a charm. I actually wonder why can't we just extend this one like other core classes. It's probably some issue with the namespace.. Maybe someone's got the answer? Hope it helped...
  • You're right. I copied the code for the factory function from the core to my extended class and it worked. It does seem like that shouldn't be necessary.
  • You must have grabbed from the core master branch, in the latest develop I've fixed it to use "new static()" instead of "new Fieldset()"
  • I just downloaded the latest core code from the develop branch, and everything seems to work. Again, thanks for all your help.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion