Does not saves from fields
Closed this issue · 3 comments
vasyl-dmytruk commented
I have model class Gender:
class Gender extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'ml' => [
'class' => MultilingualBehavior::className(),
'languages' => [
'uk' => 'Ukrainian',
'ru' => 'Russian',
'en' => 'English',
],
//'languageField' => 'language',
//'localizedPrefix' => '',
//'requireTranslations' => false',
//'dynamicLangClass' => true',
'langClassName' => GenderLang::className(), // or namespace/for/a/class/PostLang
'defaultLanguage' => 'uk',
'langForeignKey' => 'gender_id',
'tableName' => "{{%gender_lang}}",
'attributes' => [
'name', 'short_name',
]
],
];
}
public static function find()
{
return new MultilingualQuery(get_called_class());
}
}
and want to save it via ActiveForm fields in view like this:
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->field($model, 'id')->textInput() ?>
<div class="row">
<div class="col-md-6">
<?php echo $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'short_name')->textInput(['maxlength' => 255]) ?>
</div>
</div>
<div class="row">
<div class="col-md-6">
<?php echo $form->field($model, 'name_ru')->textInput(['maxlength' => 255]) ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'short_name_ru')->textInput(['maxlength' => 255]) ?>
</div>
</div>
<div class="row">
<div class="col-md-6">
<?php echo $form->field($model, 'name_en')->textInput(['maxlength' => 255]) ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'short_name_en')->textInput(['maxlength' => 255]) ?>
</div>
</div>
<div class="form-group">
<?php echo Html::submitButton($model->isNewRecord ? Yii::t('backend', 'Create') : Yii::t('backend', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
but it does not work, non of multilanguage fields does not save.
If I fill data directly in database, then it shows this fields.
P.S. Sorry for my English.
vasyl-dmytruk commented
I have resolved this issue. I add rules to my model:
public function rules()
{
return [
[['name', 'short_name', 'name_ru', 'short_name_ru', 'name_en', 'short_name_en'], 'safe'],
];
}
OmgDef commented
@SnapGet You should use rules like this
public function rules()
{
return [
[['name', 'short_name'], 'string', 'max' => 255] ,
];
}
vasyl-dmytruk commented
Understood, thank you very much. I am beginner with yii.