Wednesday, October 7, 2015

Create a widget on Yii


// protected/components/SubscriberFormWidget.php

class SubscriberFormWidget extends CWidget{
    //put your code here
    public $form;
    
     public function run()
    {
      $model=new Subscribe;

  // Uncomment the following line if AJAX validation is needed
  if(isset($_POST['ajax']) && $_POST['ajax']==='subscribe-form')
  {
   echo CActiveForm::validate($model);
   Yii::app()->end();
  }

  if(isset($_POST['Subscribe']))
  {
   $model->attributes=$_POST['Subscribe'];
   if($model->save())
    $this->redirect(array('view','id'=>$model->id));
  }

  $this->render('subscriberFormWidget',array(
   'modelsubs'=>$model,
  ));
                
      //  $this->render('subscriberFormWidget', array('modelsubs'=> new Subscribe()));
    }
    
}
// protected/components/views/subscriberFormWidget.php
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'subscribe-form',
        'action'=>Yii::app()->createUrl('subscribe/create'),
       'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            )
        ));
echo $form->error($modelsubs,'mobile_no'); 
 echo $form->textField($modelsubs,'mobile_no',array('size'=>60,'maxlength'=>254,'class'=>'subscribe-now', 'placeholder'=>'Enter Your Mobile Number …'));


echo CHtml::submitButton($modelsubs->isNewRecord ? 'Subscribe Now' : 'Save',array('class'=>'btn btn-info btn-lg'));
$this->endWidget();
Inside any view
$this->widget('SubscriberFormWidget');

No comments:

Post a Comment