Zend_Form Multiple dropdown as one value - zend-form

For my zend form I would like a month drop down and a year drop down.
I would like them to appear next to each other. Its okay for them to be separate elements in the zend_form and combine and check them in the controller, but for the design I would like them to sit right next to each other.
I could accomplish this by setting no label on the year and then doing some css trickery, but I'd like a cleaner solution.
Edit, Final Code:
public function init($options=array()) {
$this->setName('add');
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$number = new Zend_Form_Element_Text('number');
$cvv = new Zend_Form_Element_Text('cvv');
$expiry_month = new Zend_Form_Element_Select('expiry_month');
$expiry_year = new Zend_Form_Element_Select('expiry_year');
$amount = new Zend_Form_Element_Select('amount');
$submit = new Zend_Form_Element_Submit('Submit');
$amounts = self::load_amounts();
$months = self::load_months();
$years = self::load_years();
$name->setLabel("Name On Card")->setIgnore(false)->setRequired(true);
$number->setLabel("Card Long Number")->setIgnore(false)->setRequired(true);
$cvv->setLabel("CVV2")->setIgnore(false)->setRequired(true);
$expiry_month->setMultiOptions($months);
$expiry_year->setMultiOptions($years);
$amount->setLabel("Amount")->setIgnore(false)->setRequired(true)->setMultiOptions($amounts);
$submit->setLabel("Submit")->setIgnore(true);
$this->addElements(array($name, $number, $cvv, $expiry_month, $expiry_year, $amount, $submit));
$this->addDisplayGroup(array('expiry_month', 'expiry_year'), 'Expires');
}
and if its helpful to anyone, I don't think you can set a label, but you can set a fieldset title via:
$this->addDisplayGroup(array('address', 'city', 'state', 'country', 'zip'), 'Address', array('legend' => 'Billing Address'));

I some code with a different approach, but you can have a label set for the row and both month and year appear on the same row. They are also validated together as one unit with a custom validation class, I'll post the part you asked here.
$expMonth = new Zend_Form_Element_Select('exp_month');
$expMonth->setLabel('Card Expiry Date:')
->addMultiOptions(
array('1' => '01', '2' => '02', '3' => '03', '4' => '04 shortened')
->setDescription('/');
$this->addElement($expMonth);
// Generate the Expiry Year options
$expYearOptions = array();
$thisYear = date('Y');
for ($i = 0; $i < 15; ++$i) {
$val = $thisYear + $i;
$expYearOptions[$val] = $val;
}
// The Expiry Year field
$expYear = new Zend_Form_Element_Select('exp_year');
$expYear->removeDecorator('label')
->addMultiOptions($expYearOptions)
->setDescription(' (Month / Year)');
$this->addElement($expYear);
// Setup Expiry Month decorators
$expMonth->setDecorators(array(
// Show form element
'ViewHelper',
// This opens the wrapping DD tag but doesn't close it, we'll close it on
// the year field decorator later
array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'id' => 'card-expire',
'openOnly' => true)),
// Using this to slip in a visual seperator "/" between both fields
array('Description', array('tag' => 'span', 'class' => 'seperator')),
// Show the label tag displayed for exp_month
array('Label', array('tag' => 'dt'))
));
// Now for the Expiry Year field decorators
$expYear->setDecorators(array(
'ViewHelper',
// Inserting the "(Month / Year)" line using Description
array('Description', array('tag' => 'small', 'class' => 'greyout')),
// "row" is normally used to wrap a whole row, label + form element.
// I'm "misusing" it to close off the DD tag we opened in the month field earlier
// If you are already using "row", you might choose to echo the form line by line,
// where you close the dd tag manually like: echo
// $this->form->getElement('exp_year').'</dd>';
array(array('row' => 'HtmlTag'), array('tag' => 'dd', 'closeOnly' => true))
));
The full code with expiry validation can be found on my blog:
http://hewmc.blogspot.com/2010/08/validating-month-and-year-fields-as-one.html

I did the same task by creating a separate decorator.
ex: In your form init function use this
$decorators = array('ViewHelper',array('Description',array('tag'=>'','escape'=>false)),'Errors');
set a decorator in each element like:
$name = new Zend_Form_Element_Text('name');
$name->setDecorators($decorators);**
and after the following line
$this->addElements(array($name, $number, $cvv, $expiry_month, $expiry_year, $amount, $submit));
use this line of code:
$this->setDecorators (array('FormElements',array (array('data'=>'HtmlTag'), array('tag'=>'table')),'Form'));
now design your phtml according to your requirement use your control in form by their name like
$this->element->FirstName;

Related

backdrop cms module-created blocks deltas not working

(tagging with drupal7 because i cannot create a tag for backdrop)
i have a fresh backdrop install, with a new theme duped from bartik, and a new layout duped from moscone_flipped. no code changes to those yet.
i have a module that creates 2 simple blocks, mostly just some html. i have implemented hook_block_info() and hook_block_view(). i can place blocks into regions in the layout using the admin ui. i can see each block on the front end page when i place either one of them. but not both. when i have both blocks placed in the layout, for some reason both regions display the output from the same block. and i have verified that it is always the first block defined in the array returned from hook_block_info(). i have cleared caches, checked code, etc.
has anyone seen this before?
btw, i just applied the recent security upgrade, and the behavior is the same both before and after the upgrade.
i will paste in the module code below, in case i have missed something.
thanks for any help anyone can provide.
// implements hook_block_info()
function mbr_block_info()
{
$info = array();
$info['rate-tables'] = array(
'info' => 'Rate Tables (Buttons)',
'description' => 'The displays the rate table links for the sidebar',
);
$info['mbr-footer'] = array(
'info' => 'MBR Footer',
'description' => 'Displays footer links, disclaimer, copyright',
);
return($info);
}
// implements hook_block_view()
function mbr_block_view($delta = '', $settings = array(), $contexts = array())
{
$block = array();
switch($delta)
{
case 'mbr-footer':
$subject = null;
$mbrFooter = getMBRFooterBlock();
$block = array('subject' => $subject, 'content' => $mbrFooter);
case 'rate-tables':
$subject = null;
$rateTables = getRateTablesBlock();
$block = array('subject' => $subject, 'content' => $rateTables);
}
return($block);
}

Magento API: Set dropdown attribute option for a storeview

I am working with magento API and need to create dropdown options for different storeviews.
I found a function to to create a dropdown option for default storeview:
public function addAttributeOption($arg_attribute, $arg_value)
{
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table');
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
$value['option'] = array($arg_value,$arg_value);
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
}
This functions works fine, I can add a new attribut value for default storeview.
Example:
I have the attribute "mycolor" and call the function like
addAttributeOption("mycolor", "black")
Now I have a storeview for a german shop and like to set the german color. I would need something like
addAttributeOption("mycolor", "black", "schwarz", $storeview)
Means set the color option of storeview to schwarz where the color of the default value is black.
Does anybody has an Idea how can I do that?
Best regards
I figure you alreay found your solution but perhaps I can help someone else who's new to Magento like I am. Today I had to find a way to import attributes (Product Attributes only that is) from an external Products-Managing-System into Magento running with multiple store views, too. I don't know where the questioner's addAttributeOption function came from but the Magento installer script offers its very own addAttributeOption(). So I took a look into Setup.php where Magento's addAttributeOption() is defined:
{Your Magento Path}/app/code/core/Mage/Eav/Model/Entity/Setup.php
Now, in the Magento Version i'm working with (1.9.1.0) addAttributeOption() expects one argument, an array called $option. It's architecture looks as follows:
Array (
'attribute_id' => '{attributeId}',
'value' => array(
'{optionId}' => array(
'{storeId}' => '{labelName}',
),
),
'delete' => array(
//...
),
'order' => array(
//...
)
);
As you can see, 'value' expects an array and this array's key determines the storeID. In most addAttributeOption()-introductions I found on the web, the storeID is hard coded to 0 with no further explanation - 0 makes it the required default admin value. So, quite obviously by now, for adding Options with StoreView-dependent labels we simply have to add an extra array value for each StoreView like this:
Array (
'attribute_id' => $attribute_id,
'value' => array(
'option1' => array(
'0' => 'black', // required admin value
'1' => 'Schwarz (RAL 9005)', // if storeId = 1 is German
'2' => 'Black (RAL 9005)', // if storeId = 2 is English
),
'option2' => array(
'0' => 'blue',
'1' => 'Blau (RAL 5015)',
'2' => 'Blue (RAL 5015)',
),
// And so on...
)
);
Note: If your option's array-index is a number addAttributeOption() expects it to be the ID-Number of an already existing Option. This is great in case you want to update already existing options but this also means a new Option musn't be numeric. Hence I named them 'option1' & 'option2'.
You can call addAttributeOption() like this:
Mage::app();
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();
// ...
// generate your Options-Array
// I called it $newOptions
$installer->addAttributeOption($newOptions);
$installer->endSetup();

how to integrate multimodelform with echmultiselect yii?

I am running into a little snag with combining extensions "EchMultiSelect" and "MultiModelForm" of YII framework.
What I'm trying to do is copy a set of form elements one of which elements is an EchMultiSelect widget.
According to tutorial on the jqRelCopy page, I would need to pass a copy of the element (datePicker in their example) to the 'jsAfterNewId' option:
'jsAfterNewId' => JQRelcopy::afterNewIdDatePicker($datePickerConfig),
So, I tried to modify that to:
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
Where I also added the following to MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if(this.attr('multiple')=='multiple'){this.multiselect(jQuery.extend({$jsOptions}));};";
}
its copied and working properly when i am using Add Person link but what happens if i am adding/cloning three items for example and when i change the third item multiselct option then its reflecting to the first multiselect dropdown only this is same for other as well also when i am adding new items by clicking on the Add Person link then its cloning the same element to the new row item
here is the code for the form configuration variables and multimodel widget call.
//$userList=array of the userIds from users table
$memberFormConfig = array(
'elements'=>array(
'userId'=>array(
'type'=>'ext.EchMultiSelect.EchMultiSelect',
'model' => $User,
'dropDownAttribute' => 'userId',
'data' => $userList,
'dropDownHtmlOptions'=> array(
'style'=>'width:500px;',
),
),
...
...
));
calling the MultiModelForm widget from the same view file
$this->widget('ext.multimodelform.MultiModelForm',array(
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $model, //instance of the form model
'tableView' => true,
'validatedItems' => $validatedMembers,
'data' => Person::model()->findAll('userId=:userId', array(':userId'=>$model->id)),
'addItemText' => 'Add Person',
'showAddItemOnError' => false, //not allow add items when in validation error mode (default = true)
'fieldsetWrapper' => array('tag' => 'div',
'htmlOptions' => array('class' => 'view','style'=>'position:relative;background:#EFEFEF;')
),
'removeLinkWrapper' => array('tag' => 'div',
'htmlOptions' => array('style'=>'position:absolute; top:1em; right:1em;')
),
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
));
Can someone help me with this please?
Thanks in advance!
After a long searching and googleing i found the solution for this, just replace the function in your MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if ( this.hasClass('test123456') )
{
var mmfComboBoxParent = this.parent();
// cloning autocomplete and select elements (without data and events)
var mmfComboBoxClone = this.clone();
var mmfComboSelectClone = this.prev().clone();
// removing old combobox
mmfComboBoxParent.empty();
// addind new cloden elements ()
mmfComboBoxParent.append(mmfComboSelectClone);
mmfComboBoxParent.append(mmfComboBoxClone);
// re-init autocomplete with default options
mmfComboBoxClone.multiselect(jQuery.extend({$jsOptions}));
}";
}
Thats It....!!
Thanks...!!!

How can i take one attribute of my model and put it in a option element attribute of a activeDropDownList?

In de documentation for activeDropDownList() -> $htmlOptions says this: options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
array(
'value1'=>array('disabled'=>true,'label'=>'value 1'),
'value2'=>array('label'=>'value 2'),
);
So it should be posible to do it.
I have something like this:
<?php
$criteria = new CDbCriteria;
$criteria->condition = "estado = 'activa'";
$criteria->order = "fecha_inicio";
echo $form->dropDownList($model,'salida',
CHtml::listData(Salida::model()->findAll($criteria), 'id', 'fecha_inicio', 'actividad.nombre'),
array('prompt'=>'-- Selecciona --',
'options' => array(
'26'=>array('disabled'=>true,),
'23'=>array('disabled'=>true,),
))
);?>
The drop down it's generating correctly. How can do this for all option taking the value form one model's field?
I figured out:
$opciones = CHtml::listData(Salida::model()->findAll($criteria), 'id', 'precio');
$opciones_f = array();
foreach ($opciones as $id => $precio) {
$opciones_f[$id] = array('data-precio'=>$precio);
}
echo $form->dropDownList($model,'salida',
CHtml::listData(Salida::model()->findAll($criteria), 'id', 'fecha_inicio', 'actividad.nombre'),
array('prompt'=>'-- Selecciona --','options' => $opciones_f,)

Problems returning result of CDbCriteria based query

I have a query as follows
$criteria1 = new CDbCriteria();
$criteria1->condition = 'id = 1';
$modelA=Table1::model()->find($criteria1);
I can pass it to a view and return the title and entry
$this->widget('bootstrap.widgets.TbBox', array(
title' => $modelA['title'],
'content' => $modelA['entry'] ));
Now I'd like to return a range of entries
$criteria2 = new CDbCriteria();
$criteria2->condition = 'id > 7';
$modelB=Table1::model()->findAll($criteria2);
(btw : I'm following a form as laid out here). I was expecting to be able to read the resulting array of values out as below, but ['title'] is now being seen as a undefined index (obviously I'm expecting to read this out in a loop but you get the point)
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $modelB['title'][0],
'content' => $modelB['entry'][0]));
Where am I going wrong?
Thanks
No, the indexes should be specified in the different order: the number of a specific element first, then the name of the property. Additionally, it's better (=cleaner) to name the result of findAll so it'll show you (and any other reader) that it's a collection, not a single model:
$models = Table1::model()->findAll($criteria2);
// ...
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $models[0]['title']
//...
));
But even that's not necessary if you use foreach (and you probably will):
foreach ($models as $model):
// ...
$this->widget('some.name', array(
'title' => $model['title']
);
endforeach;