это потому что вы добавили
return $this->renderAjax('create', [
'model' => $model,
]);
В части else
и сценарий в первой if
.
Также используйте только type="text/javascript"
вместо javascript
, хотя браузер имеет этот тип по умолчанию, теперь вы также можете пропустить эту часть и использовать
$script=<<<JS
alert("Produdcto already exists");
JS;
$this->getView()->registerJs($js,\yii\web\View::POS_READY)
Вы должны изменить свою функцию на
/**
* Creates a new Producto model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Producto();
if ($model->load(Yii::$app->request->post())){
if(($productos = ['nombre_producto' => Yii::$app->request->get('nombre_producto')])!=null){
$script=<<<JS
alert("Produdcto already exists");
JS;
$this->getView()->registerJs($js,\yii\web\View::POS_READY)
}else {
if($model->save()){
return $this->redirect(['index']);
}
}
}
return $this->renderAjax('create', [
'model' => $model,
]);
}