I am working on zend form and fieldsets. I have created form along with the submit button. Here is the code:
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Search Results',
'id' => 'submit',
'class' => 'submitme',
),
));
I can see all my fieldsets on view side. But, I cannot see submit button.
Here is my controller file code:
$form = $this->getServiceLocator()->get('FormElementManager')->get('Client\Form\Search\SearchForm');
return new ViewModel(array('form' => $form));
And finally this is the code for view:
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag($form);
You probably miss out the part to assign an action for your form
$form->setAttribute('method', 'post')
->setAttribute('action', $this->url())
->prepare();
Try doing something like this (as mentioned by sam) you need to set attributes
$Form = $this->Form;
$Form->setAttribute('action', $this->url('(your route name here)', array('action' => 'add', 'controller' => '(your controller name here)')));
$Form->prepare();
echo $this->form()->openTag($Form);
echo $this->formSubmit($Form->get('any other feilds that you want to see'));
echo $this->formSubmit($Form->get('submit'));
echo $this->form()->closeTag();
Related
I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong?
and can each button call a different method in the controller?
Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>
clicking the first button will give post data like:
array(
[btn1] => btn1value
[YourModel] => array(...)
)
Which makes it easy to do something like:
if (isset($this->request->data['btn1'])) {
// btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
// btn2 was clicked
}
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
<?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
echo $this->Form->button(
'Your Action Description Here',
array(
'type' => 'submit',
'formaction' => 'yourActionHere' //
)
);
Try this, This is easy to do.
<div class="submit">
<?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
<?php echo $this->Form->button('Cancel', array('type' => 'button'));?>
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.:
echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));
EDIT
First form that needs to send data to a specific controller is here
echo $this->Form->create('Message', array(
'url' => array('controller' => 'messages', 'action' => 'send')
));
echo $this->Form->input('mob_network', array(
'options' => array($list)
));
echo $this->Form->input('mob_phone', array(
'type' => 'text'));
echo $this->Form->input('message', array(
'type' => 'textarea'));
echo $this->Form->submit('Send', array('div' => false,'class' => 'btn btn-default btn-add'));
echo $this->Form->end();
controller is here
class MessagesController extends AppController {
public function send()
{
pr($this->request->data);
}
}
The problem is that i cant see this data. after clicking submit button the page acts as if it was refreshed..
You should add:
echo $this->Form->end();
to the end of both forms. Or you left that out in your code?
In CakePHP 2.4, how do I pass a form directly to a controller parameter? My controller takes a parameter in the format /Model/index/by:keyword, however FormHelper keeps inserting a question mark and an equals sign into the URL: Model/index?by%3A=keyword.
This is the form I've been using. Is there any way to change this default behavior?
echo $this->form->create('Post', array('action' => '/index', 'type' => 'get', 'class' => 'navbar-form'));
echo $this->form->input("by:", array('label' => '', 'placeholder' => 'Search', 'class' => 'form-control'));
echo $this->form->end();
I'd try to create the form like this:
echo $this->Form->create('Post', array('type' => 'GET', 'url' => array('controller' => 'yourpostcontroller', 'action' => 'search')));
echo $this->Form->input('search', array('class' => 'form-control'));
echo $this->Form->button('Search', array('div' => false, 'class' => 'btn'));
echo $this->Form->end();
In your "Search" action (inside the controller) do not forget to read the request:
if(!empty($this->request->query['search']) ...{
//do something
}
Hope it helps.
I created a simple form in typical ZF2 application. The form class code is just a modified code provided by Zend's Album example.
<?php
namespace Admin\Form;
use Zend\Form\Form;
class CityForm extends Form
{
public function __construct($name = null)
{
parent::__construct('city');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text'
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'province',
'attributes' => array(
'type' => 'text'
),
'options' => array(
'label' => 'Province',
),
));
$this->add(array(
'name' => 'country',
'attributes' => array(
'type' => 'text'
),
'options' => array(
'label' => 'Country',
),
));
$this->add(array(
'name' => 'coordinate',
'attributes' => array(
'type' => 'text'
),
'options' => array(
'label' => 'Coordinate',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitButton',
),
));
}
}
And call it like this in CityController, a typical controller extends AbstractActionController:
public function addAction()
{
$form = new CityForm();
$viewData = array(
'form' => $form
);
return new ViewModel($viewData);
}
Finally in view I echo it like this:
<?php $title = 'Add New City'; ?>
<?php $this->headtitle($title); ?>
<h1><?php echo $this->escapehtml($title); ?></h1>
<?php $form = $this->form; ?>
<?php $form->setAttribute('action', $this->url('city', array('action' => 'add'))); ?>
<?php $form->prepare(); ?>
<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('province'));
echo $this->formRow($form->get('country'));
echo $this->formRow($form->get('coordinate'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
What I expected to see is a vertical form like this:
But what i got is an ugly form like this:
What's wrong with my code? Please help.
EDIT:
When I inspect element, the form generated is strange. The <input> element is inside the <label> element.
<form id="city" name="city" method="post" action="/karciscus/public/admin/city/add">
<input type="hidden" value="" name="id">
<label>
<span>Name</span><input type="text" value="" name="name">
</label>
<label>
<span>Province</span><input type="text" value="" name="province">
</label>
<label>
<span>Country</span><input type="text" value="" name="country">
</label>
<label>
<span>
Coordinate</span><input type="text" value="" name="coordinate">
</label>
<input type="submit" value="Save" id="submitButton" name="submit">
</form>
I'm pretty sure it is the cause of my ugly rendered form. I think it is not supposedly like that. How to fix it?
That's something you have to do in your css.
Give your label a fixed width/length will align them correctly.
If you want to achieve your example above, add display:block for labels then they will each take a full line, like a block element.
Fixed. For other who has the same experience with me, just add element id in form class:
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'id' => 'name', // Must have id, will render strange otherwise!
),
'options' => array(
'label' => 'Name',
),
));
This fixes the issue for me.
Use the built in twitter bootstrap css which comes with Zf2. Use divs to render your form elements. I understand that everyone thinks about the form data that you set on your .html is pure php data and you cant align it.
<div class="row">
<?php $title = 'Add New City'; ?>
<div>
<div class="row">
<?php $this->headtitle($title); ?>
<h1><?php echo $this->escapehtml($title); ?></h1>
<?php $form = $this->form; ?>
<?php $form->setAttribute('action', $this->url('city', array('action' => 'add'))); ?>
<?php $form->prepare(); ?>
<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('province'));
echo $this->formRow($form->get('country'));
echo $this->formRow($form->get('coordinate'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
</div>
Thats the sample....try doing what you like just like what i have shown above and you are good to go.
i am very much new to cakephp. I have created a simple form with input controls on it as follows:
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('address', array('rows' => '3'));
echo $this->Form->input('aaa', array(
'type' => 'date',
'label' => 'select',
'before' => '--before--',
'after' => '--after--',
'between' => 'Date',
'separator' => '****',
'empty' => '--select--'
));
echo $this->Form->checkbox('subjects', array('value' => 'Java'));
?>
java
<?php
echo $this->Form->input('gen', array(
'type' => 'radio',
'options' => array('m', 'f')
));
echo $this->Form->input('file', array('type' => 'file'));
echo $this->Form->input('listbox', array('options' => array(1,2,3,4,5), 'multiple' => 'multiple'));
echo $this->Form->end('Submit');
?>
i wish to print the values entered in these components on another page. how do i do that?
i tried to do it with the help of session(which seems to be inappropriate) as follows:
public function contactus() {
if ($this->request->data!=null) {
$var=$this->request->data;
$this -> Session -> write('myvar', $this->request->data);
//$this->set($var, $this->request->data);
$this->redirect(array('action' => 'contactview'));
}
}
but it outputs array and i cant use session to store each component's value.
how do i solve this?
According to cakephp book (Form: http://book.cakephp.org/1.3/view/1384/Creating-Forms)
<?php
echo $this->Form->create(null,
array('url' => array('controller' => 'recipes', 'action' => 'add')));
?>
//Output:
<form method="post" action="/recipes/add">
Therefore , just change your create function and add the url option.
Rather than using the session, I used this->data and it was able to resolve my problem.