I have inserting data from activeForm in yii
<?php $form = ActiveForm::begin([
'id' => 'register-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "<div class=\"\">{label}{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]);?>
<?= $form->field($model, 'firstname')->textInput(['placeholder' => 'First Name']) ?>
<?= $form->field($model, 'middlename')->textInput(['placeholder' => 'Middle Name']) ?>
<?= $form->field($model, 'lastname')->textInput(['placeholder' => 'Last Name']) ?>
Then i created a model and insert a data into db by createCommand
$db = Yii::$app->db->createCommand();
$db->insert('person', [..])->execute();
Here i have giving rules(required) for firstname and lastname.
So middle name value coming as NULL
If i give required for middlename, Then value is stored in db. otherwise it is NULL
You always need to have some validation rule for attribute you want to save via mass assignment.
E.g. just add one more rule and your middlename will be saved:
public function rules()
{
return [
[['firstname', 'lastname'], 'required'],
['middlename', 'string'], // rule for middlename
];
}
Related
I have a Fileinput field and I want to insert there screenshots one by one, using print screen, but Fileinput remove previous file. What can I do ?
use kartik\widgets\FileInput
echo '<label class="control-label">Add Attachments</label>';
echo FileInput::widget([
'model' => $model,
'attribute' => 'attachment_1[]',
'options' => ['multiple' => true]
]);
I want to use widget NumberControl in kartik's Form Bulder. But when I try this code, i have an empty form and there no errors. Couse I can't understand where is my mistake.
Please tell me if someone use a NumberControl in kartik's Form Builder.
use kartik\builder\Form;
echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 6,
'columnSize' => 'md',
'attributes' => [
'oi_count' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\number\NumberControl',
],
]
]);
According to this:https://demosbs3.krajee.com/number by default the type is "hidden", you should set it to "text".
I have following data :
Array
(
[category] => Array
(
[0] => d
[1] => 100
[2] => 100
[3] => 100
)
[volume] => Array
(
[0] => 100
[1] => 100
[2] => 100
)
[urgency] => Array
(
[0] => 100
[1] => 100
[2] => 100
)
[importance] => Array
(
[0] => 100
[1] => 100
[2] => 100
)
)
And I created DynamicModel for it with rules "each value should be integer" (added in 2.0.4).
$view_model = DynamicModel::validateData(compact('category', 'volume', 'urgency', 'importance'), [
[['category', 'volume', 'urgency', 'importance'], 'each', 'rule' => ['integer']],
]);
In view I have:
<?= $form->field($model, 'category[0]')->textInput() ?>
<?= $form->field($model, 'category[1]')->textInput() ?>
<?= $form->field($model, 'category[2]')->textInput() ?>
...
<?= $form->field($model, 'importance[2]')->textInput() ?>
Problem is, when I submit form with "d" in first input, I have errors on each "category" input:
What I do wrong?
You can use Each validator
Info: This validator has been available since version 2.0.4.
[
// checks if every category ID is an integer
['categoryIDs', 'each', 'rule' => ['integer']],
]
This validator only works with an array attribute. It validates if every element of the array can be successfully validated by a specified validation rule. In the above example, the categoryIDs attribute must take an array value and each array element will be validated by the integer validation rule.
rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.
allowMessageFromRule: whether to use the error message returned by the embedded validation rule. Defaults to true. If false, it will use message as the error message.
Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message.
This answer is applicable to ajax request scenarios
In your controller
use yii\base\Model;
use yii\widgets\ActiveForm;
use yii\web\Response;
public function actionCreate()
{
$models = [
'model1' => new Category,
'model2' => new Category,
];
if (Yii::$app->request->isAjax && Model::loadMultiple($models, Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
$validate = [];
$validate = array_merge(ActiveForm::validateMultiple($models), $validate);
// If you need to validate another models, put below.
// $validate = array_merge(ActiveForm::validate($anotherModel), $validate);
return $validate;
}
if (Model::loadMultiple($models, Yii::$app->request->post())) {
foreach($models as $key => $model) {
$model->save();
}
}
return $this->render('create', [
'models' => $models,
]);
}
In your view
<?php
$form = ActiveForm::begin([
'enableClientValidation' => false,
'enableAjaxValidation' => true,
]);
?>
<?= $form->field($models['model1'], '[model1]name')->textInput(); ?>
<?= $form->field($models['model2'], '[model2]name')->textInput(); ?>
<?= Html::submitButton('Create') ?>
<?php ActiveForm::end(); ?>
Here is the sample code from Yii2 checkboxList, I want to add custom class for each Item in checkboxList but I don't know how and where can I add that!
Could you please help me please ..
$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];
echo Html::checkboxList('CuisineId',$list2,$list,array('class' => 'test' ));
Thanks in advance.
If you want to add the same class, you should use itemOptions :
echo Html::checkboxList('CuisineId', $list2, $list, ['itemOptions'=>['class' => 'test']]);
Or if you want a custom class for each item, you should use item callback :
echo Html::checkboxList('CuisineId', $list2, $list, ['item'=>function ($index, $label, $name, $checked, $value){
return Html::checkbox($name, $checked, [
'value' => $value,
'label' => $label,
'class' => 'any class',
]);
}]);
Read more : http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#checkboxList()-detail
EDIT : add example
Just in case you only need to change the label options:
<?= Html::checkboxList('CuisineId', $list, $items, [
'itemOptions' => [
'labelOptions' => [
'style' => 'font-weight: normal',
'class' => 'some-custom-class',
],
],
]) ?>
Note: Everything you put inside itemOptions will be passed to Html::checkbox() as its own options when creating each checkbox. It means you can pass class, style, label, labelOptions, etc.
As you can use a index value inside the view when using a listview widget I wanted to change this index value as I use the same view on different places, so not every element that I use have the same index on the pages.
somepage.php
echo ListView::widget([
'summary' => false,
'viewParams' => [
'url' => '',
],
'index' => 'index + 4',//the idea
'options' => [
'class' => 'xxxx',
],
'itemOptions'=> [
'class' => 'xxxx',
],
'itemView' => '#frontend/views/product/_item_product_small',
'dataProvider' => $provider,
]);
frontend/views/product/_item_product_small.php
<div data-index="<?=$index?>">
// content
</div>
You need to create intermediate view for such cases:
somepage.php
echo ListView::widget([
// ...
'itemView' => '_view',
]);
_view.php
$this->render('#frontend/views/product/_item_product_small', ['index' => $index + 4]);