Yii2 MaskedInput widget with spesial characters - input

I need to create input for company name. It can be from 1-4 words and use "". Now I can just make like this:
<?= $form->field($model, 'name')->widget(\yii\widgets\MaskedInput::className(),['name' => 'name', 'mask' => ['[A][a]{1,10}[ ][a]{1,10}[ ][a]{1,10}[ ][a]{1,10}']]) ?>
But right now I can use just letters. I see there is $definitions in this widget but how to use it correct?
After trying some othr options, I make it like this:
<?= $form->field($model, 'name')->widget(\yii\widgets\MaskedInput::className(),['name' => 'name', 'mask' => '"[A]a"',
'definitions' => [
'a' => [
'validator' => '^[А-ЯЁA-Z]+',
'cardinality' => 50,
]
]]) ?>
But I need to be dynamic length of this definitions. So now in my field
"Asddasdsa asdnmsadas d asdasd asdsamj______________"
And I need it to make dynamic length so the " symbol was after text

If you want to show the first letter in uppercase while typing then use JQuery function as follows
$("#your-field-id").keyup(
function () {
this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase();
}
);

Related

percentage format in Kartik GridView (yii2)

I am doing a project how to calculate total in yii2. When i'm building the project, i found trouble when i'm input data to be percent format. When I'm save data,in index look 2,300%, even though it is 23%. What can i do?
In my view, format like this'format'=>['percent',],
enter image description here
this is my code
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
/* #var $this yii\web\View */
/* #var $searchModel backend\models\LaporanSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Laporans';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="laporan-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Laporan', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showPageSummary'=>true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
// 'id',
// 'inv_no',
// 'inv_date',
//'rate',
[
'attribute'=>'kode_customer',
// 'width'=>'150px',
//'hAlign'=>'right',
// 'format'=>['decimal', 0],
],
// 'kode_item',
// 'qty',
// 'price',
// 'ed',
['attribute'=>'total_price',
'pageSummary'=>true
],
// 'dsc',
['attribute'=>'total_dsc',
'pageSummary'=>true
], // 'trans',
['attribute'=>'total_trans',
'pageSummary'=>true
],
['attribute'=>'total_margin_rp',
'pageSummary'=>true
],
['attribute'=>'total_margin_persen',
'pageSummary'=>true,
// 'groupSeparator' => '.',
'format'=>['percent',],
], // 'kode_area',
['class' => 'kartik\grid\ActionColumn'],
],
]); ?>
</div>
The problem you have is probably due to the fact that you have stored the actual percentage value in the database. So for example your 23% is actually the number 23 in your database table.
If you look at the documentation for the percent formatter in Yii2 here (http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asPercent()-detail) you will see that it says:
The value to be formatted. It must be a factor e.g. 0.75 will result
in 75%.
So for your data to show up correctly you either have to provide the data as a factor, use a different formatter or write your own formatting function.

How to concatenate two columns into an autocomplete field in yii2?

i have a auto complete field in my view whose code is as follows
<?php
$data = Members::find()
->select(['first_name as value', 'first_name as label','id as id'])
->asArray()
->all();
echo 'Name' .'<br>';
echo AutoComplete::widget([
'name' => 'member_name',
'clientOptions' => [
'source' => $data,
'minLength'=>'3',
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#receipt-member_id').val(ui.item.id);//#receipt-member_id is the id of hiddenInput.
}")],
]);
?>
auto complete works correctly, it shows the first name of all the 'members'.
But there can be many guys with similar first name, so what i want is to concatenate first_name and last_name. In normal dropdowns it can be done as follows
<?php
$models1 = Members::find()->all();
$data = array();
foreach ($models1 as $model1)
$data[$model1->id] = $model1->first_name . ' '. $model1->last_name;
echo $form->field($model, 'member_id')->dropDownList(
$data,
['prompt'=>'Select...']);
?>
how can i do this with the autocomplete widget?
try this way
$data = Members::find()
->select(['concat(first_name,last_name) as value', 'first_name as label','id as id'])
->asArray()
->all();
If you need space between 2 fields, use this:
$data = Members::find()
->select(['concat(first_name, SPACE(1), last_name) as name', 'first_name as label','id as id'])
->asArray()
->all();
SPACE() method will generate space accord the specific number you entered, for you :D

Yii 2.0 Select Pre-selected values from Database

I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.

Select2 add placeholder after creation

I have an existing Select2 dropdown that is generated by my framework (Yii). Unfortunately there's no way to specify a placeholder. Is there any way to add it afterwards?
The dropdown is generated as follow:
$form->inputRow(TbInput::TYPE_SELECT2, $model, 'id_tecnico', null,
array('data' => $utenti,'class' => 'span12 adaptContainerCssClass',));
There's not a programmatic way to do it with select2 options, but you can modify the text with jQuery after select2 has initialized:
$('.select2-chosen').text("New Placeholder Text");
The .select2-chosen element is the span that wraps the placeholder text.
There's how to add a default value for a dropDownList on Yii Framework.
echo $form->dropdownList($model,'variable', $data, array('prompt'=>'Choose One')); ?>
'pluginOptions' => array(
'placeholder' => 'Your place holder',
'width' => '40%',
'tokenSeparators' => array(',', ' '),
)
but if you want to retain the key value pairs just do it this way
$form->inputRow(TbInput::TYPE_SELECT2, $model, 'id_tecnico', null,
array('data' => array(-1=>"Your first Element")+$utenti,'class' => 'span12 adaptContainerCssClass',));
The + operator will add the array to your key value pair without changing the key values.

ZF2 form element description

in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here