We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Form :: Notice: Undefined variable: form in and Fatal error: Call to a member function render() on a non-object in

Hi Expert, having problems about Phalcon\Forms\form after submitting data to form got error

(the form) bizRegisterForm.php

<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Select;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;

class bizRegisterForm extends Form
{
  public function initialize($entity = null, $options = null)
  {
    $businessname = new Text('businessname', [
      'placeholder' => 'Use Your Business Trading name'
    ]);
    $businessname->setFilters(['string']);
    $businessname->addValidators([
      new PresenceOf([
        'message' => 'The Business Name is required'
        ])
    ]);
    $this->add($businessname);

    $businessemail = new Text('businessemail', [
      'placeholder' => 'e.g. [email protected]'
    ]);
    $businessemail->setFilters(['email']);
    $businessemail->addValidators([
      new PresenceOf([
        'message' => 'The business e-mail is required'
        ]),
      new Email([
        'message' => 'The business e-mail is not valid'
        ]),
    ]);
    $this->add($businessemail);
  }
}

The Controller

<?php
class BusinessController extends ControllerBase
{
  public function indexAction()
  {
      $form = new registerForm();
     // other code ../
     $this->view->form = $form
  }

  public function registerAction()
  {
    $registerform = new bizRegisterForm;
    if($this->request->isPost())
    {
      $bussiness->business_name     = $this->request->getPost('businessname');
      $bussiness->phone             = $this->request->getPost('businessphone');
     // other code ...
      if($bussiness->save())
      {
        $this->flash->success('fyes please');
      }
      $this->flash->error($bussiness->getMessages());
    }
    $this->view->form = $registerform;
  }
}

register.volt

{{form('id':'register-form', 'class':'form')}}
        <div class="form-group">
         {{form.render("businessname")}}
        </div>
    <div class="form-group">
    {{form.render('businessphone')}}
    </div>
    // the rest of the code
    <div class="form-group">
         <button autocomplete="off" class="button button-full button-primary" type="submit">
          <span class="button-text">Next</span>
         </button>
        </div
{{end_form()}}

Submited form got this error Notice: Undefined variable: form in blabla balb Fatal error: Call to a member function render() on a non-object in same line any advice from the expert very appreciated



134

Looks like you have return statement before view var "form" assignment. Try to move $this->view->form = $registerform; before if($this->request->isPost()) if you didn't changed $registerform there



1.7k
edited Jan '16

Looks like you have return statement before view var "form" assignment. Try to move $this->view->form = $registerform; before if($this->request->isPost()) if you didn't changed $registerform there

Thank you @Boris for fast respons and apologize for late replay. yep this is work like a charms now BUT the form cannot work anymore for full syntax

  public function registerAction()
  {

    /** Get session info */
    $auth             = $this->session->get('auth');
    /** Query the active user */
    $user             = Users::findFirst($auth['id']);
    /** get register form @ bizRegisterForm*/
    $registerform     = new bizRegisterForm();
    // tHE BORIS way :) IT WORKS
    $this->view->form = $registerform;
    //But this form not work anymore
    if($this->request->isPost())
    {
      /** get Business model from Business model */
      $bussiness                    = new Business();
      $bussiness->users_id          = $user->id;
      $bussiness->business_name     = $this->request->getPost('businessname');
      $bussiness->phone             = $this->request->getPost('businessphone');
      $bussiness->email             = $this->request->getPost('businessemail');
      $bussiness->suburb            = $this->request->getPost('businesssuburb');
      $bussiness->business_state_id = $this->request->getPost('businessState');
      $bussiness->postcode          = $this->request->getPost('postcode');

      if($bussiness->save())
      {
        /**saving and redirect...*/
        return $this->response->redirect('complete-business');
      }
      $this->flash->error($bussiness->getMessages());
    }
  }

Usnig phalcon 2.0.9 any advise from the Expert very appreciate



134

Since you are using form, there are no need to bind variables to model manually, you can simply do it like that way: (For user binding you can use form callback beforeValidation()/afterValidation() or bind it after form validation)

<?php

    public function registerAction()
    {
        /** Get session info */
        $auth = $this->session->get('auth');
        /** Query the active user */
        $user = Users::findFirst($auth['id']);
        /** get register form @ bizRegisterForm*/
        $registerForm = new bizRegisterForm();

        if ($this->request->isPost()) {

            $business = new Business();

            $registerForm->bind($this->request->getPost(), $business);

            if ($registerForm->isValid()) {
                $business->user_id = $user->id;

                if ($business->save()) {
                    $this->flash->error($business->getMessages());
                } else {
                    return $this->response->redirect('complete-business');
                }

            } else {
                foreach ($registerForm->getMessages() as $message) {
                    $this->flash->error($message->getMessage());
                }
            }
        }

        $this->view->form = $registerForm;
    }