marcodemurtas
marcodemurtas

Reputation: 35

why the controller don't read an if function loading the parameters?

this is my create function in my controller:

public function actionCreate()
    {
        $searchModel = new AutoriSearch();
        $model = new Autori();
        $model->RIDUtente = Yii::$app->user->getId();
        $model->Accettazione='SI';

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        
        if ($model->load(Yii::$app->request->post()) ) {
            
        $model->modified = date( 'y-m-d' );
        $model->creator_aut = Yii::$app->user->getId();
        $model->Utenteup_autori = Yii::$app->user->getId();
        if ($model->IParte) {
            $model->CampoRicerca = $model->IParte." ".$model->IIParte." ".$model->Prefisso." ".$model->Qualificazione." ".$model->IIEnte." ".$model->Sigla;
        }
            if($model->Accettazione == "" || $model->Accettazione == 'SI' ){
                $model->RIDAutore = $model->IDAutore;
            }
        $model->save();
        return $this->redirect(['view', 'id' => $model->IDAutore]);
    }

        return $this->render('create', [
            'model' => $model,
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,

        ]);
    }

The problem is that the record is not saved, and the system don't enter in the if function "if ($model->load(Yii::$app->request->post()) )". So this is a problem. In others controller it gives me no error, for this controller it gives me the error. This is strange. Can anyone help me fast please? It's very important!!!! Thank you.

This is the form:

<div class="autori-form">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
        'type' => ActiveForm::TYPE_HORIZONTAL,
        
    ]); ?>

    <?= $form->field($model, 'IParte', ['labelOptions'=>['style'=>'font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>'width:190px; height: 30px;'])->label('I Parte del nome') ?>

    <?= $form->field($model, 'IIParte', ['labelOptions'=>['style'=>'position: relative; top: -63px; left: 360px;font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:190px; position: relative; position: relative; top: -58px; left: 360px;'])->label('Autori:<br>II Parte del nome') ?>
    
    <?= $form->field($model, 'IIEnte', ['labelOptions'=>['style'=>'width: 100px; position: relative; top: -120px; left: 800px;font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:190px; position: relative; top: -116px; left: 800px;'])->label('Enti: Ente subordinato') ?>
<div style="position: relative; top: -120px;">
    <?= $form->field($model, 'Prefisso', ['labelOptions'=>['style'=>'font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:300px'])->label('Appellativi o altra parte del nome') ?>

    <?= $form->field($model, 'Sigla', ['labelOptions'=>['style'=>'font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:300px'])->label('Sigla dell\'ordine') ?>

    <?= $form->field($model, 'Qualificazione', ['labelOptions'=>['style'=>'font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:300px'])->label('Qualificazioni') ?>

    <?= $form->field($model, 'Fonte', ['labelOptions'=>['style'=>'font-size: 12px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:300px']) ?>

    <?= $form->field($model, 'RIDTipoAutore', ['labelOptions'=>['style'=>'font-size: 12px;']])->dropdownList(
        TipiAutore::find()
        ->select(['TipoAutore'])
        ->indexBy('IDTipoAutore')
        ->column(),
        ['prompt'=>'', 'style'=>' height: 30px; width:300px']
    )->label('Tipo Autore') ?>

    <?= $form->field($model, 'Accettazione', ['labelOptions'=>['style'=>'font-size: 12px;']])->dropdownList(
        Autori::find()
        ->select(['Accettazione'])
        ->indexBy('Accettazione')
        ->column(),
        ['style'=>' height: 30px; width:300px', 'onchange' => 'accettato(this)'] 
    )->label('Accettazione') ?>   

    

    <?php //$form->field($model, 'CampoRicerca')->textInput(['maxlength' => true]) ?>

    
    <?= $form->field($model, 'RIDUtente')->hiddenInput(['value'=> Yii::$app->user->identity->id])->label(false) ?>

     <?= $form->field($model, 'RIDAutore', ['labelOptions'=>['style'=>'font-size: 12px; position: relative; top: -28px;']])->textInput(['maxlength' => true, 'style'=>' height: 30px; width:300px; position: relative; top: -28px;', 'disabled' => true])->label('Forma accettata') ?>
    </div>
     <a name="button" id="btnMostra" style="position: relative; top: -199px; left: 540px; cursor: pointer;">Clicca per ricercare la forma accettata</a> 

    <script>
        function accettato(select) {
           console.log(select,$(select));
           $("#autori-ridautore").prop("disabled", $(select).val()!="NO"); 
        }
    </script>

    
    <?= Html::hiddenInput( 'modified', strtotime("now") ); ?>

    <div class="form-group" style="position: relative; top: -170px; left: 860px; width: 200px;">
        <?= Html::submitButton(Yii::t('app', 'Salva'), ['class' => 'btn btn-success']) ?>
        &nbsp;&nbsp;&nbsp;&nbsp;<?= Html::a('Annulla', ['autori/index'], ['class'=>'btn btn-info']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

RULES OF Autori.php:

public function rules()
    {
        return [
            [['RIDUtente', 'modified', 'RIDTipoAutore'], 'required'],
            [['RIDUtente', 'RIDAutore', 'RIDTipoAutore', 'creator_aut'], 'integer'],
            [['IParte', 'IIEnte'], 'string', 'max' => 100],
            [['IIParte', 'Qualificazione'], 'string', 'max' => 50],
            [['Prefisso'], 'string', 'max' => 30],
            [['Sigla'], 'string', 'max' => 10],
            [['Accettazione'], 'string', 'max' => 2],
            [['CampoRicerca'], 'string', 'max' => 400],
            [['Fonte'], 'string', 'max' => 3200],
            [['modified'], 'string', 'max' => 8],
        ];
    }

RULES OF AutoriSearch Model:

public function rules()

{
    return [
        [['IDAutore', 'RIDUtente', 'RIDAutore', 'RIDTipoAutore', 'creator_aut'], 'integer'],
        [['autoreMix', 'IParte', 'IIParte', 'Prefisso', 'Qualificazione',
        'IIEnte', 'Sigla', 'Accettazione', 'CampoRicerca',
        'Fonte', 'modified'], 'safe'],
    ];
}

Upvotes: 0

Views: 47

Answers (1)

Mahsa
Mahsa

Reputation: 383

your forms method is get but you are getting data by post and this is incompatible

 'method' => 'get',

 $model->load(Yii::$app->request->post()

Upvotes: 3

Related Questions