LXXIII LXXIII - 4 months ago 8x
Ajax Question

In a user's Edit Info form, how do I include the name of the field(s) and user's input value(s) in a response from the model

On a Yii2 project, in a user's Edit Info form (inside a modal):

I'm currently figuring out which fields were changed using the jQuery

method, and I'm grabbing their value with jQuery's

However, I want to do less with JavaScript and do more with Yii's framework.

I can see in the Yii debugger (after clicking into the AJAX POST request) that Yii is smart enough to know which fields were changed -- it's showing SQL queries that only
the fields that were changed.

What do I need to change in the controller of this action to have Yii include the name of the field changed -- including it's value -- in the AJAX response? (since my goal is to update the main view with the new values)

public function actionUpdateStudentInfo($id)
$model = \app\models\StudentSupportStudentInfo::findOne($id);
if ($model === null) {
throw new NotFoundHttpException('The requested page does not exist.');

$model->scenario = true ? "update-email" : "update-studentid";
if ($model->load(Yii::$app->request->post()) && $model->save()) {

return $this->renderAjax('_student_support_alert_success');
return $this->renderAjax("_edit_student_info",[
"model" => $model,

I'm currently returning a static success view.


You can use $model->dirtyAttributes just after load the data to get a $attrib => $value pair array.

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#getDirtyAttributes()-detail (this docs says:)

Returns the attribute values that have been modified since they are loaded or saved most recently.

The comparison of new and old values is made for identical values using ===.

public array getDirtyAttributes ( $names = null )

(sorry for formatting, sent by mobile)