I am trying following to make a dropdown readonly:
$element = $this->CreateElement('select', 'type');
$element->addMultiOptions( $Types );
$element->setRequired(true);
$element->setAttrib('readonly',true);
$element->setLabel('Type');
$elements[] = $element;
A dropdown is shown but I am able to select other values.
How to make a dropdown readonly ?
Thanks
You can use the read only attribute. You could avoid outputting a dropdown at all if it is not the best element to use in your particular case.
$element->setAttrib('disabled','disabled');
If you mean to only have one value selected, you could always disable it. Using your current framework, you might do something like:
$element->setAttrib('disabled', true);
Related
I am working in gravityforms to pre-populate a form with database values dynamically, which is working. I need to be able to specity which of these options is selected by default when the form is built, but I can't find the option to do so. I have seen the placeholder, but I presume this doesnt actualy select anything on the dropdown. I have been unable to find any docs or references that allow me to set the "selected" options once I have built all the text/value pairs in the array and set the choices.
The function (which I have redacted here) works fine and populates the field, I just need to populate a placeholder and/or default selected value.
add_filter('gform_pre_render_1', 'getFieldValues');
add_filter('gform_pre_validation_1', 'getFieldValues');
add_filter('gform_pre_submission_filter_1', 'getFieldValues');
function getFieldValues($form) {
global $wpdb;
foreach ($form['fields'] as $field) {
if ($field->id == '40') {
// get the list of values from the Database
$list1Q = "SELECT <data> FROM <table> WHERE <params> = <value>";
$list1R = $wpdb->get_results($list1Q);
// Generate a nice array that Gravity Forms can understand
if (!empty($list1R)) {
$list1A[] = array();
foreach ($list1A as $listItem) {
// Add current value to the array for dropdown choices
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable);
}
}
// Set choices to field
$field->choices = $List1C;
***** THIS IS WHERE I WOULD LIKE TO SET THE SELECTED VALUE *****
}
}
return $form;
}
If there is a better way to go about populating this field, I am open to suggestions at it seems that the form loads a bit slow using this method. Otherwise, I would love to know how to set a selected choice value after populating the choices.
the quick answer is, there is an option that i found for "isSelected" that can be included in the array when defining choices. I added a ternary operation to match on what I wanted selected and it set the "selected value for me.
$isSelected = ($listItem->variable == "value I want to select") ? true : false;
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable, 'isSelected' => $isSelected);
Adding the ternary choice and changing the array push allowed me to set a value as selected.
When i select value from drop down list,
and I submit form, i want my drop down list to have selected value,
not to be on default value again.
I try like this, but not working:
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array('name'=>'countriesIssued'), $select = array($_POST['countriesIssued']));?>
Also i would like to add a first value to be default, not from db, i want to do it in the code like this, array('empty'=>'--Select country--')
but not working.
Thanks
If you must change the name of your dropdownList you must manually set the value of code to $_POST['countriesIssued'] in your controller/view. As for the default, prompt is used to set this.
<?php if(!$model->code) $model->code=$_POST['countriesIssued'];?>
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array(
'name'=>'countriesIssued','prompt'=>'--Select country--'
));?>
The second parameter (currently 'code') must be a key in this array: $countriesIssuedList
See for example here.
"Also i would like to add a first value to be default" ... perhaps you can use array_merge()?
I'm trying to add extra HTML attributes to some select options in a dropdown list in Joomla 2.5, and want to use the built-in HTML helpers rather than write the HTML by myself. The current output is:
<select>
<option value="Red">Red</option>
</select>
but I would like it to be something like:
<select>
<option value="Red" data-img="red.jpg">Red</option>
</select>
so that I can access the data-img attribute using Javascript when the selected option changes.
I'm creating the select options like so:
$select = JHtml::_('select.option', "Red", "Red");
and then passing them to JHTML to create the generic list HTML:
$html = JHTML::_('select.genericlist', ...);
I've looked through the documentation and tried passing various different parameter to the functions, but it's very confusing in terms of all the options (option.attr, attr etc) that the functions use, and Google has turned up nothing either.
Can anyone tell me what extra parameters I need to pass to the functions to get it properly add the extra attributes to the <option> elements?
Thanks in advance!
I was struggling on this exact scenario today, needing to add some extra data with the select's options. After a thorough analyze of the joomla/libraries/joomla/html/html/select.php file, I succeeded to do this with a few downside...
First, in my case, the data used for the select is coming from the database, and need some preparation for this scenario :
$db =& JFactory::getDBO();
$sql = 'SELECT nom AS value , nom AS text, prix FROM #__reservation_nourritures order by `ordering` asc';
$db->setQuery($sql);
$nourritures = $db->loadObjectList();
foreach ($nourritures as $nourriture){
//the data to use MUST be in an array form for the extra html attributes...
$nourriture->data = array('data'=>$nourriture->prix);
}
Once the data is ready, you can pass it to the JHTML function to build the select :
echo JHTML::_('select.genericlist',$nourriture,'myId',array('class'=>'nourritures','option.attr'=>'data'));
In short, the 'option.attr' must be used to insert attributes into the options. Note : The select.genericlist function MUST have only 3 arguments for this to work. From what I understand from the function, attributes only get merged into the options if you pass exactly 3 arguments to the function, otherwise it just ignores it. So if you want, as exemple, to define a preselected option with the additional parameters, you are out of luck. Here is the part regarding this in the function :
if (is_array($attribs) && func_num_args() == 3)
{
// Assume we have an options array
$options = array_merge($options, $attribs);
}
To my understanding, this is a bug and/or a bad behavior. I'll fill in a bugg in the joomla tracker when I get time.
You can do something like this
$option = JHtml::_('select.option', "Red", "Red", array('option.attr' => 'optionattr', 'attr' => array('data-img' => "red.jpg")));
or like this
$option = JHtml::_('select.option', "Red", "Red");
$option->optionattr = array(
'data-img' => "red.jpg"
);
I have a Zend_Form with a dropdown field.
When the user sets a value in the url this one should be selected as default value in this dropdown.
So what i do at the moment is this:
$parlang = $this->getRequest()->getParam('lang');
if($parlang){
$this->view->filterForm->getElement('ddLanguage')->setValue($parlang);
}
if ($this->getRequest()->isPost()) {
if($this->view->filterForm->isValid($_POST)){
...
...
...
No i want to check if the value of the variable is even a valid value for the dropdown? How can i check this in coorporation with the form validation. Yes i can check the variable against a array or so but this seems to be "fighting against the framework".
So what is the Zend way to do such a thing?
Edit:
My final solution for all who are interested, is:
$parlang = $this->getRequest()->getParam('lang');
if($parlang){
$ddLanguage = $this->view->filterForm->ddLanguage;
if($ddLanguage->isValid($parlang)){
$ddLanguage->setValue($parlang);
$language = $parlang;
}
}
I ran a quick test and it looks like one method you can use is Zend_Form_Element_Select::getMultiOption() to check if the language exists in the select values.
<?php
$parlang = $this->getRequest()->getParam('lang');
if ($parlang) {
$el = $this->view->filterForm->getElement('ddLanguage');
// attempt to get the option
// Returns null if no such option exists, otherwise returns a
// string with the display value for the option
if ($el->getMultiOption($parlang) !== null) {
$el->setValue($parlang);
}
}
If your Multiselect element contains a list of country, I would just populate a default in your element value according to the one in the URL.
In order to do so, you could create a custom Zend_Form_Element as follow:
class My_Form_Element_SelectCountry extends Zend_Form_Element_Select
{
protected $_translatorDisabled = true;
public function init()
{
$locale = Zend_Registry::get('Zend_Locale');
if (!$locale) {
throw new Exception('No locale set in registry');
}
$countries = Zend_Locale::getTranslationList('territory', $locale, 2);
unset($countries['ZZ']);
// fetch lang parameter and set US if there is no param
$request = Zend_Controller_Front::getInstance()->getRequest();
$lang = $request->getParam('lang', 'US');
// sort your country list
$oldLocale = setlocale(LC_COLLATE, '0');
setlocale(LC_COLLATE, 'en_US');
asort($countries, SORT_LOCALE_STRING);
setlocale(LC_COLLATE, $oldLocale);
// check weither the lang parameter is valid or not and add it to the list
if (isset($countries[$lang])) {
$paramLang = array($lang => $countries[$lang]);
$countries = array_merge($paramLang, $countries);
}
$this->setMultiOptions($countries);
}
}
You get the idea from this custom form.
If what you're trying to do isn't a Multiselect field filled by a country list but a list of language instead, then the logic is the same, you just need to change the call to the static method Zend_Locale::getTranslationList()and grab whatever information you need.
One more thing, if you just want a single element in your Multiselect element, then go for a Zend_Form_Element_Hidden.
It's a lot of "if" but I can't understand how looks like your Multiselect element exactly from your question.
Now let's take a look on the validation side, when you're using a Multiselect element, Zend_Framework automatically adds an InArray validator, which means that you don't have anything to do to check weither the data sent are correct or not. isValid is going to do it for you.
Weither a user let the default parameter and everything will be fine, or he modifies/deletes this parameter and the default parameter (en_US in this case, see code above) is going to be set as a default value for the Multiselect field.
To answer your last question, no it's not against the framework to check a variable set by a user and compare it with an array (from getTranslationList()for example). I would say it's even the recommended way to do things.
I am generating options for my dropdown using
// view.php
foreach ($plants as $row):
$options[$row->plant_id] = $row->plant_name;
endforeach;
and then lower in the HTML part of view.php
//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);
The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.
The HTML source also shows 'Select' in form_dropdown was ignored.
I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.
Any idea what is going on here or how to solve this issue?
You need to have the default option in your $options array...
So before your foreach, do:
$options = array('Select');
I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.
If you wanted to explicitly set this value as the default you would pass 0 as the default argument.