Zend Form create button link - zend-framework

I have Zend_Form with button type element. i don't how to use onclick type from zend_form.
Index.phtml
Search
Zend_Form
$Search = new Zend_Form_Element_Submit('Search');
$Search->setAttrib('ID', 'searchbutton');
$Search->setDecorators(array('ViewHelper'));
Here using this zend form how to perform the onclick action using search button.could you please any one help on this?
Thanks for advice!

Hey you would have to do something like this
$Search = new Zend_Form_Element_Submit('Search');
$Search->setAttrib('ID', 'searchbutton');
$Search->setDecorators(array('ViewHelper'));
$form = new Zend_Form('FormExample');
$form->setAction('destination.php');
$form->setMethod('POST');
$form->addElement($Search);
$form->setView(new Zend_View());
print($form);
If you have other elements than the $Search you just have to addElements as an array, like this:
$form->addElements([$Search,$Element1, $Element2]);

Related

HTML table Class codeigniter

In the following code (codeigniter documentation), how can i put an <a href=""> on a specific field? Is it possible?
$this->load->library('table');
$query = $this->db->query('SELECT * FROM my_table');
echo $this->table->generate($query);
Thanks a lot.
The solution is to do a foreach on your dataset and then amend the element using the anchor function which is an HTML helper within CI. Something like this:
foreach ($data as $=>$row){
$data[$k]['field'] = anchor($row['URL'],$row['DisplayText']);
}

subform within a subform

I'm working on a model using Zend Form. I have a subform called $product_item. I would like to add multiple instances of it to another subform called $items. How would I go about doing that? I'm not finding the Zend reference guide particularly helpful.
You can just add sub-forms to sub-forms:-
$form = new Application_Form_Test();
$subForm1 = new Application_Form_TestSubForm();
$subForm2 = new Application_Form_TestSubForm();
$subForm1->addSubForm($subForm2, 'sub2');
$form->addSubForm($subForm1, 'sub1');
$this->view->form = $form;
On submission the subform values will be available in arrays in the $_POST array. $value=$_POST['sub1']['sub2']['name'] for example.
http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.subforms
To print or access elements in sub forms you have several options:-
If $subForm1 has an element declared thus:-
$email = new Zend_Form_Element_Text('email');
Then the email field can be rendered in the view like this:-
<?php echo $this->element->sub1->email; ?>
Remember that the elements are referenced by their names not by the variables you use to declare them.
Also, remember that $this->element is referencing an instance of Zend_Form so you have all of those methods available. That means you can do this:-
<?php
$form = $this->element;
$formElements = $form->getElements();
?>

Zend Framework / Form / DisplayGroup / SetLegend problem

I have the following code in my 'Default_Form_Registration' class (inside _init method):
$fullname = $this->addDisplayGroup(
array('firstname', 'lastname'),
'fullname');
$fullname->setLegend('Full Name');
My form is being displayed properly (elements inside the fieldset), however there is no legend being displayed. I have not changed the decorators at all, using default decorators.
What am I doing wrong?
You can pass options to the addDisplayGroup through the 3rd param:
$fullname = $this->addDisplayGroup(
array('firstname', 'lastname'),
'fullname', array('legend' => 'Full Name'));
Also, as a note, addDisplayGroup returns Zend_Form and not the display group.
If you want to use the setLegend method, you need to do like this:
$this->getDisplayGroup('fullname')->setLegend('Full Name');

Easy way to get "params" from Joomla menu table

I don't know too much about Joomla, but I'm trying to work with a Menu on a Joomla site. In the Database I can see a column called params in the menu table, and it has some data I need. The params column has this data:
categories=446
feedLink=1
fusion_item_subtext=
fusion_columns=1
fusion_customimage=
splitmenu_item_subtext=
page_title=
show_page_title=1
pageclass_sfx=
menu_image=-1
secure=0
I know I can do a mysql query, get that column and parse the value using string manipulation/regex, but that doesn't sound like the right way.
I have seen some code in Joomla that looks like:
$cid = $params->get('secure');
Does Joomla have a special way to query and return objects so that these params are accessible with this type of syntax?
Right way is to use JMenu::getParams method
$app =& JFactory::getApplication();
$menu =& $app->getMenu();
$params = $menu->getParams($menuItemId);
$params->get('paramName');
Yes, Joomla does have special way of getting the parameters in an easily accessible object based on JObject.
you can get the entire site menu with this
$menu = JFactory::getApplication()->getMenu();
$item = $menu->getActive(); // will get active menu item. can use getItem() instead to get specific item
$item->get('parmName');
This is not exact code, more like pseudocode. This will get you on the right track...
Helpfull Stuff:
Joomla Framework API
JMenu Documentation
first you get a JApplication instance like this
$app = & JFactory::getApplication();
or for joomla 1.5 use:
global $mainframe //to get JApplication object
get JMenu instance like this:
$menu = $app->getMenu();
you can get active menu params or any other menu params like this
$active = $menu->getActive(); //get active menu
$menuInstance = $menu->getActive($Itemid); // to get Itemid use JRequest::getInt('Itemid', 0);
here you have an StdClass object with params field inside, now u use JParameter class like this
$menuParams = new JParameter($menuInstance->params);
here you have it, to get any parameter you want:
$someParam = $menuParams->get('some_param', 'default');

Populate values to checkbox while editing in zend framework

I am trying to populate the values into check box. I want check box to be checked when there is value stored in database.
This is my code in form:
$form ['test_1'] = new Zend_Form_Element_Checkbox('test_1');
$form['test_1']->setLabel('test1')->setCheckedValue('1');
$form ['test_2'] = new Zend_Form_Element_Checkbox('test_2');
$form['test_2']->setLabel('test2')->setCheckedValue('2');
If there is value 1 in database i want first check box to be checked and if its 2 then 2nd checkbox needs to be checked.
What do i need to do in the controller.
Could anyone please help me on this issue.
The easiest way would be to fetch the values from the database as an array that maps to the form input elements, e.g. return a row like
array('test_1' => 'value of checkbox', 'test_2' => 'value of checkbox');
You could then simply call $form->populate($values) and let do Zend_Form do the setting, e.g. in your controller do
public function showFormAction()
{
$form = $this->getHelper('forms')->get('MyForm');
$data = $this->getHelper('dbGateway')->get('SomeTable');
$form->populate($data->getFormData());
$this->view->form = $form;
}
Note: the helpers above do not exist. They are just to illustrate how you could approach this. Keep in mind that you want thin controllers and fat models, so you should not create the form inside the controller, nor put any queries in there.