Hi All,
My adoption to yii2 is very slow. It seems like learning new framework. Please help me on this.
I utilized many ajax form submission in my projects since they are faster then reloading everything ( I might be wrong here! But, using ajax is more convenient to me.). Below is my code in yii 1
public function actionNextCode() {
$stat = 'error';
$message = 'Error in form submission not as Ajax!';
$title = 'Generate new code';
$nextCode = 'XXX';
if (Yii::app()->request->isAjaxRequest && isset($_POST['Partner'])) {
$model = new Partner;
$model->attributes = $_POST['Partner'];
if ($model->code == null || $model->code == '') {
$model->code = 'XX';
}
$criteria = new CDbCriteria;
$criteria->condition = 'code LIKE :code';
$criteria->params = array(':code' => $model->code . '%');
$criteria->order = 'code DESC';
$latestModel = Partner::model()->find($criteria);
if ($latestModel !== null) {
$number = $latestModel->code;
preg_match("#([a-z]+)([\d]+)#i", $number, $matches);
$latestNum = $matches[2];
$newNum = ((int) $latestNum) + 1;
$model->code = $matches[1] . str_pad($newNum, strlen($latestNum), '0', STR_PAD_LEFT);
} else {
$model->code .= '01';
}
$stat = 'success';
$message = 'New code is generated successfully.';
$title = 'Generate new code';
$nextCode = $model->code;
}
echo CJSON::encode(array(
'status' => $stat,
'message' => $message,
'title' => $title,
'nextCode' => $nextCode,
));
Yii::app()->end();
}
This is usually used to generate new value. It is called by pressing a button in a form. The process is using ajax. Now, when turn to yii2, I completely have no clue. Can anyone help guide me on turning this into pjax? FYI, I use message and title to display message using toastr. In yii2, I prefer to use krajee/growl.
Thank you in advance.