for the few who use this framework, I am with a problem. There is no way for the validator to work properly, when using it it works and it throws the error message but it also validates it when doing an update, something that should not and does not let me update. I have reviewed on the internet what refers to scenarios and it does not work for me either. Create a scenario 'create' and continue doing the same or directly does not validate anything. Does anyone know how to solve it?
public function actionCreate()
{
$model = new Marcas();
$model->scenario = 'create'; // única linea que agregue aca
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$datos = [
'accion' => 'create',
];
if ($model->save()) {
echo Json::encode($datos);
}
} else {
return $this->render('create', [
'model' => $model
]);
}
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->save()) {
Yii::$app->getSession()->setFlash('success',[
'message' => Html::encode('El registro se actualizo correctamente'),
]);
}else{
Yii::$app->getSession()->setFlash('error',[
'message' => Html::encode('El registro no se pudo actualizar'),
]);
}
return $this->redirect('index');
} else {
return $this->render('update', ['model' => $model]);
}
}
public function actionValidation() {
$model = new Temporadas;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
}
and this is in the model
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'] = ['codigo','nombre', 'cuit'];
return $scenarios;
}
public function rules()
{
return [
[['codigo','nombre'],'required'],
['codigo','unique','on' => 'create'],
[['codigo'], 'string', 'max' => 2],
[['nombre', 'color_marca'], 'string', 'max' => 50],
[['cuit'], 'string', 'max' => 13],
];
}
This is the view
<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
use demogorgorn\ajax\AjaxSubmitButton;
use yii\web\View;
/* @var $this yii\web\View */
/* @var $model backend\models\Marcas */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="form-group mt-3 card-grid p-3">
<?php Pjax::begin(['id' => 'pjax-container']); ?>
<?php $form = ActiveForm::begin(
[
'id' => $model->formName(),
'enableClientValidation' => true,
'enableAjaxValidation' => true,
'validationUrl' => Url::toRoute('marcas/validation'),
'fieldConfig' => ['errorOptions' => ['encode' => false, 'class' => 'd-none']],
'errorSummaryCssClass' => 'alert alert-danger',
]
); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'codigo')->textInput(['maxlength' => true, 'autocomplete' => "off", 'autofocus' => true]) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'nombre')->textInput(['maxlength' => true, 'autocomplete' => "off"]) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'cuit')->textInput(['maxlength' => true, 'autocomplete' => "off"]) ?>
</div>
</div>
<div class="row">
<div class="col-md-8">
<!-- BOTON GUARDAR AJAX -->
<?php AjaxSubmitButton::begin([
'label' => Yii::t('app', 'Guardar'),
'useWithActiveForm' => $model->formName(),
'ajaxOptions' => [
'type'=>'POST',
'url'=>$model->isNewRecord ? 'create' : Url::to(['update', 'id' => $model->codigo]),
'success' => new \yii\web\JsExpression('function(data){
data = JSON.parse(data);
if(data.accion=="create"){
toastr.success("Se registro correctamente")
}
$.pjax.reload({container:"#pjax-container"});
}')
],
'options' => ['class' => 'btn btn-primary', 'type' => 'submit'],
]);
AjaxSubmitButton::end(); ?>
<?= Html::a(Yii::t('app', 'Cancelar'), ['cancelar'], ['class'=>'btn btn-danger']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
I have also tried other things that I saw, but nothing works. I'm doing this in an advanced template, not in the basic one. I do not know what other relevant data I can add because the database is perfect, what I'm going for is that the validator works correctly, just that I do not want it to do so in the update, that's why it creates that scenario and does not work. That's the only code I add.