I think that I am missing something simple here, I have a new installation of Yii 2 and a ajax validated form that does not post.
I echo out the time in the view and I see the time update when the submit button is clicked but $_POST is null.
In the log, I am seeing my info statments:
application POST =
application Attempting to load the model.
application Model was not loaded, rending the default view.
My first thought was the ActiveForm’s ID, but nothing seems to do anything.
When I set enableAjaxValidation=false, nothing changes.
Help Please
The Form code
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper; //Needed for the dropdown list
use app\models\Modes;
use app\models\Schedules;
/* @var $this yii\web\View */
/* @var $model app\models\SettingsForm */
/* @var $form yii\widgets\ActiveForm */
/* @var $account app\models\Accounts */
.... omitting html code not relevant...
<div class="settings-form">
<?php $form = ActiveForm::begin(['id' => 'settings-form','enableAjaxValidation'=>true]); ?>
<?= $form->field($model, 'ModesID')->dropDownList(ArrayHelper::map(Modes::find()->all(), 'idModes', 'Name')) ?>
<?= $form->field($model, 'SchedulesID')->dropDownList(ArrayHelper::map(Schedules::find()->all(), 'idScheduled', 'Name')) ?>
<?= $form->field($model, 'SendEmail')->checkbox() ?>
<?= $form->field($model, 'SendText')->checkbox() ?>
<div class="form-group">
<?= Html::submitButton('Update', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
echo var_dump($_POST);
echo '<br><br>';
$date = date('m/d/Y h:i:s a', time());
echo $date;
?>
The Controller code
use Yii;
use app\models\Accounts;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\Response; //Needed for AJAX
use app\models\User;
use \app\models\SettingsForm;
use app\models\EmailForm;
use app\models\PasswordForm;
use app\models\ServerForm;
use \app\models\ProviderForm;
.... omitting actions and functions not relevant...
/**
* Shows the basic account settings
* @return mixed
*/
public function actionIndex()
{
$account = User::getAccount();
if(!isset($account)) return $this->redirect(['site/logout']);
$model= new SettingsForm();
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::info('Ajax request loaded!');
Yii::$app->response->format = Response::FORMAT_JSON;
return \yii\widgets\ActiveForm::validate($model);
}
Yii::info( 'POST = ' . var_dump(Yii::$app->request->post()));
Yii::info('Attempting to load the model.');
if($model->load(Yii::$app->request->post()))
{
Yii::info('Model loaded');
//change the account
$account->ModesID = $model->ModesID;
$account->SchedulesID = $model->SchedulesID;
$account->SendEmail = $model->SendEmail;
$account->SendText = $model->SendText;
if($account->save())
{
Yii::info('Model was saved');
Yii::$app->session->setFlash('formSave');
return $this->refresh();
}
else
{
Yii::info('Model was not saved');
Yii::$app->session->setFlash('formError');
return $this->refresh();
}
}
Yii::info('Model was not loaded, rending the default view.');
return $this->render('index', ['model' => $model,'account'=>$account]);
}