Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Class Form interest

Hi guys,

I'm pretty sure it's a dumb question, but I didn't find any answers. I just dicovered Phalcon Php a week ago. I used very often Symfony 2, but Phalcon and Symfony don't have exactly the same architecture. In the two first Tutorials from the documentation, there is a form but the class Phalcon/Form isn't used.

In fact, I'm trying to understand where to put my forms class in my project (I asssume the purpose of this kind of use, is for reuse)

Thanks for answering this question

Have a nice day



5.7k
edited Jul '14

Hello, I'm beginner too.

Maybe, to help you there's: http://docs.phalconphp.com/en/latest/reference/forms.html

But I don't understand how use it.

I created a form, but I can't send her into view.

If you can use this doc, please tell me how you did it.

edited Jul '14

Hi, I totally understood this part of the documentation but where do I put my forms file in the architecture ? /app/Forms/myForm ?



5.7k
Accepted
answer
edited Jul '14

Maybe, use a namespace?

/app/forms/myForm.php

namespace 'MyApp\Forms'

use Phalcon\Forms\Form;

class AdminForm extends Form{
    ...
}

And register the namespace in the loader:

$loader->registerNamespaces(array(
    'MyApp\Forms'=>__DIR__.'/app/forms/'
))->register();

I think it's your decision (choose path of directories):

And use them in controller:

use MyApp\Forms;

class IndexController extends ControllerBase{

    public function IndexAction(){
        $form = new AdminForm();
    }

}

Please show me your code, how to use Forms.

edited Jul '14

I don't have any code at the moment but I will as soon as I start developing it ;) Thanks for your answer



5.7k
edited Jul '14

If you understand the docs, please tell me how return the form into view.

edited Jul '14

I assume you must use this in your controller

<?php echo $form->render('name') ?>



17.9k

@brieucdnl

The code below:


<?php echo $form->render('name') ?>

should not be in a controller. It should be in a view.

edited Jul '14

Ok thanks good to know I've some bad habit from Symfony, and I'll use easily Volt, so I won't put Php in my views



5.7k

Not a problem :)

In controller:

$this->view->adminForm = new AdminForm(); // AdminForm returns the class Form

In view(volt):

{{ adminForm.render('someElement') }}