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

Not rendering form in volt

Hi guys!

Tell me please, where error:

    public function categoryEditAction()
    {
        $cid = $this->dispatcher->getParam('id');

        $category = Categories::findFirst( $cid );

        $form = new Form( $category );

        $form->add(new Text("name"));

        $this->forms->set('categoryEdit', $form);

    }
{{ form.render('categoryEdit') }}

returned error Notice: Undefined variable: form in ...

I havent used the form yet, dont you have to put $this->view, so the virable or array can be found in the view? Correct me if i'm wrong.

edited Sep '15

i use formManager. i need added variable $form to view anyway?

$di->set('forms', function(){
        return new Phalcon\Forms\Manager();
});

Like i said, i havent used forms yet.. But maybe you could try?

edited Sep '15

You are setting forms in $di then you should be using that instead of form. But can you show me your Form.php?

i use formManager. i need added variable $form to view anyway?

$di->set('forms', function(){
      return new Phalcon\Forms\Manager();
});

code written above it all I have. I thought forms can be created from the data model. but maybe I was wrong. or just not right trigger in volt. If you are difficult to give a simple example of the display form of the model in volt.

Thank you and sorry for my english.



16.1k
Accepted
answer
edited Sep '15

You need a form similar to this:

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;

class UserForm extends Form
{

    public function initialize($entity = null, $options = null)
    {
      if (isset($options['edit']) && $options['edit']) {
          $id = new Hidden('id');
      } else {
          $id = new Text('id');
      }

      $this->add($id);

      $firstname = new Text('firstname', array(
          'placeholder' => 'First Name'
      ));

      $firstname->addValidators(array(
          new PresenceOf(array(
              'message' => 'The first name is required'
          ))
      ));

      $lastname = new Text('lastname', array(
          'placeholder' => 'Last Name'
      ));

      $lastname->addValidators(array(
          new PresenceOf(array(
              'message' => 'The last name is required'
          ))
      ));

      $this->add($firstname);
      $this->add($lastname);

      $email = new Text('email', array(
          'placeholder' => 'Email'
      ));

      $email->addValidators(array(
          new PresenceOf(array(
              'message' => 'The e-mail is required'
          )),
          new Email(array(
              'message' => 'The e-mail is not valid'
          ))
      ));
      $this->add($email);
     }
}

And then you can call this form in your controller like $form = new UserForm(); as referenced here: https://docs.phalcon.io/en/latest/reference/forms.html

Thank you Bryan, I will try ...

I just thought that Falcon model can lead to a form.