I’m using the multimodelform extension.
But I’m struggling with the following problem:
I want to use ajax validation.
[b]The problem now is: How can I get the controller to validate the multimodelform and (if there is no child-model or not all required fields are filled), to display the error-summary as well for the multimodelform-fields ?
[/b]
With this code it only highlights only the fields in the main model if not valid…
Further Explanation:
-
There are 2 models: Document and Documentpos
-
I’m using a function “LoadForm” to load the subform into the create form.
-
The sub-view which contains the Document-Form and the Multimodelform for Documentpos looks like following:
$form = $this->beginWidget(
'GxActiveForm', array(
'id' => 'document-form',
'action' => '' . $currentAction . '/id/' . $id,
'enableAjaxValidation' => true,
));
DocumentController.php
public function actionCreate()
{
Yii::import('ext.multimodelform.MultiModelForm');
$deleteItems = '';
$formName = '';
$currentAction = Yii::app()->session['currentAction'];
$document = new Document;
$documentpos = new Documentpos;
$validatedDocumentpos = array();
$errorIndex = null;
$masterValues = array('dcp_dcid' => $document->dc_id);
if (Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate(array($document));
// echo MultiModelForm::validate($documentpos, $validatedDocumentpos, $deleteItems, $masterValues);
$errors = $document->getErrors();
if ($errors) {
ChromePhp::error('errors: ' . print_r($errors, true) . '<br/>' . $document->dc_id);
} else {
ChromePhp::info('output:>>> ' . $document->dc_id);
}
Yii::app()->end();
}
if (isset($_POST['Document'])) {
$document->attributes = $_POST['Document'];
$detailOK = MultiModelForm::validate($documentpos, $validatedDocumentpos, $deleteItems, $masterValues);
if ( //validate detail before saving the master
$detailOK == true
) {
if (MultiModelForm::validate($documentpos, $validatedDocumentpos, $deleteItems, $masterValues)) {
if ($detailOK && empty($validatedDocumentpos)) {
Yii::app()->user->setFlash('error', 'No detail submitted ');
$detailOK = false;
} else {
if ($document->save()) {
MultiModelForm::save($documentpos, $validatedDocumentpos, $deleteItems, $masterValues);
$this->redirect(array('update', 'id' => $document->dc_id));
}
Yii::app()->user->setFlash('info', 'OK Detail submitted & saved');
$detailOK = true;
}
}
}
}
$this->render(
'create', array(
'formName' => $formName,
'model' => $document,
'validatedItems' => $validatedDocumentpos,
//submit the documentpos and validatedDocumentpos to the widget in the edit form
'documentpos' => $documentpos,
'currentAction' => $currentAction,
)
);
}
Can someone help me with this? I just don’t get it after hours and hours (already weeks).