$this->view->layout()->setContentKey('') problem! - zend-framework

i set a new contentkey for showing menu in right column of my site.like this:
public function menuAction()
{
$this->view->layout()->setContentKey('menu');
}
this is layout code:
<div class="center_col"><?php echo $this->layout()->content; ?></div>
<div class="right_col"><?php echo $this->layout()->menu; ?></div>
but there is a prob!! after setting menu contentKey every action result showing in right column (content don`t work!)
i have a plugin for menu action , i think it is that reasone!
this is my plugin:
class Places_Controller_Plugin_ActionSetup extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request)
{
$front = Zend_Controller_Front::getInstance();
if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack')) {
$actionStack = new Zend_Controller_Plugin_ActionStack();
$front->registerPlugin($actionStack, 97);
} else {
$actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
}
$menuAction = clone($request);
$menuAction->setActionName('menu')->setControllerName('index');
$actionStack->pushStack($menuAction);
}}
i nead menu action call in all pages , so i use of this plugin!
is there any way for solve my prob?!
answer with Marcin and SMka help :
public function menuAction()
{
$this->view->placeholder("menu")
->append($this->view->render("menu/menu.phtml" ));
}

in your Menu action just do render to named segment
// Renders my/login.phtml to the 'form' segment of the
// response object
$this->render('login', 'form');
no need layout()->setContentKey, this method is for other things

Related

Symfony 1.4 Label position in a form

Is there a way to change the position of the label in a form? I would like to have it above the input field (sfWidgetFormInputText and sfWidgetFormChoice) . Is that possible?
Depends on how you're rendering the form field. You can do it a few ways:
If you want to render an individual field differently to the rest in your form, you can render the label and widget separately like this in your template:
// \apps\myApp\modules\myModule\templates\_form.php
echo $form['field_name']->renderLabel(); // Render the label
echo $form['field_name']->render(); // Render the widget
echo $form['field_name']->renderError(); // Render the error
echo $form['field_name']->renderHelp(); // Render the help message
If you want to apply the layout across your entire form. You should create a new formatter class and apply it to your form:
// \lib\form\formatter\sfWidgetFormSchemaFormatterCustom.class.php
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = '<div class="label">%label%</div><div class="field">%field%</div>%error% \n %help %hidden_fields%',
$errorRowFormat = '<div>%errors%</div>',
$helpFormat = '<div class="form_help">%help%</div>',
$decoratorFormat = '<div>\n %content%</div>';
}
Apply it in your form's configure method like this:
// \lib\form\myForm.class.php
public function configure()
{
//....
$formatter = new sfWidgetFormSchemaFormatterCustom($this->getWidgetSchema());
$this->widgetSchema->addFormFormatter('custom', $formatter);
$this->widgetSchema->setFormFormatterName('custom');
}
If you want the formatter to be used globally across your project, you can set it as the default formatter in your ProjectConfiguration.class.php.
// \config\ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
// ...
sfWidgetFormSchema::setDefaultFormFormatterName('custom');
}
}
You have to do it in the template associated with the form in which you can reorganize all the form using html tags (<th>, <tr>, <td> ....).

ZendFramework Form: Change select to input type text if verification fails

EDIT: Okay, I've stripped this down to the bare minimum.
The below code is how I would set up what I'm trying to accomplish in straight html/php.
If the form has been submitted and the verification of fields does not pass, a text field appears, otherwise, if the form has NOT been submitted, a dropdown is offered.
html/php:
<form method="post" action="">
<div class="state">
<?php
if(!$_POST['submit']){
// show the select list of states.
echo '<select name="state">
<option>list of all states</option>
</select>';
}else{
// show text input box
echo '<input type="text" value="'.$_POST['select'].'" name="state" />';
}
?>
</div>
<input type="submit" name="submit" value="submit" />
But I have no clue how I would set this up with the ZendFramework Forms Class, or how to tap into it to even begin to do this.
You really should not do this kind of stuff (I mean write plain-text form) if you're using Zend Framework. You should use the built-in methods.
First of all, enable the Form and create a form. Then use this very-easy-to-understand code. Note that I've not tried if it works 100% but this is 100% the logic you need.
Form class
class Application_Form_YourFormName extends Zend_Form
{
public function init()
{
$this->setMethod(self::METHOD_POST);
$this->setAction('THE-URL-WHERE-THIS-FORM-IS-MANAGED');
$Element = new Zend_Form_Element_Text('state');
$Element->setLabel('State:');
$Element->addValidators(array(/*DON'T KNOW WHAT KIND OF VALIDATION YOU NEED*/));
$Element->addFilters(array(new Zend_Filter_StringTrim(),
new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_QUOTES))));
$Element->setRequired();
$this->addElement($Element);
unset($Element);
$this->addElement('reset', 'Reset');
$this->addElement('submit', 'Submit');
}
public function stateNotPresent()
{
$this->removeElement('state');
// Note that getStates() is an hypotetical method of an
// hypotetical Application_Model_State where you can retrieve an
// array containing the list of the state you have. This array is
// needed to fill the Select list.
$States = Application_Model_State::getStates();
$Element = new Zend_Form_Element_Select('statelist');
$Element->setLabel('State:');
$Element->setMultiOptions($States);
$Element->addValidator(new Zend_Validate_InArray($States));
$Element->setRequired();
$Element->setOrder($this->count() - 2);
$this->addElement($Element);
unset($Element);
}
}
Controller class
public function name-of-the-action-you-needAction()
{
$Form = new Application_Form_YourFormName();
if ($this->_request->isPost())
{
if ($Form->isValid($this->_request->getPost()))
{
// Do things. A good text has been entered
}
else
{
$Form->stateNotPresent();
if ($Form->isValid($this->_request->getPost()))
{
// Do things. A good selection has been entered.
}
else
{
// echo the edited form (the one with the dropdown list)
$this->view->Form = $Form;
}
}
}
// The first time the page is requested.
// The page with the text box will be printed
else
$this->view->Form = $Form;
}
VIEW-OF-THE-ACTION.phtml
if ($this->Form != null)
echo $this->Form;
I hope you'll appreciate the effort I made to let you understand.

Load Zend_Form from a view helper in Zend?

Is it possible to load a Zend_Form from a view helper? I'm using thise form in a login action method. But I also want this form to be visible on the navigation on every page (so without the login action actually being called yet), the post method of the form will send to the login action method.
I'm guessing it should be done with a view helper but I don't see how.
Any ideas?
I tried with this:
my view helper:
class Zend_View_Helper_LoginForm
{
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
and I call it from my layout like this:
<?php echo $this->form(); ?> but this doesn't work. (I'm able to call the same form through an action method though!)
In this case it gives me this error (which doesn't make sense because my helper is only 9 lines long):
Warning: Missing argument 1 for Zend_View_Helper_Form::form() in C:\xampplite\htdocs\zendpr\library\Zend\View\Helper\Form.php on line 44
Your view helper should extends the Zend_View_Helper_Abstract class and the method of the view helper must have the same name as the class :
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
function loginForm() {
$form = new Form_LoginForm();
return $form;
}
}
and you call it like this in your view script :
echo $this->loginForm();
If you call :
echo $this->form();
You are using the view helper Zend_View_Helper_Form
Have your View_Helper extend Zend_View_Helper_Abstract and override the setView()
class Zend_View_Helper_XX extends Zend_View_Helper_Abstract {
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
Initialise the form in your controller action and set the form reference
// controller action code
$this->view->form = $form;
Then in the view helper you can reference the form via the view
// view helper code
$this->view->form;
class Zend_View_Helper_LoginForm extents Zend_Form {
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
OR
$this->view->form=$form;
Both are going to return the form. The view form is more specific for view.
Add this into your phtml view file
echo $this->form();
To answer this question - Remove the Parenthetical
Should be
echo $this->form;

Do an ZendX autocomplete in a layout

I would like to use ZendX_Jquery autocomplete in a partial, which is in my layout.
How can I do that :
My layout :
<div class="prefix_11 grid_5" id="header_search_engine">
<?php echo $this->partial("/common/_search_engine.phtml"); ?>
</div>
An action :
public function autocompleteAction($search='') {
$this->view->autocompleteElement = new ZendX_JQuery_Form_Element_AutoComplete('ac');
$this->view->autocompleteElement->setJQueryParam('source', '/searchengine/getsearch');
$this->view->autocompleteElement->setJQueryParam('minLength',
$this->configApplication->autocomplete->max_cars);
}
How can I use it in the partial, in the layout ?
How can i send the autocompleteElement in the partial view ?,
Thanks to help.
Fabrice
#fabrice
I had the same problem using autocomplete with my layout and I have solved it with the following:
Use placeholder for my searchBox
Create a Zend_Controller_Plugin that extends Zend_Controller_Plugin_Abstract
in predispatch execute method renderUiWidgetElement of autocomplete element.
set the placeholder with the form
Example:
class myLibrary_Controller_Plugin_SearchBox extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
$this->searchBox();
}
public function searchBox()
{
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
$view = $viewRenderer->view;
$searchForm = new Application_Form_JQueryForm();
$searchForm->acProduct->renderUiWidgetElement();
$view->placeholder('searchBox')->set($searchForm);
}
}
The key is to launch method renderUiWidgetElement();. Without it, the layout will not add the neccesary javascript to the autocomplete element. I got this information from here: http://cientouno.be/blog/categorie/zend-framework
Thanks a lot to cientouno!
Thank you, but, I used the ActionStack for display on every page. And do, I use a form.
public function autocompleteAction() {
$formAutoComplete = new Frontoffice_Form_Autocomplete();
$this->view->autocompleteElement = $formAutoComplete;
$this->_helper->viewRenderer->setResponseSegment('autocomplete');
}
and, in the layout :
<div class="prefix_10 grid_6" id="header_search_engine">
<?php echo $this->partial("/common/_search_engine.phtml");
echo $this->layout()->autocomplete;
?>
</div>
You can send the autocompleteElement in the partial view with this code.
<div class="prefix_11 grid_5" id="header_search_engine">
<?php echo $this->partial("/common/_search_engine.phtml", array('autocompleteElement ' => $this->autocompleteElement); ?>
</div>

Zend_Form - multiple forms on same page

Having multiple forms in one page, when i submit one of them, how can i tell wich one was submitted?
I thought about generating uniqe ids for each from, and saving them as hidden fields and to the user-session - while this is a solution, the problem with it is that there is no good place to remove old ids from the session.
Any better ideas how to solve this problem?
Thanks in advance!
First of all: have you considered sending the two forms to two different actions? That way you can handle each form separately in an action each. This should be the "best-pratice" if you're using the Zend MVC component.
The other option is to check for the value of the submit button which will be included in the request, e.g.
<input type="submit" name="save" value="form1" />
// in PHP:
// $_POST["save"] will contain "form1"
<input type="submit" name="save" value="form2" />
// in PHP:
// $_POST["save"] will contain "form2"
Be careful as the value-attribute will be rendered as the button's label.
So perhaps you want to distingush the forms by different submit-button names:
<input type="submit" name="save-form1" value="Submit" />
// in PHP:
// $_POST["save-form1"] will contain "Submit"
<input type="submit" name="save-form2" value="Submit" />
// in PHP:
// $_POST["save-form2"] will contain "Submit"
EDIT:
During the comment-dialog between the OP and myself the following seems to be a possible solution:
class My_Form_Base extends Zend_Form
{
private static $_instanceCounter = 0;
public function __construct($options = null)
{
parent:: __construct($options);
self::$_instanceCounter++;
$this->addElement('hidden', 'form-id',
sprintf('form-%s-instance-%d', $this->_getFormType(), self::$_instanceCounter);
}
protected _getFormType()
{
return get_class($this);
}
}
class My_Form_Type1 extends My_Form_Base
{
public function init()
{
// more form initialization
}
}
class My_Form_Type2 extends My_Form_Base
{
public function init()
{
// more form initialization
}
}
some errors in you code, shoudl be something like this:
class Application_Form_Idee_Base extends Zend_Form
{
private static $_instanceCounter = 0;
public function __construct($options = null)
{
parent::__construct($options);
self::$_instanceCounter++;
$this->addElement('hidden', 'form-id', array(
'value' => sprintf('form-%s-instance-%s', $this->_getFormType(), self::$_instanceCounter))
);
}
protected function _getFormType()
{
return get_class($this);
}
}