I am trying to hide the label for a specific field in _form.php without success.
I have tried couple of variation like, but none is working:
<?= $form->field($model, 'sample_text')->textArea('label'=>false) ?>
and alternate code:
<?= $form->field($model, 'sample_text')->textArea('label'=>'') ?>
What is the right approach to hide a label?
Ok, I found the solution.
<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>
Or you can modify template value for particular field and remove {label} part from it. I.e.:
<p><?= $form->field($page, 'image', [
'template' => '<div class=\"\">{input}</div><div class=\"\">{error}</div>'
])->fileInput() ?></p>
At time of writing after digging into the core code, I have found this to be the best solution to hide the label and prevent rendering the full field template with errors etc. for hiddenInput.
<?=
$form->field($model, 'address_uuid', [
'template' => '{input}',
'options' => ['tag' => false]
])->hiddenInput([
'readonly' => true,
])->label(false)
?>
<?= $form->field($model, 'password', [
'inputOptions'=>[
'class'=>'form-control',
'placeholder'=>'Password'
]
])->passwordInput()->label(false); ?>
<?= $sffForm->field($sffModel, 'url_keywords', ['enableLabel' => false])->textInput(['placeholder' => 'URL / keywords']) ?>
You can disable label, while creating form field class
$form->field($model, 'email', [
'inputOptions' => [
'enableLabel' => false,
]
])
The best way to hide the label in form input field, is to pass empty value to array on 'attributeLabels()' function in the model.
i.e you have input filed name 'client_name', so on the 'attributeLabels()' function's return array pass the empty string as array value
public function attributeLabels()
{
return [
'id' => 'ID',
'gender' => 'Gender',
'client_name' => '',
.
.
.
]
}
Related
I am modifying a system already developed in yii2 framework.
There is a functionality where a user views a list of customers and against each record there is a button for editing. When clicked, a form is displayed with values automatically filled in for the customer against whom the 'edit' button was clicked.
Now, the is a field for address which is filled with the database record id of the address instead of the name of the location. The id is a number, and the field was validated to check that the value is a number when the 'Update' button is clicked.
Problem:
Instead of filling the field with the number (i.e the id) I am requested by users to fill that field with the name of the location which is not a number and therefore will violate the validation rule.
I want to be able to make the value of the input field (i.e the value attribute) to be the id -a number so that it does not violate the set rule - but display the name of the location in the text input so that the users have a readable 'value' to look at.
How can I achieve this?
CODE
There was this:
<div class="col-sm-6">
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'options' => ['value' =>$model->village],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
The value in $model->village is the id of the village. This is what users do not want.
I tried doing this:
<div class="col-sm-6">
<?php
$villageName = Yii::$app->db->createCommand('SELECT village_name FROM v_village WHERE id='.$model->village)->queryOne();
$streetName = Yii::$app->db->createCommand('SELECT street FROM v_street WHERE street_id='.$model->street)->queryOne();
?>
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'options' => ['value' => $villageName['village'], 'id' => $model->village],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
But that violates the validation rule and the error I get says 'Village must be a number'
You have to set data array of your select.
<div class="col-sm-6">
<?php
$villageName = Yii::$app->db->createCommand('SELECT village_name FROM v_village WHERE id='.$model->village)->queryOne();
$streetName = Yii::$app->db->createCommand('SELECT street FROM v_street WHERE street_id='.$model->street)->queryOne();
?>
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'data' => [$model->village => $villageName['village']],
'pluginOptions' => [
'allowClear' => true //are you sure you want to be able to clear the option? If you save it empty the query above will break
],
]); ?>
</div>
I'm not sure what is your purpose, but if you have only this value you can just show it like text.
If you want to be able to change the village you code must be like this:
<?= $form->field($model, 'village')->widget(\kartik\widgets\Select2::classname(), [
'data' => ArrayHelper::map(Village::find()->all(), 'id', 'village_name'),
'pluginOptions' => [
'allowClear' => true //are you sure you want to be able to clear the option? If you save it empty the query above will break
],
]); ?>
Also you can remove the query and set data like this:
'data' => ArrayHelper::map(Village::find()->where(['id' => $model->village])->all(), 'id', 'village_name')
I am just starting Yii2 framework.
I want to create a dropdown list which is 1 to 10 and a submit button
Once select the option and click the button should go to next page to show the number I choose.
In my view file : index.php
use yii\widgets\ActiveForm;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'QTY')->dropDownList(range(1, 10)) ?>
<?= Html::submitButton('Buy', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
Then when I go to the page it gave me 'Undefined variable: model' at dropdown list there.
What should I do to make it correct?
And what is the different between Html and CHtml?
Thanks.
this code is form.php not index.php.
because we can see, there are active form.
your model is undefined maybe you write the wrong code
this is example of controller index.php
public function actionIndex()
{
$searchModel = new PersediaanBarangSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Html and Chtml is the same
in Yii1=CHtml
in Yii2=Html
This is ment to be pagination? If yes use default functionality of the grid view.
This goes to controller:
$query = Post::find()->where(['status' => 1]);
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'created_at' => SORT_DESC,
'title' => SORT_ASC,
]
],
]);
return $this->render('path_to_view',['dataProvider'=>$provider]);
Read more
This goes to view:
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'created_at:datetime',
// ...
],
]);
Read more
Actually you model is not loaded, Please check below example.
public function actionIndex($id = Null)
{
$data=array();
$data['model'] = !empty($id) ? \app\models\YourModel::findOne($id) : new \app\models\YourModel();
return $this->render('index', $data);
}
I would like to make an input field appear only in the 'create' form and not in the 'update' form. How can I achieve this?
You can do that by following way
if ($model->isNewRecord){
echo $form->field($model, 'password')->passwordInput(['maxlength' => true, 'placeholder' => "Enter password"]);
}
You can use $model->isNewRecord in many creative ways that can help you do some stuff depending on create/update.
i.e.
Hide a field
if ($model->isNewRecord){
echo $form->field($model, 'title')->textInput()
}
Change a style, class or property like readonly
$isEditable = (model->isNewRecord);
$class = (model->isNewRecord)?'class-a':'class-b';
echo $form->field($model, 'title')->textInput([
'readonly'=>$isEditable,
'disabled' => $isEditable,
'class'=>$class])
Changing a label
Html::submitButton( ($model->isNewRecord)
?'Create'
:'Update',
['class' => 'btn btn-success','id' => 'submit-btn',]);
Show a different button
echo ($model->isNewRecord)
? Html::a('Cancel', ['index'], ['class' => 'btn btn-warning'])
: Html::a('Close', ['otherplace'], ['class' => 'btn btn-primary']);
You can play a lot with $model->isNewRecord and actually modify some stuff depending on the state.
And many other stuff.
i've been trying to create yii\jui\Dialog that is triggered on button click in the form view. the Dialog itself will contain a Gridview Widget within it using Yii 2.0.
i have managed to create the dialog and fill it with the Gridview widget. the only problem that i encounter is that the Gridview within the dialog is can't be filtered properly. the filter process is fine though, but every time i filtered the Gridview, the Dialog modal will also be closed.
i also have tried to use Pjax by encapsulate the gridview within Pjax widget. this time the modal Dialog won't be closed. but then it can only filter 1 time. once the Gridview is filtered then we can't filter it anymore.
below is my "_form" view : (i am using button called "cari" to trigger the pop up dialog)
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'judul')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'isi')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'kategori_id', [
'addon' => ['append' =>
['content'=>
Html::button('x', ['class'=>'btn btn-default','name' => 'del_kat', 'id' => 'del_kat', 'onclick' => '$("#kat_id").val("");']) . PHP_EOL .
Html::button('Cari', ['class'=>'btn btn-primary','id' => 'modal_kat', 'onclick' => '$("#kategori_dialog").dialog("open"); return false;' ]),
'asButton' => true
]
]
])->textInput(['id'=> 'kat_id', 'readonly' => true]) ?>
here is my GridView Code :
GridView::widget([
'dataProvider' => $kategoriModel->search(Yii::$app->request->queryParams),
'filterModel' => $kategoriModel,
'columns' => [
'kategori_id',
'nama',
[
'label' => 'test',
'format' => 'raw',
'value' => function ($data) {
return Html::button('+', ['class'=>'btn btn-default','id' => 'modal_kat', 'onclick' => '$("#kategori_dialog").dialog("close"); $("#kat_id").val("'.$data->kategori_id.'");' ]);
},
],
],
]);
and here is Dialog Code :
Dialog::begin([
'id' => 'kategori_dialog',
'clientOptions' => [
'modal' => true,
'autoOpen'=>false,
'title'=>'List Kategori',
'width'=>'auto',
],
]);
Pjax::begin(['id' => 'kategori-pjax', 'enablePushState'=>false]);
echo $kategoriGrid;
Pjax::end();
Dialog::end();
as you guys can see, i tried to use Pjax but i couldn't make it work as expected (as i can only filter the Gridview once then it can be filtered again). i remember doing this just fine back in Yii 1.1 (i was using CJuiDialog back).
so i wonder if i am missing something here in Yii 2.0? i am kinda new to Pjax stuff, so i think i miss some properties that have to be set in the Pjax widget.
in my case, the solution to this is by using Kartik's Gridview. i installed it and set the Pjax property to true.
use kartik\grid\GridView;
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $model,
'columns' => $gridColumns,
'pjax' =>true,
]);
and now i can successfully filter the grid within the dialog. it looks like Kartik has better implementation when wrapping the Grid with Pjax Container.
i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>