Closed loveorigami closed 9 years ago
and also then i added rules to Post, as required, the item not saved
/**
* @ Rules from Post model
*/
public function rules()
{
return [
[['title', 'content'], 'required'],
[['content'], 'string'],
[['title'], 'string', 'max' => 255],
];
}
if I commented this rules and added to PostLang model - validation not working
// rules from PostLang model
public function rules()
{
return [
[['post_id', 'language'], 'required'],
[['post_id'], 'integer'],
[['content'], 'string'],
[['language'], 'string', 'max' => 6],
[['title'], 'string', 'max' => 255]
];
}
@loveorigami Can you paste code of findModel
method from your controller? It seems you doesn't call multilingual()
scope
Example
protected function findModel($id)
{
if (($model = Artist::find()->notRemoved()->multilingual()->andWhere(['id' => $id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Thanks. I changed PostController to
protected function findModel($id)
{
if (($model = Post::find()->multilingual()->andWhere(['id' => $id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
now added to postLang table 2 rows. It is fine.
But it is problem with validation and view
public function rules()
{
return [
[['title', 'content'], 'required'],
[['content'], 'string'],
[['title'], 'string', 'max' => 255],
[['active'], 'integer'],
];
}
if I added new post - post not created if I update post - post updated successfully
protected function findModel($id)
{
if (($model = Post::find()->localized('ru')->multilingual()->andWhere(['id' => $id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
but method localized('ru') not give effect
First of all... You should read docs. You don't need change locale to edit a translation. Both translations must be saved via one form. Validations rules work correctly. You get validation errors, because you configure your app to require all translations have title and content attributes, but your form have values of required attributes only for default language.
And so, you should remove ->localized('ru')
from findModel($id)
Also, your form should be similar to (if default language is Russian):
$form->field($model, 'title')->textInput(['maxlength' => 255]);
$form->field($model, 'title_en')->textInput(['maxlength' => 255]);
$form->field($model, 'content')->textInput();
$form->field($model, 'content_en')->textInput();
Then, a new model will have all required fields. I recommend you to use dynamic model if you doesn't need additional logic for translations.
Example from my app (attributes for different translations in different tabs)
Also if you set forceOverwrite
to false title and content will be required only for default language.
So, if current locale translation is not set default language translation will be used for output. I think it's very common.
Sorry for my english ). Доки я конечно же читал. Но... после того, как познакомился с вашим бихевиром в статье http://www.elisdn.ru/blog/40/yii-based-multilanguage-site-content-translations
Там рассматривался вариант в числе прочих с хранением переводов в основной таблице, префиксируя значения полей языками (title_en, title_ru)...
Оставшееся впечатление от статьи способствовало тому, что строки
$form->field($model, 'content')->textInput();
$form->field($model, 'content_en')->textInput();
я мысленно отнес к этому случаю и не рассматривал вовсе, поскольку у меня переводы хранятся в отдельной таблице.
Большое спасибо за пояснения и отдельная благодарность за полезное расширение!
Пошел пробовать и внимательно перечитывать инструкцию ) !
@loveorigami Удачи в начинаниях :)
I have small problem with this behavior
I think