Commit 2cd58c22 by semenov

1

parent 86e514c8
......@@ -43,7 +43,33 @@ class BackendController extends Controller
*/
public function actionSubmit()
{
if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax)
{
\Yii::$app->response->format = Response::FORMAT_JSON;
$response = [
'success' => false,
'message' => 'Произошла ошибка',
];
if ($formId = \Yii::$app->request->post(Form::FROM_PARAM_ID_NAME))
{
/**
* @var $modelForm Form
*/
$modelForm = Form::find()->where(['id' => $formId])->one();
$model = $modelForm->createValidateModel();
if ($model->load(\Yii::$app->request->post()) && $model->validate())
{
$response['success'] = true;
$response['message'] = 'Успешно отправлена';
}
return $response;
}
}
}
/**
......
......@@ -12,7 +12,8 @@ use skeeks\modules\cms\form\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
'modelForm' => $model->modelForm,
]); ?>
]);
?>
<? if ($fields) : ?>
<? foreach ($fields as $formField) : ?>
......
......@@ -35,4 +35,63 @@ class ActiveForm extends \skeeks\cms\base\widgets\ActiveForm
echo \yii\helpers\Html::hiddenInput(Form::FROM_PARAM_ID_NAME, $this->modelForm->id);
}
public function run()
{
parent::run();
$this->view->registerJs(<<<JS
$('#{$this->id}').on('beforeSubmit', function (event, attribute, message) {
return false;
});
$('#{$this->id}').on('afterValidate', function (event, attribute, message) {
var Jform = $(this);
var ajax = sx.ajax.preparePostQuery($(this).attr('action'), $(this).serialize());
new sx.classes.AjaxHandlerBlocker(ajax, {
'wrapper': '#' + $(this).attr('id')
});
//new sx.classes.AjaxHandlerNoLoader(ajax); //отключение глобального загрузчика
new sx.classes.AjaxHandlerNotifyErrors(ajax, {
'error': "Не удалось отправить форму",
}); //отключение глобального загрузчика
ajax.onError(function(e, data)
{
})
.onSuccess(function(e, data)
{
var response = data.response;
if (response.success == true)
{
$('input', Jform).each(function(i,s)
{
if ($(this).attr('name') != '_csrf' && $(this).attr('name') != 'sx-auto-form')
{
$(this).val('');
}
});
sx.notify.success(response.message);
} else
{
sx.notify.error(response.message);
}
})
.execute();
return false;
});
JS
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment