Zend 2 Form Select multiple : How to preselect values - php

I have the following form element configured and i don't know why the values are not preselected.
$this->add(array(
'name' => 'item_ids',
'type' => 'Select',
'attributes' => array(
'id' => 'item_ids',
'class' => 'form-control',
'multiple' => 'multiple',
'value' => array('1','2'),
),
'options' => array(
'label' => 'Items',
'label_attributes' => array(
'class' => 'col-sm-2 control-label',
),
'value_options' => array(
'1' =>'Item 1',
'2' =>'Item 2',
'3' =>'Item 3'
),
)
));
I want that the "Item 1" and "Item 2" are preselected.
I hope someone can help me with my problem.
#### Update ####
Found something like that in the documentation, i will give it a try :
'value_options' => array(
array(
'value' => '1',
'label' => 'Orange',
'selected' => true,
),
array(
'value' => '2',
'label' => 'Lemon',
),
),

you can set form values like :
$form->getElement('selector')->setValue('val');
or
$form->setDefaults(array(
'selector' => 'val'
));

As I mentioned in the Update of my first post, I found something that seemed to be the right way.
I tested it and this is the solution :
$this->add(array(
'name' => 'item_ids',
'type' => 'Select',
'attributes' => array(
'id' => 'item_ids',
'class' => 'form-control',
'multiple' => 'multiple',
),
'options' => array(
'label' => 'Items',
'label_attributes' => array(
'class' => 'col-sm-2 control-label',
),
'value_options' => array(
array(
'value' => '1',
'label' => 'Item 1',
'selected' => true,
),
array(
'value' => '2',
'label' => 'Item 2',
'selected' => true,
),
array(
'value' => '3',
'label' => 'Item 3',
),
),
)
));

Related

How to set Default value on search form?

I'm using a wordpress real estate plugin for my project which is wpcasa, I already do some modification, But this thing takes my time to figure out.
I think they use array to get set values/labels on search form.
Anyone can Help me to how to add default value into a search form?
Here's the code below
$defaults = array(
'keyword' => array(
'label' => __( 'Keyword or Listing ID', 'wpcasa' ) . '…',
'type' => 'text',
'class' => 'width-3-4',
'priority' => 10
),
'submit' => array(
'label' => __( 'Search', 'wpcasa' ),
'type' => 'submit',
'class' => 'width-1-4',
'priority' => 20
),
'offer' => array(
'label' => __( 'Offer', 'wpcasa' ),
'key' => '_price_offer',
'data' => wpsight_offers(),
'type' => 'select',
'data_compare' => '=',
'class' => 'width-1-5',
'priority' => 30
),
'location' => array(
'data' => array(
// wp_dropdown_categories() options
'taxonomy' => 'location',
'show_option_none' => __( 'Location', 'wpcasa' ),
'option_none_value' => '',
'hierarchical' => 1,
'orderby' => 'ID',
'order' => 'ASC'
),
'type' => 'taxonomy_select',
'class' => 'width-1-5',
'priority' => 40
),
'listing-type' => array(
'data' => array(
// wp_dropdown_categories() options
'taxonomy' => 'listing-type',
'show_option_none' => __( 'Type', 'wpcasa' ),
'option_none_value' => '',
'hierarchical' => 1,
'orderby' => 'ID',
'order' => 'ASC'
),
'type' => 'taxonomy_select',
'class' => 'width-1-5',
'priority' => 50
),
$details['details_1']['id'] => array(
'label' => $details['details_1']['label'],
'key' => '_details_1',
'data' => $details['details_1']['data'],
'type' => 'select',
'data_compare' => '>=',
'class' => 'width-1-5',
'priority' => 60
),
$details['details_2']['id'] => array(
'label' => $details['details_2']['label'],
'key' => '_details_2',
'data' => $details['details_2']['data'],
'type' => 'select',
'data_compare' => '>=',
'class' => 'width-1-5',
'priority' => 70
)
);
`
Example on keyword I will set a value HOME Instead showing its label.
Thank you!
I got it now I should add 'default' => 'Myvalue',
example
'keyword' => array(
'label' => __( 'Keyword or Listing ID', 'wpcasa' ) . '…',
'type' => 'text',
'class' => 'width-3-4',
'default' => 'HELLO',
'priority' => 10
),
Thanks for the help/advice!

CGridview fit with screen Resolution without Scroll

Im using Editable Grid extension of CGridview.
MY Grid:
$this->widget('EditableGrid', array(
'dataProvider' => $dataProvider,
'template' => '{items}{pager} ',
'id' => 'PO-grid',
'htmlOptions'=>array(
//'style'=>'width:1100px;'
),
'columns' => array(
array(
'class' => 'EditableGridColumn',
'header' => '',
'name' => 'PO_{gridNum}_{rowNum}_edit',
'imageurl'=> Yii::app()->request->baseUrl.'/images/update.png',
'tag' => 'button',
'tagHtmlOptions' => array(
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'StentysRef',
'name' => '[{gridNum}][{rowNum}]ref',
'tag' => 'textField',
'tagHtmlOptions' => array(
'readonly'=>true
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'SupplierRef',
'name' => '[{gridNum}][{rowNum}]productref',
'tag' => 'textField',
'tagHtmlOptions' => array(
'readonly'=>true
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'Product',
'name' => '[{gridNum}][{rowNum}]productname',
'tag' => 'textField',
'tagHtmlOptions' => array(
// 'size' => '2'
'readonly'=>true
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'Qty',
'name' => '[{gridNum}][{rowNum}]qty',
'tag' => 'textField',
'tagHtmlOptions' => array(
'size' => '2',
'readonly'=>true,'width'=>'10px','style'=>'width:70px;'
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'Description',
'name' => '[{gridNum}][{rowNum}]description',
'tag' => 'textField',
'tagHtmlOptions' => array(
// 'size' => '5'
'readonly'=>true
)
),
array('class'=>'CButtonColumn','template' => '{delete}',
'buttons'=>array(
'delete' => array(
'options'=>array('style'=>'margin-left:20px'),
'imageUrl'=>\Yii::app()->request->baseUrl.'/images/delete.png', )
),
),
),
));?>
My problem is, Gridview is fit with 1600 X 900 px screen , it should not fit with below px.
Anyone please help me to reolve this problem.
I want to make this grid fit with screen 1366 px.

Create Textarea input box in Zend 2 Form

I'm trying to set a textarea in a Zend Form but it always creates an input type text box.
I read some code using the Zend\Form\Element\Textarea but still no luck
This is how I am doing it in my ProjectForm.php:
$this->add(array(
'name' => 'summary',
'type' => 'Zend\Form\Element\Textarea',
'options' => array(
'label' => 'Resumen',
),
));
And in Project.php I have this
$inputFilter->add($factory->createInput(array(
'name' => 'summary',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
)));
Thanks
Nevermind, I was calling echo $this->formInput instead of echo $this->formTextarea viewHelper.
You have to mention the type in attributes:
$this->add(array(
'name' => 'summary',
'attributes' => array(
'id' => 'summary'
'type' => 'textarea',
'class' => '',
),
'options' => array(
'label' => 'Resumen',
),
)
);

loop options into array

I have an array that has a value with multiple options that is broken down into it's own array. One of those values I want to populate with a loop. Is this possible with the ; in the forloop I would think it would break. What is the best way to accomplish this request?
array(
'name' => __('Ensemble List', 'januas'),
'type' => 'checkbox',
'options' => array(
for ($x=0; $x<=10; $x++){
array('name' => __($x , 'title'), 'value' => $x)
}
)
),
Here is the complete array outside of what I posted:
$meta_boxes[] = array(
'id' => 'januas_speakers',
'title' => __('Ensemble', 'januas'),
'pages' => array('ja-event'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => __('Visible', 'januas'),
'desc' => __('Select Yes to show the box in the event page, No to hide it.', 'januas'),
'id' => 'januas_speakers_visible',
'type' => 'select',
'options' => array(
array('name' => __('Yes', 'januas'), 'value' => 'y'),
array('name' => __('No', 'januas'), 'value' => 'n'),
)
),
array(
'name' => __('Position', 'januas'),
'desc' => __('Select the preferred position for the box.', 'januas'),
'id' => 'januas_speakers_position',
'type' => 'select',
'options' => array(
array('name' => __('Main', 'januas'), 'value' => 'main'),
array('name' => __('Sidebar', 'januas'), 'value' => 'sidebar'),
)
),
array(
'name' => __('Order', 'januas'),
'desc' => __('Insert the box order (ex: 1).', 'januas'),
'id' => 'januas_speakers_order',
'std' => 1,
'type' => 'text_small'
),
array(
'name' => __('Show Title', 'januas'),
'desc' => __('Select Yes to show the box title, No to hide it.', 'januas'),
'id' => 'januas_speakers_showtitle',
'type' => 'select',
'options' => array(
array('name' => __('Yes', 'januas'), 'value' => 'y'),
array('name' => __('No', 'januas'), 'value' => 'n'),
)
),
array(
'name' => __('Show in Top menu', 'januas'),
'desc' => __('Select Yes to show the menu item in the event page top menu, No to hide it.', 'januas'),
'id' => 'januas_speakers_showinmenu',
'type' => 'select',
'options' => array(
array('name' => __('Yes', 'januas'), 'value' => 'y'),
array('name' => __('No', 'januas'), 'value' => 'n'),
)
),
array(
'name' => '',
'desc' => '',
'id' => 'januas_images_gallery',
'type' => 'image_gallery'
),
array(
'name' => __('Ensemble List', 'januas'),
'desc' => 'Select the ensemble memebers for this event.',
'id' => 'januas_speakers_completelist',
'type' => 'checkbox',
'options' => array_map(function ($x) {
return array(
'name' => __($x, 'januas'),
'value' => $x,
);
}, range(0,11))
),
array(
'name' => __('Display order', 'januas'),
'desc' => '',
'id' => 'januas_speakers_speakersorder',
'type' => 'event_speakers'
),
array(
'name' => '',
'desc' => '',
'id' => 'januas_speakers_backtotop',
'type' => 'backtotop'
)
),
);
You could use array_map:
array(
'name' => __('Ensemble List', 'januas'),
'type' => 'checkbox',
'options' => array_map(function ($x) {
return array(
'name' => __($x, 'title'),
'value' => $x,
);
}, range(0,11))
),
You should use array_push
$a = array(
'name' => __('Ensemble List', 'januas'),
'type' => 'checkbox',
'options' => array()
);
for ($x=0; $x<=10; $x++){
array_push( $a['options'], array('name' => __($x , 'title'), 'value' => $x) );
}
print_r($a);
Do you need to run the loop inside of options? How about this approach.
$foo = array();
for ($x=0;$x<10;$x++)
$foo[] = $x;
$bar = array(
'name' => 'hello',
'options' => $foo
);
print_r($bar);
https://eval.in/54925

ZF2 - Control/customise individual radio/checkboxes

I'm attempting to add data- values on radio buttons within ZF2. Is it possible to control each of the inputs specified with value_options?
A typical radio button added to a form:
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
'daily' => 'Daily',
'weekly' => 'Weekly',
'monthly' => 'Monthly',
),
),
));
Ultimately, I would like something like the following, so I can specify individual parameters/options for each radio item:
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
array(
'attributes' => array(
'value' => 'daily',
'data-item' => 'apple'
),
'options' => array(
'label' => 'Daily'
)
),
array(
'attributes' => array(
'value' => 'weekly',
'data-item' => 'orange'
),
'options' => array(
'label' => 'Weekly'
)
),
array(
'attributes' => array(
'value' => 'monthly',
'data-item' => 'pear'
),
'options' => array(
'label' => 'Monthly'
)
),
),
),
));
My reason for wanting the above, is that I want to use JavaScript to change something upon selecting a radio button, so it needs to hold data attributes.
Is anything like this possible yet?
It can be done by providing an array (or an object which implements ArrayAccess) instead of a single value (almost as you wrote in your example).
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
'daily' => array(
'label' => 'Daily',
'value' => 'daily',
'attributes' => array(
'data-item' => 'apple',
),
),
'weekly' => array(
'label' => 'Weekly',
'value' => 'weekly',
'attributes' => array(
'data-item' => 'orange',
),
),
'monthly' => array(
'label' => 'Monthly',
'value' => 'monthly',
'attributes' => array(
'data-item' => 'pear',
),
),
),
),
));
https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L177
This should work on radios, multi-checkboxes & selects too.

Categories