I'm a relative noob when it comes to Zend Framework, however I've got a form that I need to use if a couple of views so I thought I might use a Action Helper to instantiate the form set a few attributes and pass it to the relevant view. I've created the Action Helper and can call it from within the relevant controller's action, however when I try to pass the form to the action's view nothing gets rendered, ie:
$form = new Application_Form_Colour;
if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
{
$model = new Application_Model_Colour();
$model->changeColour($form->getValues());
$form->reset();
}
else
{
$form->newColour->setAttrib('disabled', 'disabled');
}
$this->view->form = $form;
Is there something I am doing wrong or have I got the wrong idea of what an Action Helper can be used for? Maybe its not an Action helper that I need to use?
It turned out I was just being stupid! Instead of
$this->view->form = $form;
at the end of the Action Helper I should have done:
return $form;
Then in my controller:
$this->view->form = $this->_helper->myActionHelper->myActionHelperMethod();
Silly me...
Related
I want to have a modal Report Issue form in the sidebar that loads on every page. I can't seem to wrap my head around where to place the controller/form initialization code.
A typical controller action:
public function actionContact()
{
$model = new Feedback();
if ($model->load(Yii::$app->request->post()) && $model->sendEmail()) {
Yii::$app->getSession()->addFlash('success', 'Thank you for contacting us.<br /><br />We will respond as soon as possible.');
$model = new Feedback();
}
if (Yii::$app->request->isPjax) {
return $this->renderAjax('contact', [
'model' => $model,
]);
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
The $model is set when the action is first called, but if the modal is to be placed in the sidebar, every controller and/or controller/action could be called, and I don't want to have the $model initialization repeated each time (DRY).
I'm not sure if this is a prime condition for a custom widget, or a controller->beforeAction, or something else altogether.
Any insight is greatly appreciated.
Use a widget. The widget will create an empty $model and pass it to it's view. You can insert the widget in any view or in the layout too.
Regarding the
every controller and/or controller/action could be called
why would any controller be called? Your form should send the info to 1 controller/action not any of them. The widget will be shown by any controller/action but the widget's form will always just call 1.
I have a form which contains a Collection of an unspecified number of subforms. I want to have functionality allowing the user to add a new, blank item to the Collection for them to fill in. The Symfony docs tell us how to do this using Javascript clientside to add new blank form controls, which are then submitted and persisted as normal, but I'd like to do it serverside in the controller, without Javascript.
The problem I'm encountering is to do with the way Symfony Forms work. I have an "Add" button added to my main form, and I intend to detect whether it is that button which has been clicked, so that I can add the blank item to the Collection and re-render the form. But to detect the click I need to call $this->createForm and at that point the form is fixed with the original set of items, it's too late to add an extra one.
//Symfony Action
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
$form->handleRequest($request);
//Ideally I'd somehow do this test earlier, but I need $form to do it...
if ($form->get('add')->isClicked() )
{
//TOO LATE!
$person->getSelections()->add(new Selection() );
}
if ($form->isValid())
{
if ($form->get('save')->isClicked() )
{
//Persist
}
}
//Render page etc
Things I've thought about:
Putting the Add button in a completely different form on the same page, which submits to a different Action which can then do some preparatory work before forwarding to the main Action above
Inspecting submitted HTTP data directly to note that Add has been clicked (shame not to use the standard Symfony method)
Give up and use Javascript as suggested (it might work in this example, but I'd like to have the option of carrying out server-side activity (without AJAX...) as part of adding the new blank item)
How can I best achieve this in a proper Symfony way?
EDIT Just seen this: https://github.com/symfony/symfony/issues/5231, which is essentially a feature request to allow what I'm after. One suggestion a commenter makes is to add a blank item to the Collection and then remove it if it's not needed - I don't know how one would do that, but it sounds promising.
ANOTHER EDIT It occurs to me that, because I need two different aspects of the $form I'm creating, I could maybe just make the $form, use it to handle the request, detect the button click, and then throw that $form away, before altering my model and creating another $form. I don't know if that would somehow fall foul of some rules about handling the submission twice.
I'm not 100% but I think you could do the following...
//Symfony Action with (Request $request, ...)
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->get('add')->isClicked()) {
// Add thing
} elseif ($form->isValid()) {
// or
// } elseif ($form->get('save')->isClicked() && $form->isValid()) {
// Persist and what not
}
}
//Render page etc
I haven't tested it so I don't know whether it will trigger the form errors (or if it will actually work) so if it does (or it doesn't) I apologise.
What I did in the end was have my Add button hit a separate Action, which then delegates to the main action with a flag to say "add a new Selection", as below:
public function selectionsAddAction(Request $request, $id)
{
return $this->selectionsAction($request, $id, true);
}
public function selectionsAction(Request $request, $id, $addNew = false)
{
$person = $this->getPerson($id);
//Also use "add mode" if we just deleted the last one!
if (!$person->getSelections()->count())
{
$addNew = true;
}
//$addNew is set by a separate action, hit by a different form with the Add button in
if ($addNew)
{
$person->getSelections()->add(new Selection() );
}
//We now have the right number of items, and can build the form!
$form = $this->createForm(new SelectionsType($lookups), $person);
//...
}
Im my layout I have search form, so it's displayed on every page.
It looks like this:
<?
$form = new SearchIndexForm();
$form->setAction($this->configuration['BaseUrl']Index/search);
echo $form; ?>
I would like to have sth like that: when someone click submit button, the form redirects to the action "search" on "Index" controller, where there is processing the form values.
So, what should I do, to have equivallent to this, which will be working with data send by post method from form in layout?
if($this->_request->isPost()){
$formValues = $this->_request->getParams();
if ($form->isValid($formValues)){
...
}
}
With the above, when I click submit, it gets me to the /Index/search, but nothing happens...
The form itself worked perfectly when it was in one action.
It seems like you're doing everything right but I think that there is some code that you should change.
Try the following code
$request = Zend_Controller_Front::getInstance()->getRequest();
//or you can try
//$request = $this->getRequest();
if($request->isPost()){
$form = new SearchIndexForm();
if ($form->isValid( $request->getPost() ) ){
echo 'This should output if the form is valid' . PHP_EOL;
}
}
I don't like accessing variables directly as you did in $this->_request because Zend might have to do doing things to the variables to make them 'proper'. I know that sometimes nothing is done but better safe than sorry. Unless you're positive about it, which I'm usually not positive about unless I've really looked at the code.
Ive done some search but no success.. i am trying to figure out how to define others layout() parts such layout()->content variable.. i would love to get int layout()->navigation (a custom one) which display the navigation..
Any ideas ?
Thanks.
Not sure if this is what you want, but you can create additional 'parts' of layout simply by assigning a value to your new part. ZF will take care of the rest. For example, in a bootstrap.php you could do:
public function _initNewLayoutPart() {
$view = $this->bootstrap('view')->getResource('view');
$view->layout()->newpart = 'some new part';
}
Then in your layout.phtml you could just echo the new part:
<?php echo $this->layout()->newpart; ?>
It is possible by just creating a new variable in layout, you can define it in your controller (preferably in init or postDispatch). Just like this:
public function init()
{
$this->view->layout()->motd = '<b>Message of the day.</b>';
}
Then in your actual view where you want to see the message, all you have to do is:
<?php echo $this->layout()->motd; ?>
If you want something fancier, such as rendering a whole page or sidebar, try the following:
public function init()
{
$this->view->layout()->sidebar = $this->view->action('render', 'sidebar');
}
With render being the action (including render.phtml) and sidebar being the controller.
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;