SELECT element option values - html

i am doing a select in html then I have something in the JS part like this
var languageEN = "en,1";
var languageFR = "fr,2";
var languageDE = "de,3";
and my html markup will be like
<select style="display: none;" id="select">
<option value=languageEN>English</option>
<option value=languageFR>Francais</option>
<option value=languageDE>Deutsch</option>
</select>
but it seems that I am hitting undefined.
I been trying to find whats the correct syntax to include in the option value but no avail. thanks.

If using PHP is an option you could do this. The first part could go at the top of the page, then the second part wherever the select needed to be. You could add as many options or change them any way you liked this way.
<?php
$options = array(
0 => array(
'name' => 'English',
'value' => 'languageEN'
),
1 => array(
'name' => 'Francais',
'value' => 'languageFR'
),
2 => array(
'name' => 'Deutsch',
'value' => 'languageDE'
),
);?>
<select style="display: none;" id="select">
<?php
foreach ($options as $option);
{
echo '<option value="'.$option['value'].'">".$option['name'].'</option>'
}
?>
</select>

Related

Update form after ajax save

I want form data updated after ajax save. Cause if item was new (id - empty), it tries to create new one each time. Also there are back-end generated fields which are appears after save.
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => ['category/save', 'id' => $category ? $category->id : ''],
'enableClientValidation' => true,
// 'enableAjaxValidation' => false,
'validateOnChange' => false,
'validateOnBlur' => false,
'validateOnSubmit' => true,
'options' => [
'id' => 'customer-update',
'class' => 'ajax-submit',
],
'fieldConfig' => [
'template' => '<div class="row-content-col1">{label}</div><div class="row-content-col2">{input}{error}</div>'
]
]); ?>
.......
<?php echo $form->field($category, 'url')->textInput(['class' => 'ip', 'readonly' => true]); ?>
......
<?php $form->end(); ?>
Form field produce such html:
<div class="row-content-col1"><label class="control-label" for="category-url">Url</label></div><div class="row-content-col2"><input type="text" id="category-url" class="ip" name="Category[url]" readonly><div class="help-block"></div></div>
</div>
And than on controller i return this (tried different variations):
{"error":false,"message":"Category 'asdfzsdf sdf' saved","data":{"name":"asdfzsdf sdf","url":"asdfzsdf-sdf","project_id":1,"id":21}}
What is valid response for ajax form? Or is there other way to handle this all ?
Pjax is really useful for your challenge, Just add your form inside of Pjax widget. add form action to new path(such: site/control-data).
In your action method do what you want, but send response like that :
return $this->renderAjax('form path',$model);
It's the general of what you must do.
But maybe you have problem with jquery or pjax or need some more data, but all questions have an answer,
See Pjax for ActiveForm

Yii2 add extra attribute in select > options list

How I can add an extra attribute like in below screenshot state_id=1 in options list for all.
<?= $form->field($model, 'district_id')->dropDownList(ArrayHelper::map($Districts, 'id', 'name')) ?>
You need to iterate through the $Districts array and associate all the attributes you want to add to the <option> of the dropdown, i assume that your $Districts array has something like below
$Districts=[
1=>"North Andaman",
2=>"South Andaman"
3=>"Nicobar"
];
Now you need to iterate that array and associate the attributes with every option
foreach ($Districts as $id => $name) {
$optionAttributes[$id] = ['my-attr' => 'value'];
}
The above will show you something like
Array
(
[1] => Array
(
[my-attr] => value
)
[2] => Array
(
[my-attr] => value
)
[3] => Array
(
[my-attr] => value
)
)
Now when creating your dropdown you should pass this array to the options option of the dropdownList() see below
echo $form->field($model, 'district_id')->dropDownList(
$Districts,
['options' => $optionAttributes]
);
Now if you see the source of the page it will show you the dropdown like below
<select id="contacts-district_id" name="Contacts[district_id]" class="form-control">
<option value="1" my-attr="value">North Andaman</option>
<option value="2" my-attr="value">South Andaman</option>
<option value="3" my-attr="value">Nicobar</option>
</select>

Selected value in a dropdown list Yii2

could someone tell me how to create a selected value in dropdown list?
Here is my dropdown list:
<?= Html::dropDownList(
'calculation-type',
$calculateByMonths,
$calculationTypeList, [
'options' => [
Employee::DISABLED =>[
'disabled' => true,
'selection' => true
]
],
'id' => 'calculation-type',
]); ?>
That line selection => true doesn't work, I don't know why :( Thanks for the help.
As you can see in official Yii2 documentation second argument in Html::dropDownList is $selection, and it has to contain string or array of selected values.
Values in dropDownList are keys in $items array. For example, if you have an array of months and you need to make February selected:
<?php
$month = [
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
];
echo \yii\helpers\Html::dropDownList(
'months', //name of your select tag
'feb', // selected option value
$month // array of items
);
?>
<!-- Output of the dropDownList-->
<select name="months">
<option value="jan">January</option>
<option value="feb" selected>February</option>
<option value="mar">March</option>
<option value="apr">April</option>
</select>

CakePHP onChange event

I have no idea why this onChange event is not working. Maybe I got so used to the code that I can't see my mistake. I'd appreciate your help:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend' => false);
echo $form->select('capacidade', $options, array(
'class' => '',
'label' => '',
'default' => '200',
'onchange' => "javascript:checkForOther(this);",
));
?>
</td>
As others suggested, place the attributes in the correct parameter position. The other thing i would do is remove the javascript and just have the function name in there like:
From: 'onchange' => "javascript:checkForOther(this);"
To: 'onchange' => "checkForOther(this)"
Try
Putting the attributes as the fourth argument instead of the third:
echo $form->select('capacidade', $options, null,
array(
'class'=>''
,'label' => ''
,'default' => '200'
,'onchange' => "javascript:checkForOther(this);"
)
);
Does the source of the generated page have the onChange attribute?
See the documentation
According to CakePHP documentation on select method, the third argument is used to choose which option is selected by default.
You must use the fourth argument to pass HTML attributes to a select element:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend'=>false);
echo $form->select('capacidade', $options, '200', array('class'=> '', 'label' => '', 'onchange' => "checkForOther(this);"));
?>
</td>

Zend subform view script render

I would rather not deal with decorators as my form design is not exactly straight forward, but i would like to keep the functionality of validating the forms.
So i have it set up where sub forms are working correctly, but when i try to style it manually in my viewscript i get the name without the parent. I've seen other posts that are similar, but i haven't found a solution.
Example:
This is in my view script
<?php echo $this->form->username->renderViewHelper();?>
I then get
<input type="text" value="" id="username" name="username">
When rendered. It should be
<input type="text" value="" id="form1-username" name="form1[username]">
How do i get that form1 portion?
Thanks!
Edit
Ok, so i found one way.
By using belongTo, it works:
$form1->addElements(array(
new Zend_Form_Element_Text('username', array(
'belongsTo' => 'form1',
'required' => true,
'label' => 'Username:',
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alnum',
array('Regex',
false,
array('/^[a-z][a-z0-9]{2,}$/'))
)
))
));
Is there a better way to do this or is this the only way?
Edit2
public function prepareSubForm($spec){
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}
public function setSubFormDecorators(Zend_Form_SubForm $subForm){
$subForm->setDecorators(array(
'FormElements', \\<--- I tried to change this to PrepareElements before.
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}
I believe you can get your desired output just by using:
<?php echo $this->form->username; ?>
I get the expected output when calling this without renderViewHelper. This is also without any special code for decorators or preparing sub forms. All I had to do was add belongsTo to the form element.
UPDATED:
If you set this to be your default decorator, you can eliminate the dd/dt tags from rendering, instead it will use a div. Then you may be closer to getting the custom output you want. You can change the tag in HtmlTag from div to whatever tag you would like to wrap your elements in. This is what I use mostly:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
This is the default for Zend Framework:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId)))
array('Label', array('tag' => 'dt'))
);
Note that file, and submit/button elements use different decorators.
Also see this answer