Codxplore.com

Latest news:

New android application Islamic Video has been released. its a free islamic video application for android smartphone. Download now!

Yii2 Validate Only Create Action



 

Its called 'scenarios': http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

in model:

public function rules()
{
    return [
        ['title', 'required', 'on' => 'create']
    ];
}

in controller:

public function actionCreate()
{
     $model = new Item();
     $model->scenario = 'create';
     ...
}

You'd better define scenario as a constant though.

 

source : http://stackoverflow.com/questions/29449019/yii2-validate-only-create-action


Views: 3740

Yii ajax form submit



Form code :

 $form = $this->beginWidget('CActiveForm', array(

                        'id' => 'aa--aprvl--setup-_form-form',
                        // Please note: When you enable ajax validation, make sure the corresponding
                        // controller action is handling ajax validation correctly.
                        // See class documentation of CActiveForm for details on this,
                        // you need to use the performAjaxValidation()-method described there.
                        'enableAjaxValidation' => false,
                    ));
                    ?>

                   

                        labelEx($model, 'TBL_NM'); ?>
                        textField($model, 'TBL_NM', array('class' => 'form-control')); ?>
                        error($model, 'TBL_NM'); ?>
                   


                    hiddenField($model, 'APRVL_GRP_MID', array('class' => 'form-control')); ?>


                   

                        labelEx($model, 'IS_ACT'); ?>
                        dropDownList($model, 'IS_ACT', getYesNoArray(), array('class' => 'form-control')); ?>
                        error($model, 'IS_ACT'); ?>
                   


                   

                                                echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('approvalGrpSetting/addSetting')),
                                array(
                                    'data' => 'js:jQuery(this).parents("form").serialize()+
                                     "&request=ajax"',
                                    'success' => 'function(data){
                                        $("#msg").html(data);
                                    }'
                                ),
                                array(
                                    'id' => 'ajaxSubmitBtn',
                                    'name' => 'ajaxSubmitBtn'
                                )
                         );
                        ?>
                   


                        endWidget(); ?>

               

 

Controller code :

public function actionAddSetting()
    {
        //debugPrint($_POST);
        $model = new AA_APRVL_SETUP();

        if (isset($_POST['AA_APRVL_SETUP'])) {
            $model->attributes = $_POST['AA_APRVL_SETUP'];
            $model->MID = Yii::app()->midcodeHelper->getMid('AA_APRVL_SETUP');
            if ($model->save()) {
                echo '

Successfully added

';
            }
            else{
                echo CHtml::errorSummary($model);
            }
        }
    }

 


Views: 967

Subscribe Us


Follow Us