Hi there,
I am developping an admin interface in my yii app for managing translations.
I have defined the following Cform configuration array :
return array(
'title'=>t('Form_Translations_Title'),
'elements'=>array(),
'buttons'=>array(
'save'=>array(
'type'=>'submit',
'label'=>t('Form_Translations_Submit'),
),
),
);
And I populate it at runtime by browsing my sourceMessages DB table, and my available languages in my translatedMessages table.
This works since I am able to display the source messages and their translations in this Form_Translations_Title of mine.
The problem, is when i submit, the $_post var is empty.
Here’s the action code :
$translationsForm = $this->controller->loadTranslationsForm();
var_dump($_POST);
// check if translationsForm submitted some source messages and / or translations and save them
// note that there is no validation because the form doesn't have a model
// no validation neither in the rows because the TranslationsRows instances are not ActiveRecords
// this is why we enable validation in the save calls
if($translationsForm->submitted('save'))
{
foreach($translationsForm->elements as $translationRow)
{
$translationRow['source']->model->save();
foreach($translationRow['translations']->elements as $translation)
$translation->model->save();
}
$this->controller->redirect(array('translations'));
}
I suspect that my issue is that I didn’t define a model for the translationsForm. It doesn’t seem relevant.
Also, here’s the loadTranslationsForm() operations below.
Any ideas why i get a null post ?
If it’s for the reason i’m suspecting, then i believe it’s a bad behaviour from the submit logic, because I only want to populate models associated with subforms.
As you can see below, I already had to declare a useless model in my TranslationsRowForm to get the code working.
Maybe this could be part of the issue…
public function loadTranslationsForm()
{
$translations = TranslationsRow::getTranslationsRows();
if($translations===null)
throw new CHttpException(404,'The requested page does not exist.');
return new TranslationsGridForm($translations);
}
/*
* TranslationsGridForm is the CForm meant to update every source message and its translations in every target language.
* For each sourceMessage, it includes a TranslationsRowForm element
*/
class TranslationsGridForm extends GTForm
{
private $_translations;
public function __construct($translations)
{
parent::__construct('application.views.admin.forms.translationsGridForm');
$this->_translations = TranslationsRow::getTranslationsRows();
$this->model = $this->_translations[0];
// create translationsRowForm for each available sourceMessage
foreach($this->_translations as $translationsRow)
$this[$translationsRow->sourceModel->id] = new TranslationsRowForm($translationsRow);
}
}
class TranslationsRowForm extends GTForm
{
public function __construct(/*TranslationsRow*/ $translationsRow)
{
parent::__construct('application.views.admin.forms.translationsRowForm');
$this->model=$translationsRow;
// Add source message element
$this['source']=new CForm('application.views.admin.forms.translationsSourceMiniForm',$translationsRow->sourceModel);
// Add translations elements for each supported language
foreach($translationsRow->translationsModels as $translation)
{
$this['translations'][$translation->language] = new CForm('application.views.admin.forms.translationsTargetForm',$translation);
}
}
}