I have used the following code for to add multiselect attribute in mine custom module Form
$fieldset->addField('multiselect2', 'multiselect', array(
'label' => Mage::helper('checkout')->__('Select Type2'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "return false;",
'onchange' => "return false;",
'value' => '4',
'values' => array(
'-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),
'1' => array(
'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
'label' => 'Size'
),
'2' => array(
'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
'label' => 'Color'
),
),
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
Its working Good. But i want to set the values as dynamically. That means I want to display all category list under this multiselect attribute. I have searched lot of things. But i cant get. Can we use foreach loop for adding value to that attribute? or have any other way to do this? Please help me guys!
You can prepare values array with any data before and then use it in your form. For categories it's smth like this:
$categories = Mage::getModel('catalog/category');
$values = array();
foreach ($categories as $category) {
$values[] = array('label' => $category->getName(), 'value' => $category->getId());
}
Hope it's clear and works for you.
Related
Is there any way to configure Zend\Form\Element\Select to return integer?
If I have a Form with a Select Element something like this (this is the common way to configure Select Element according to documentation and my internet research):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
I thought If I change value option like this:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
the integer will be returned but I was wrong. In both cases string is returned. My php code write this form values to a db table where category_id is defined as int.
In ZF2 use the Zend\Filter\Int or Zend\Filter\ToInt depending on which version of ZF2 you are using, Zend\Filter\Int became deprecated in ZF2.4.
In your form, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}
The below code creates some radio buttons and html to go with them, it works well however I now want to set the first radio button as the default selected one and im not sure what needs to be added.
<?php
for ($i =0; $i < count($packages); $i++){
echo "<div class='package-outter'>";
echo "Name: ".$packages[$i]['Package']['name']."<br>";
echo "Number of Campaigns (per month): ".$packages[$i]['Package']['quantity']."<br>";
echo "Price: ".$packages[$i]['Package']['price']."<br>";
if ($i == 0){
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
));
}else{
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
'hiddenField' => false, // added for non-first elements
));
}
echo "</div>";
}
?>
I'm not really familiar with CakePHP but a quick Google Search gave me this
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
So you should add the attribute 'value' and set it as your default selected radio
If you try the associated value option it should work.
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => array(
1 => 'High',
2 => 'Medium',
3 => 'Low'
)
'value' => 'Medium'
) ); ?>
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => $status,
'value' => 1 => default value of input
));
Try this
$this->Form->control('company_id', [
'label' => 'Escolha a empresa',
'options' => $companies ,
'value'=> key($companies),
'type' => 'radio',
'required' => true
])
I am using the Yii Framework, and I'm having issues with the bootstrap.widgets.TbGridView widget in conjunction with the Pager widget.
(Please note that I am new to Yii - so feel free to point out any obvious mistakes on my behalf)
All the filtering works fine, however when I select any option from the 'Fulfilled' dropdown menu it changes the URL structure of the Pager.
For instance, by default when I view the Pager links in Firebug the URL looks like this:
3
However once I make a selection from the 'Fulfilled' dropdown, and try to use the Pager again it no longer works, and after inspecting it via Firebug the Pager links have all changed dramatically to this:
3
I would expect the URL to retain a very similar structure as the initial link above.
Below is the view code for the page
<div class="row-fluid">
<div class="inner">
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id' => 'shoppurchases-grid',
'type' => 'striped bordered condensed',
'dataProvider' => $model->setPurchaseType('school')->search(10),
'filter' => $model,
'template' => '{summary}{items}{pager}',
'columns' => array(
array(
'header' => 'Product Image',
'value' => 'CHtml::image($data->product->displayImageUrl, $data->product->product_name, array("class"=>"grid-image"))',
'type' => 'raw',
),
array(
'header' => 'Product',
'name' => 'product.product_name',
'value' => '$data->product->product_name',
'filter' => CHtml::activeTextField($model, 'filterProductName', array('placeholder' => 'filter by product name')),
'type' => 'raw',
),
array(
'header' => 'Username',
'name' => 'user.firstname',
'value' => '($data->user instanceof MyUser) ? CHtml::link(CHtml::encode($data->user->username),array("/carrot/myuser/update","id"=>$data->user->user_id)) : ""',
'filter' => CHtml::activeTextField($model, 'filterUserName', array('placeholder' => 'filter by username')),
'type' => 'raw'
),
array(
'header' => 'Form Name',
'name' => 'user.form_name',
'value' => '($data->user instanceof MyUser) ? $data->user->form_name : ""',
'filter' => CHtml::activeTextField($model, 'filterFormName', array('placeholder' => 'filter by form name')),
'type' => 'raw'
),
array(
'header' => 'Student',
'name' => 'user.username',
'value' => '($data->user instanceof MyUser) ? $data->user->fullname : ""',
'filter' => CHtml::activeTextField($model, 'filterFirstName', array('placeholder' => 'filter by first name')),
'type' => 'raw'
),
array(
'header' => 'Price',
'name' => 'price',
'filter' => BHtml::activeCompareableTextField($model, 'price', array('placeholder' => 'filter by price')),
'type' => 'raw'
),
array(
'header' => 'Purchase Time',
'name' => 'purchase_time',
'value' => 'app()->dateFormatter->format("dd/MM/y H:m:s", $data->purchase_time)',
'filter' => BHtml::activeCompareableDateRange($model, 'purchase_time', array('placeholder' => 'filter by purchase time')),
'type' => 'raw'
),
array(
'header' => 'Fulfilled',
'name' => 'fulfilled',
'value' => user()->hasAuth(Group::READ_ONLY, "equal") ? '$data->fulfilled ? "Yes" : "No"' : 'CHtml::activeCheckBox($data, "fulfilled")',
'filter' => CHtml::activeDropDownList($model, 'fulfilled', array(0 => 'No', 1 => 'Yes'), array('prompt' => 'All')),
'type' => 'raw',
'htmlOptions' => array('class' => 'align-center'),
'headerHtmlOptions' => array('class' => 'align-center'),
),
array(
'name' => 'organisation_name',
'visible' => ((user()->hasAuth(Group::GROUP_ADMIN, 'equal')) && (!user()->hasState('view_org'))),
'filter' => CHtml::activeDropDownList($model, 'organisation_id', CHtml::listData(Organisation::model()->leaChildren, 'organisation_id', 'organisation_name'), array('prompt'=>'All Schools')),
),
array(
'header' => '',
'value' => 'CHtml::link("Refund", Yii::app()->controller->createUrl("bulk",array("id"=>$data->primaryKey)), array("class" => "btn btn-save", "name" => CHtml::activeName($data,"refunded")))',
'type' => 'raw',
'visible' => !user()->hasAuth(Group::READ_ONLY, "equal")
),
),
)); ?>
</div>
Thanks in advance - please remember I'm new to Yii
update...
I've checked the Firebug log and can see the GET request is failing (as it shows up in RED text in the Firebug log)
The URL is :
http://mysite.local/site/shop/purchases/admin/ShopPurchases%5BfilterProductName%5D//ShopPurchases%5BfilterUserName%5D//ShopPurchases%5BfilterFormName%5D//ShopPurchases%5BfilterFirstName%5D//ShopPurchases%5Bprice%5D//ShopPurchases%5Bpurchase_time%5D//ShopPurchases%5Bfulfilled%5D/0/ajax/shoppurchases-grid/ShopPurchases_sort/price?ajax=shoppurchases-grid
Once it tries to call this 'failed' URL the Pager stops working until I do a page refresh (e.g F5) and it returns to its default pager settings.
update 2...
The failed URL when loaded directly into the address browser for some reason will automatically attempt to add 'www.' at the start of the URL so the full url now looks like this..
http://wwwmysite.local/site/shop/purchases/admin/ShopPurchases[filterProductName]//ShopPurchases[filterUserName]//ShopPurchases[filterFormName]//ShopPurchases[filterFirstName]//ShopPurchases[price]//ShopPurchases[purchase_time]//ShopPurchases[fulfilled]/0/ShopPurchases_page/2/ajax/shoppurchases-grid?ajax=shoppurchases-grid
Is this something possibly related to my local htaccess file (this problem doesn't seem to exist on our 'live' version of the app.
I had to use some functionality to 'Remove Filters' in Yii for this to work - will post some code shortly.
i'm using PHP Yii framework, and came across a problem regarding adding Id and other attributes to a tab. I'm using yii bootstrap by the way as extension:
$tabs = array(
'item1' => array(
'label' => 'label1',
'content' => $content1,
),
'preview' => array(
'label' => 'Preview',
),
);
$this->widget('bootstrap.widgets.TbWizard',
array(
'id' => 'harvest-tabs',
'type' => 'tabs',
'tabs' => $tabs,
'pagerContent' => false,
));
How do i add an 'id' and 'class' property to each of the tab 'item1' and 'preview'?
i tried:
'item1' => array(
'label' => 'label1',
'content' => $content1,
'id' => 'some id'
'class' => 'some class'
),
'preview' => array(
'label' => 'Preview',
'id' => 'some other id'
'class' => 'some other class'
),
But this doesn't add a class to it. I only see <li>'s
that serves as the container for the tabs. Sorry i'm a newbie here. I'd appreciate any help Thanks!
You should try this:
'item1' => array(
'label' => 'label1',
'content' => $content1,
'htmlOptions'=>array(
'id' => 'some id'
'class' => 'some class'
),
),
I haven't tried it but I'm following yii bootstrap logic here. Tell me if it's not working.
I have a select element as follows and I want to make it non required field. But I am unable to do so. It gives me error "Value is required and can't be empty"
$this->add(array(
'name' => 'civil_status',
'type' => 'Select',
'attributes' => array(
'id' => 'civil_status',
'class' => 'form-control',
),
'options' => array(
'empty_option' => 'Civil status',
'value_options' => array(
'married' => 'Married',
'single' => 'Single',
'other' => 'other',
),
)
));
I have set the validation as follows
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => FALSE,
'allow_empty' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => '2',
'max' => '255',
),
),
),
)));
I tried to remove required from input filter as above and even more I have tried to remove required validation from the controller too as follows. But nothing seams to work. Please let me know if you guys know to fix this issue.
$form->getInputFilter()->get("civil_status")->setRequired(FALSE);
$form->getInputFilter()->get("civil_status")->setAllowEmpty(TRUE);
Select has build in InArray validator you dont need to use StringLength here or filters you only need to use 'required' => false which should work.
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => false,
)));
I know it might be strange question but did you add your InputFilter to your Form before you hit $form->IsValid() ?