Form elements not rendering with view pick

Hello Guys,

I've got a problem with phalcon in my project and I'm sure this is a issue.

I got a controller where I'm using $this->view->pick(); to pick the right view. It is rendering the right view and it's working. But on the page I got a form-element, but this form-element doesn't render.

So I debugged the code and set an exit; on the end of the view file with the form element. In this case the form element will shown. I found out, that the problem issued when the $this->getContent(); method is finished. When I set exit; directly after this method, the form element is deleted.

For testing I deleted all javascript includes to prevent that it will be deleted by javascript code.

Little bit code (the view):

   <div class="col-md-8 no-padding no-margin">
    <div class="col-md-12 big-content">
        <div class="panel panel-default panel-teal">
            <div class="panel-heading">
                <h1 class="panel-title"><?= $lang->_('me.settings'); ?></h1>
            </div>
            <div class="panel-body">
                <?= $this->flash->output(); ?>
                <!-- Will not be shown --><?= $this->tag->form(['me/settings/store', 'method' => 'POST', 'role' => 'form', 'class' => 'form-horizontal']); ?>
                <div style="display:none;">
                    <?= $settingsForm->render('csrf'); ?>
                </div>
                <div class="form-group">
                    <label for="settings-mail" class="col-lg-3 control-label"><?= $lang->_('me.settings.email'); ?></label>
                    <div class="col-lg-9">
                        <?= $settingsForm->render('settings-mail'); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="settings-block-newfriends" class="col-lg-3 control-label"><?= $lang->_('me.settings.blocknewfriends'); ?></label>
                    <div class="col-lg-9">
                        <div class="checkbox">
                            <label>
                                <?= $settingsForm->render('settings-block-newfriends'); ?>
                            </label>
                        </div>
                    </div>
                </div>
                <!-- Will not be shown --><?= $this->tag->endForm(); ?>
            </div>
        </div>
    </div>
</div>
<div class="col-md-4 no-padding no-margin">
    <div class="col-md-12 small-content">
        <?= $this->partial('shared/static-buttons'); ?>
    </div>
   </div>

Template View (layout folder):

<?= $this->partial('shared/navigation'); ?>
<?= $this->partial('shared/subnavigation'); ?>

<div class="container content">
    <?php echo $this->getContent(); ?><!-- After this, form elements are deleted -->
</div>

Controller (will be extended in following controller):

/**
 * ControllerBase
 */
class SubControllerBase extends ControllerBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function beforeExecuteRoute() {
        $ns = $this->dispatcher->getNamespaceName();
        $c = $this->dispatcher->getControllerName();

        $this->view->pick($ns . '/' . $c);
    }

}

The controller:

namespace Me;

use SubControllerBase;
use SettingsForm;

/**
 * SettingsController
 */
class SettingsController extends SubControllerBase
{

    public function initialize()
    {
        parent::initialize();

        $this->createSubNavigation('me', 'settings');
    }

    public function indexAction()
    {
        $this->tag->appendTitle($this->lang->_('me.settings'));
        $settingsForm = new SettingsForm();
        $settingsForm->setLang($this->lang);

        $this->view->setVar('settingsForm', $settingsForm);
    }

    public function storeAction()
    {

    }

}

Result - you see: No form-element. Wtf?:

Result-HTML

edited Dec '14

I'm doing kind of the same thing except I rely Phalcon MVC do the view picking generally, only when I want to reuse the same view, like on a add and edit actions, I pick the same view to render. I'm not sure its the best way at all, but the form renders correctly in both actions.

Phalcon view structure

  • view (folder)
    • users (folder)
      • add.phtml
      • edit.phtml
    public function addAction()
    {
        // Use the edit.phtml view instead of the add.phtml view 
        $this->view->pick("users/edit");

        // Create Form etc ...
    }

Edit: Here is how the view directory is set, got this either from the documentation or one of the example projects:

    // Registering the view component
    $di->set('view', function() {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir(__DIR__ . '/views/');           
            \Phalcon\Tag::setDocType(\Phalcon\Tag::HTML5);
            return $view;
    });


2.6k
edited Dec '14

Thanks for your answer, Edward. You're right, I tried to get my project work with automatic picking my views. Unfortunately I have a different logic. I'm using 2 folders for both controller and namespace.

For example: I got a controller called MeController. This controller got his view folder in views/me. Now I go to the next page. The next page got the route like this: http://domain/me/settings. I don't want to execute the settingsAction in MeController, but a new SettingsController. That's what I realized with the phalcon router configuration. But my problem is: I don't want to have a new folder in views for every Controller at http://domain/me/*, but a folder in views/me. Because I got the same with community for example. So when I got a controller SettingsController at http://domain/community/settings the problem is, that I've got 2 folder with same name.

Maybe I have to try to get this work without manually picking the view.

EDIT: I got this to work with $this->view->setViewsDir but now it can't get access to my shared folder in views dir. How can I resolve this?

EDIT2: Ok ... don't work :( Now it picks the right views. I used this example by phalcon: https://github.com/phalcon/cphalcon/issues/692 I created a plugin like the example in the link. It picks the right view, but when the layout (in layout folder) will be executed, the form elements are away.

EDIT3: After hours and hours playing around with the automatic picking of views I give up. I went back to the manually picking views. This is working perfect, but still not show these form elements in html :( The strange thing is, even when I write normal html <form> it doesn't work. Wtf? I just created a new phalcon project to reproduce this. In the new project it is working ... So I have to find my issue in the project. But I don't know where I have to start ....

EDIT4: I got it. It was a javascript. I disabled this for test, but the cache was still executing this.... omg I just felt dumb.

MfG Yannici

So sorry I pointed you at the wrong direction.