I have a search module. It has one form within it that produces the search field, I am using a viewScript that is stored within views/scripts/forms to render the form.
I want this form to appear on ever screen so I have written a ViewHelper that creates the from and returns it. This works fine when I am within the search module but when I am in any other module I am getting an error.
Any ideas why?
This is what is used to create the viewScript in the form
$this->setDecorators(array(
array( 'ViewScript', array('viewScript' => 'forms/Search.phtml'))
));
Thanks,
Martin
In my code I have to put a / in front of the path so for you it would mean:
$this->setDecorators(array(
array( 'ViewScript', array('viewScript' => '/forms/Search.phtml'))
));
Related
I have a simple ZF form that includes a hash element:
$hash = new Zend_Form_Element_Hash('hash');
$hash->setSalt('hf823hflw03j');
$hash->addErrorMessage('Form must not be resubmitted');
This works ok, but if I choose to strip out all the decorators and format the form using :
$this->setDecorators( array( array('ViewScript', array('viewScript' => '_form_register.phtml'))));
Then it seems that the hash value is renewed each time it is submitted and thus doesn't work.
In addition, PHPunit thinks the form element hash is invalid and so doesn't test the form processing.
Is there any solution to this?
Do you really need to use viewScript Decorator? Otherwise try to use this:
$this->setDecorators(array('ViewHelper','Errors'));
If you want to change the look of the Errors, you could create your own ErrorDecorator and then use it as follows:
$this->setDecorators(array('ViewHelper', new My_ErrorDecorator()));
I am aware that form errors can be set to render at the top of the form using decorator code such as:
$form->setDecorators(array(
array('FormElements'),
array('FormErrors'),
However, I have subforms within my (parent) form and I need to render the subforms errors - aggregated and rendered at the top of the parent form. How can this be achieved please? Thanks.
I am a bit new to Zend but I have a similar implementation that acheives this. Although I also have extra code in here that makes the errors display in a custom manner and also puts the subforms into seperate divs.
The important parts to notice are the facts that the errors are turned off on both forms by not specifying the decorator FormErrors and then creating a new errors decorator on the parent form.
On my main form I've added a FormErrors decorator such as this:
$form->setDecorators(array(
new Zend_Form_Decorator_FormErrors(array(
'ignoreSubForms'=>false,
'markupElementLabelStart'=> '<p>',
'markupElementLabelEnd'=> '</p>',
'markupListStart'=>'<div class="formErrors">',
'markupListEnd' => '</div>',
'markupListItemStart'=>'<div>',
'markupListItemEnd'=>'</div>'
)),
'FormElements'
));
Then on the subform you turn off the form errors
$subForm->setDecorators(array(
'FormElements',
array(
array('data' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'formRow')
)
));
I want to prefix the form title within the form tags, but above the form elements.
<form>
<h3>Login Form</h3>
<!-- form elements -->
</form>
I do not want to use a view script.
I thought of using the Description decorator, but that appears to be only available to the elements.
How do I set an arbitrary description for a form, and position it (either append / prepend) within the form tags? I have a feeling I need to play with the HtmlTag decorator but I've played with it and can't get the right results.
Typical. Ask a question, work out the solution.
$form->setDecorators(array(
array(
'Description',
array(
'tag' => 'div',
'class' =>'title'
)
),
'FormElements',
'Form'
))
->setDescription('Enter Login Credentials:');
Depending where you place the description decorator will determine where in the form tags it displays, i.e. place above 'FormElements' to display above the elements; place below 'FormElements' to display the description below the elements; place below the 'Form' decorator to display after the form tags.
I think this may be an easy solution, but I've spent an hour now investigating to no avail.
I have a registration form in an element that is being used in views belonging to different controllers. Using the "url" attribute, I've told it to submit to /users/register, but for some reason, the fields aren't submitting to the database.
Instead, there are errors for "undefined index" and a MySQL error for an undefined secondary key that I set (it's empty because it's not being submitted). Strangely, the form works fine if I include the element somewhere in the users views. Does anyone know why this is happening?
UPDATE - Here's the relevant code, sorry:
<?php
echo $form->create(array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')));
?>
The form fields are all correct, since the element works in the user controller's views anywhere. Do I need to include any other information in the creation of the form to point it more directly?
Specify the model 'User' as the first parameter to the $form->create() method.
<?php
echo $form->create('User', array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')
)
);
?>
This will ensure that the form fields are named as data[User][field_name], and prevent you from seeing that undefined index error.
Hope this works for you!
Without seeing the code, it sounds like Cake is magically assuming that the Model is the one for the controller that controls the current view. When that controller is Users, it works correctly. When it is, say, Articles, it will be trying (and failing) to fit the form fields to the Article model.
Without seeing any code, it is impossible to offer any more help.
EDIT:
If the form contains mixed models, e.g. User and Article, you must prefix the fieldnames like this:
$form->input('User.username');
$form->input('Article.title');
etc.
If you don't, the controller will assume they all belong to its own model.
I currently use zend_navigation via an XML file.
However I need to overwrite the previous breadcrumb to be its dynamic parent, in the controller.
Is this possible? It seems to me that zend_navigation is fairly static and the zend documentation keeps timing out.
Thanks
I have put:
public function addAction() {
$this->view->navigation()->addPage(array(
'type' => 'uri',
'label' => 'New page')
);
in my controller but no crumbbar shows up for that page.
Any ideas? $this->navigation() threw a
Method "navigation" does not exist and was not trapped in __call()
Also of note that my crumbBar is in my layout and not individual views.
Yes you can use an array.
What you should do really is create your array and then input it into the factory of the Zend_Navigation to create your pages for you.
Unfortunately my code is too complicated to show an example of how I used it. But I'll provide a simple example...
Once you create your navigation container, you can just add new pages to it.
Like
$this->navigation()->addPage(array(
'type' => 'uri',
'label' => 'New page'));
But you can also use addPages(). This is what I do.
I think you should just wait for the documentation to load back up for you and then look at that. Its really easy in fact.
When you have a more specific question, just ask that and give me a poke. I've had to use Navigation quite a lot so know it quite well.
Additionally, check out #zftalk on freenode. Theres lots of help on there.
// Disable Layout
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
// Output XML than HTML
$this->getResponse()->setHeader('Content-Type', 'text/xml; charset=utf-8');
$container = new Zend_Navigation();
// Replace this section with real dynamic data.
$pages = array(
array(
'label' => 'Save',
'action' => 'save',
),
array(
'label' => 'Delete',
'action' => 'delete',
),
);
// Add pages
$container->addPages($pages);
$this->view->navigation($container);
// Output the data.
echo $this->view->navigation()->sitemap();
Additionally uses Zend Router for redirecting site.com/sitemap.xml to this controller/function.
Thank you for many developers who help me to reach here.