Can't use $this->form()->***() helpers in my project - zend-form

I specified list of zend components in my composer.json
"zendframework/zend-xmlrpc": "2.0.*",
"zendframework/zend-config": "2.0.*",
"zendframework/zend-log": "2.0.*",
"zendframework/zend-db": "2.0.*",
"zendframework/zend-inputfilter": "2.0.*",
"zendframework/zend-json": "2.0.*",
"zendframework/zend-form": "2.0.*",
"zendframework/zend-mvc" : "2.0.*",
"zendframework/zend-session" : "2.0.*",
"zendframework/zend-view" : "2.0.*"
But when I try to output form inside view script:
<?php
use Zend\Form\Form;
use Zend\Form\Element;
$form = new Form();
$form->setAttribute('action', '/contact/process');
$form->setAttribute('method', 'post');
$form->prepare();
echo $this->form()->openTag($form);
echo $this->form()->closeTag();
?>
I get exception:
ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for form
#in /var/www/projectdir/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/ServiceManager.php line 452
#at ServiceManager->get('form', true) in /var/www/projectdir/vendor/zendframework/zend-servicemanager/Zend/ServiceManager/AbstractPluginManager.php line 110
#at AbstractPluginManager->get('form', null) in /var/www/projectdir/vendor/zendframework/zend-view/Zend/View/Renderer/PhpRenderer.php line 340
#at PhpRenderer->plugin('form') in /var/www/projectdir/vendor/zendframework/zend-view/Zend/View/Renderer/PhpRenderer.php line 359
#at PhpRenderer->__call('form', array()) in /var/www/projectdir/views/editWorker.phtml line 19
#at PhpRenderer->form() in /var/www/projectdir/views/editWorker.phtml line 19
.....
But other views helpers such as $this->htmlList($items) work properly

The form view helpers are all located in the Zend\Form\View\Helper namespace. There is a separate config where all form view helpers are injected in the view helper plugin manager.
Usually this happens automatically. At least, when you require "zendframework/zendframework" and not all individual components this works out of the box. In the Zend\Mvc namespace there is a special factory which helps to instantiate the view helper's plugin manager. It also tries to inject view helpers from other components: the navigation, i18n and form view helpers.
That being said, there are either two options where this went wrong for you:
The factory does a class_exists() call on those additional view helper configs. If the class Zend\Form\View\HelperConfig cannot be found due to autoloading issues, the form view helpers are not initialized.
The factory is not even called. If you use Zend\Mvc\Application this can be a bug and please provide some more info about your case so others can reproduce it. If you do not use the Application, you have to wire these kind of things manually.
In the case of the latter and you want to wire the stuff yourself, instantiate the helper config, grab the view helper manager and inject it:
// $renderer is the Zend\View\Renderer\PhpRenderer
$plugins = $renderer->getHelperPluginManager();
$config = new Zend\Form\View\HelperConfig;
$config->configureServiceManager($plugins);

Related

Controllers sharing parts: How to include a controller's output from within another controller's view in Prestashop >= 1.5?

In the shop's customer section I would like to render the user account menu (which we can see by default when reaching /my-account URL) as a side column on some others controllers like the ones associated to "/my-adresses", "/identity" pages ..
I thought I would have to create another controller which purpose would be to gather menu infos and only render the menu <ul> list. Then I could override Controllers such as MyAccountController, IdentityController to include this former Ctrl and then render its content as part of the views of those two other controllers views.
So how one can load a specific controller from another in order to render shared views between pages ? Which is the right/clean way to do that ?
I heard about $this->getController() but I did not find any snippet or implementation of what I'd like to achieve. I new to Prestashop but even if the code seems clear, I don't get the point here.
Thank you !
After getting a bit deeper into sources, I ended up moving the menu (initially part of the MyAccountControllerCore alias my-account template) into a brand new Controller "MyAccountMenuController", in /override/controllers/front/.
<?php
// In /override/controllers/front/MyAccountMenuController.php
// The "exposer" controller
public function display()
{
// Do what ever you want to pass specific variables
if (! $this->template) {
throw new Exception(get_class($this) . '::display() : missing template.');
}
$this->context->smarty->display($this->template);
return true;
}
I am now able to import this menu by adding the following snippet inside the initContent() method of each controller that are part of the customer account section (this means that each of them must be overridden, for more info about overriding controllers, see documentation):
<?php
// In /override/controllers/front/MyAccountController.php
// The "consumer" controller
public function initContent() {
// ...
// Importing the customer area's menu
$menuController = $this->getController('MyAccountMenuController');
ob_start();
$menuController->run();
$this->context->smarty->assign('myAccountMenu', ob_get_clean());
}
I don't think the original purpose of getController method (located in ControllerCore class) was meant to include another controller output, at least in Prestashop 1.5. Still, to me this approach is far cleaner than duplicate views code.
If you have a better (cleaner) approach to implement such a mechanism, please let me know !
Any thoughts ?

FluentBootstrap - Acces to the form object from partial views

I have a view that renders a form with code similar to the following:
#using( var form = Bootstrap.Form().SetHorizontal( 3 ).AddCss( Css.ColSm8, Css.ColMdOffset2 ).Begin() )
{
#form.DisplayFor( m => m.Name )
// bla bla bla
#Html.Action( "Details", "Fare", new { entity = Model.FareId } )
}
How can I access the form object in the partial view, so that the same layout is applied to the whole form?
A lot of work was spent ensuring that the Bootstrap control stack would carry-over into partial views. You have two options here:
The first is to just pass the form object to the partial/action as part of the model. In your case, you would just add it as another property in the anonymous model object you're sending to the action.
You don't need to use the form instance to make FluentBootstrap recognize you're in a form. It's just a convenience to make calling the extensions appropriate to a form easier. You can also just call something like Bootstrap.DisplayFor(x => x.Name) right from the global Bootstrap object in your partial and it will respect any settings you've placed in the containing form defined in the containing view.

submit form from one model to another view in Yii

How do I post from one controller into another view?
I have a Review model and a Product model. The Review form is displayed in the Product view through a widget, but how do I submit the form itself? Right now, it doesn't do anything. I can submit through review/create, but not through the Product View.
Or am i suppose to do the post in the widget?
You can achieve it if you put code like below on components/ReviewWidget.php . I supposed you have Review as model and its respective controller and views file on default locations.
<?php
class ReviewWidget extends CWidget{
public function init() {
return parent::init();
}
public function run(){
$model = new Review;
if (isset($_POST['Review'])) {
$model->attributes = $_POST['Review'];
$model->save();
}
$this->renderFile(Yii::getPathOfAlias('application.views.review'). '/_form.php',array(
'model' => $model,
));
}
}
Then, call above widget on any where on view like below ,
<?php $this->widget('ReviewWidget'); ?>
It will handle item creation only. You have to create code to item update by yourself.
In your controller action you must use function renderPartial
$this->renderPartial('//views/reviw/_form',array('data' => $data ) );
First argument of this function is used to determine which view to use:
absolute view within a module: the view name starts with a single slash '/'. In this case, the view will be searched for under the
currently active module's view path. If there is no active module,
the view will be searched for under the application's view path.
absolute view within the application: the view name starts with double slashes '//'. In this case, the view will be searched for
under the application's view path. This syntax has been available
since version 1.1.3.
aliased view: the view name contains dots and refers to a path alias. The view file is determined by calling
YiiBase::getPathOfAlias(). Note that aliased views cannot be themed
because they can refer to a view file located at arbitrary places.
relative view: otherwise. Relative views will be searched for under the currently active controller's view path.
Also you can use this function in your views. But the most convenient way to reuse views is to create widgets.

OpenCart Breadcrumb in header.tpl

Can someone tell me how can I have the breadcrumb nav within the header.tpl and not in the product.tpl of opencart?
I've just had to figure this out for a new site we're building and I've come up with the following; use at your own risk (I'll report back if I run into any major problems, but I can't foresee any... famous last words)
Basically the breadcrumbs are built in the controllers and we need the resulting $breadcrumbs array in the header controller. Modify system/engine/controller.php as follows:
[...snip...]
protected function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child,array('parent_data'=>&$this->data));
}
[...snip...]
This will send all the data in the parent controller, before render() was called, to every controller/method of the $children. Then we just need to pick this up in the header controller as follows:
<?php
class ControllerCommonHeader extends Controller {
protected function index($args=array()) {
// parent data
$this->data['parent_data'] = $args['parent_data'];
[...snip...]
And we can access everything in the template with $parent_data['whatever']. In this case, $parent_data['breadcrumbs'] will be the array of breadcrumbs that I can loop over with the code I've removed from each page.tpl and added to my header.tpl.
Due to the way 1.5.X is coded, you'll need to rewrite every controller and add a method back to the document class to allow passing from the product controller to the header controller. Is there any particular reason you want to do so?
if all else fails just hack the css, something like
.breadcrumb {
margin-left: -270px;
margin-top: -65px;
}
will move the breadcrumb up and to the left.

Captcha image and raw format

I've trying to get properly work kcaptcha class from kcaptcha.ru in my own component. 'Cause class not build for Joomla natively I break my brain on the wall.
And at the beginning...
I've a url to image generated by this class like: http://.../index.php&task=captcha&format=raw
In main controller I've put method
function captcha() {
include(JPATH_COMPONENT.DS.'libraries'.DS.'captcha'.DS.'kcaptcha'.DS.'kcaptcha.php');
$session = &JSession::getInstance('default', array());
$captcha = new KCAPTCHA();
if ($session) {
$session->set('captcha_keystring', $captcha->getKeyString());
}
}
And I've see in browser
When I request an image from the class all working good but in my component I cannot set session variables.
Any ideas how to fix this problem?
And problem solved successfully.
For &format=raw in controller Joomla set default mime-type to text/html.
For healing this issue developer must reset mime/type via setting
$document = &JFactory::getDocument();
$document->setMimeEncoding('image/png');
mime/encoding off course depends on you needs.