Answer the question
In order to leave comments, you need to log in
Yii2 ActiveRecord how to replace value with ID on save?
I wanted to use yii \ jui \ AutoComplete, I spent half a day, and I still can’t fasten it so that not the value but id is saved.
Here is the controller
$scope = new Scope;
$scopeItems = $scope->find()
->select(['scope as value', 'scope as label', 'id as id'])
->asArray()
->all();
$vacancy = new Vacancy();
if ($vacancy->load(Yii::$app->request->post()))
{
$vacancy->scope_id = $scope->id;
if ($vacancy->save()){
$vacancy = new Vacancy();
}
else {
var_dump($vacancy->getErrors());
}
<div class="vacancy-form">
<?php Pjax::begin(['id' => 'new_vacancy']) ?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($vacancy, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($vacancy, 'text')->textarea(['rows' => 6]) ?>
<?= $form->field($vacancy, 'date')->widget(DatePicker::className(),['dateFormat' => 'dd-MM-yy', 'language' => 'ru']) ?>
<?= $form->field($vacancy, 'minprice')->textInput() ?>
<?= $form->field($vacancy, 'maxprice')->textInput() ?>
<?= $form->field($vacancy, 'employer_id')->textInput() ?>
<?= $form->field($scope, 'scope')->widget(AutoComplete::className(), [
'clientOptions' => [
'source' => $scopeItems,
'minLength'=>'2',
'options' => ['class' => 'form-control'],
]]) ?>
<?= $form->field($vacancy, 'skill_id')->textInput() ?>
<?= $form->field($vacancy, 'jobtime_id')->textInput() ?>
<?= $form->field($vacancy, 'city_id')->textInput() ?>
<?= $form->field($vacancy, 'mtop')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($vacancy->isNewRecord ? 'Create' : 'Update', ['class' => $vacancy->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question