jbrinley / wp-forms

An API for creating and processing forms in WordPress
101 stars 10 forks source link

Call to a member function get_validators() on a non-object #6

Closed emeraldjava closed 10 years ago

emeraldjava commented 10 years ago

Hey,

i keep getting this error

Fatal error: Call to a member function get_validators() on a non-object in C:\oconnellp\wamp\wordpress\wp-content\plugins\wp-forms-master\classes\WP_Form_Submission.php on line 110

1   0.0003  680776  {main}( )   ..\index.php:0
2   0.0004  685624  require( 'C:\oconnellp\wamp\wordpress\wp-blog-header.php' ) ..\index.php:17
3   0.0007  711720  require_once( 'C:\oconnellp\wamp\wordpress\wp-load.php' )   ..\wp-blog-header.php:12
4   0.0010  742544  require_once( 'C:\oconnellp\wamp\wordpress\wp-config.php' ) ..\wp-load.php:29
5   0.0016  876344  require_once( 'C:\oconnellp\wamp\wordpress\wp-settings.php' )   ..\wp-config.php:120
6   0.3729  60237384    do_action( )    ..\wp-settings.php:308
7   0.5309  73860472    call_user_func_array ( )    ..\plugin.php:406
8   0.5309  73860504    WP_Form_Listener->check_form_submission( )  ..\plugin.php:406
9   0.5320  73927632    WP_Form_Submission->is_valid( ) ..\WP_Form_Listener.php:20
10  0.5320  73927632    WP_Form_Submission->validate( ) ..\WP_Form_Submission.php:17

when i run this form class in my plugin. Just wondering can you spot any error with how i register my validator method?

class Raceday_Registration_Form{

        public function build_form(WP_Form $form) {
                $args = func_get_args();
                call_user_func_array(array($this, 'bhaaRegisterForm'), $args);
        }

        /**
         *
         * http://jsfiddle.net/kY5LL/12/
         * @param unknown $form
         */
        private function bhaaRegisterForm(WP_Form $form) {
                error_log('bhaaRegisterForm');

                $firstname = WP_Form_Element::create('text')->set_name('firstname')->set_label('First Name')->set_id('firstname');
                $lastname = WP_Form_Element::create('text')->set_name('lastname')->set_label('Last Name')->set_id('lastname');

                $submit = WP_Form_Element::create('submit')
                ->set_name('submit')
                ->set_label('Register Runner');

                $form->add_element( WP_Form_Element::create('number')
                                ->set_name('number')->set_id('number')
                                ->set_label('Race Number'));
                $form->add_element( WP_Form_Element::create('number')
                                ->set_name('runner')->set_id('runner')
                                ->set_label('BHAA ID'));
                $form->add_element($firstname);
                $form->add_element($lastname);
                $form->add_element($submit);
                $form->add_class('form-example');

                $form->add_validator(array($this,'bhaa_validation_callback'));
                $form->add_processor(array($this,'bhaa_processing_callback'));
        }

        public function filter_button_views( $decorators, $element ) {
                if ( $element->type == 'text' ) {
                        $decorators = array(
                                        'WP_Form_Decorator_HtmlTag' => array('tag' => 'div',
                                                        'attributes' => array( 'class' => 'control-group' )),
                        );
                }
                return $decorators;
        }

        public function bhaa_processing_callback( WP_Form_Submission $submission, WP_Form $form ) {
                $first_name = $submission->get_value('firstname');
                // do something with $first_name
                error_log('firstname '.$first_name);
                // redirect the user after the form is submitted successfully
                $submission->set_redirect('');//home_url('aPage'));
        }

        public function bhaa_validation_callback( WP_Form_Submission $submission, WP_Form $form ) {
                error_log('bhaavalidation_callback');
                //if ( $submission->get_value('first_name') != 'Jonathan' ) {
                //        $submission->add_error('first_name', 'Your name should be Jonathan');
                //}
        }
}```
jbrinley commented 10 years ago

You're setting up your validator callback correctly. The form itself is not getting passed into the Submission object. How are you hooking in your registration callback (i.e., where are you calling wp_register_form())? Are you hooked into 'wp_forms_register'? My initial suspicion is that you're registering the form too late.

emeraldjava commented 10 years ago

I was calling the wp_register_form() method, but i hadn't realised that i also needed to hook into the 'wp_register_form' action. Might be good to add more emphasis to these two steps in the documentation.