linccce

Full Modal Example

Jun 18th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.04 KB | None | 0 0
  1. View you want to call modal at:
  2. /* Some styles for optimal form view */
  3.  
  4. <style>
  5. #myModal.modal-body{
  6.     max-height: 650px;
  7.     overflow-y: auto;
  8. }
  9. #myModal{
  10.     top: 50px;
  11. }
  12. </style>
  13.  
  14. /*End of styles*/
  15.  
  16. /* Start of Modal code */
  17. <?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal',
  18.         'htmlOptions'=>array(
  19.             'aria-hidden'=>true,
  20.             'role'=>'dialog',
  21.             ))); ?>
  22. <div class="modal-header">
  23.     <a class="close" data-dismiss="modal">&times;</a>
  24.     <h4><?php Yii::t('svsite','Register') ?></h4>
  25. </div>
  26. <div class="modal-body">
  27.     </div>
  28. <div class="modal-footer">
  29.     <?php $this->widget('bootstrap.widgets.TbButton', array(
  30.         'type'=>'primary',
  31.         'label'=>Yii::t('svsite','Register'),
  32.         'url'=>'#',
  33.         'htmlOptions'=>array('onclick' => '$("#registration-form").submit()'),
  34.         'ajaxOptions'=>array(
  35.             'type'=>'POST',
  36.         )
  37.     )); ?>
  38.     <?php $this->widget('bootstrap.widgets.TbButton', array(
  39.         'label'=>'Close',
  40.         'url'=>'#',
  41.         'htmlOptions'=>array('data-dismiss'=>'modal'),
  42.     )); ?>
  43. </div>
  44. <?php $this->endWidget(); ?>
  45. /* End of Modal code */
  46.  
  47. /* Button that invokes Modal */
  48. <?php $this->widget('bootstrap.widgets.TbButton',
  49.         array('label'=>'Click me',
  50.         'ajaxOptions'=>array(
  51.             'type'=>'post',
  52.             'dataType'=>'json',
  53.             'url'=>array(controller/register),
  54.             'success'=>'function(data){
  55.                 $("#myModal .modal-header h4").text( data.header);
  56.                 $("#myModal  .modal-body").html(data.body);
  57.             }'),
  58.             'type'=>'primary',
  59.             'htmlOptions'=>array(
  60.                 'data-toggle'=>'modal',
  61.                 'data-target'=>'#myModal',
  62.                 'id' => 'open-modal-'.uniqid(),
  63.             ),
  64.         )); ?>
  65. /* End of buton */
  66. Form view file:
  67. <?php
  68. $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
  69.     'id'=>'registration-form',
  70.         'action'=>array('site/register'),
  71.     'enableAjaxValidation'=>true,
  72.         'clientOptions'=>array(
  73.             'validateOnSubmit'=>true, //Validation options
  74.             'validateOnChange'=>true,
  75.             'validateOnType'=>true,
  76.         ),
  77. )); ?>
  78.     <p class="help-block">Fields with <span class="required">*</span> are required.</p>
  79.  
  80.     <?php echo $form->errorSummary($model); ?>
  81.  
  82.     <?php echo $form->textFieldRow($model,'name',array('class'=>'span5','maxlength'=>255, 'labelOptions'=>array('label'=>false), 'placeholder'=>'Enter your name')); ?>
  83.  
  84.     <?php echo $form->textFieldRow($model,'surname',array('class'=>'span5','maxlength'=>255, 'placeholder'=>'surname')); ?>
  85.  
  86.     <?php echo $form->textFieldRow($model,'email',array('class'=>'span5','maxlength'=>255)); ?>
  87.  
  88.     <?php echo $form->passwordFieldRow($model,'password',array('class'=>'span5','maxlength'=>255)); ?>
  89.        
  90.     <?php echo $form->passwordFieldRow($model,'password_repeat',array('class'=>'span5','maxlength'=>255)); ?>
  91.  
  92.     <?php echo $form->textFieldRow($model,'city',array('class'=>'span5','maxlength'=>255)); ?>
  93.        
  94.         <?php echo $form->captchaRow($model, 'captcha', array('showRefreshButton' => true)  ); ?>
  95.  
  96. <?php $this->endWidget(); ?>
  97.  
  98. Model of the form:
  99.  
  100. class User extends CActiveRecord
  101. {
  102.  
  103.     public $password_repeat;
  104.     private $salt = $this->email;
  105.     public $captcha;
  106.     /**
  107.      * @return string the associated database table name
  108.      */
  109.     public function tableName()
  110.     {
  111.         return 'base_user';
  112.     }
  113.  
  114.     /**
  115.      * @return array validation rules for model attributes.
  116.      */
  117.     public function rules()
  118.     {
  119.         // NOTE: you should only define rules for those attributes that
  120.         // will receive user inputs.
  121.         return array(
  122.             array('email, password, password_repeat, captcha', 'required'),
  123.                         array('email', 'unique'),
  124.                         array('email', 'email'),
  125.             array('name, surname, email, password', 'length', 'max'=>255),
  126.                         array('captcha', 'captcha'),
  127.                         array('password_repeat', 'safe'),
  128.                         array('password', 'compare', 'compareAttribute' => 'password_repeat'),
  129.             array('id, name, surname, email, password, city', 'safe', 'on'=>'search'),
  130.         );
  131.     }
  132.  
  133.     /**
  134.      * @return array relational rules.
  135.      */
  136.     public function relations()
  137.     {
  138.         // NOTE: you may need to adjust the relation name and the related
  139.         // class name for the relations automatically generated below.
  140.         return array(
  141.         );
  142.     }
  143.  
  144.     /**
  145.      * @return array customized attribute labels (name=>label)
  146.      */
  147.     public function attributeLabels()
  148.     {
  149.         return array(
  150.             'id' => 'ID',
  151.             'name' => 'Name',
  152.             'surname' => 'Surname',
  153.             'email' => 'E-mail',
  154.             'password' => 'Password',
  155.             'city' => 'City',
  156.         );
  157.     }
  158.  
  159.     /**
  160.      * Retrieves a list of models based on the current search/filter conditions.
  161.      *
  162.      * Typical usecase:
  163.      * - Initialize the model fields with values from filter form.
  164.      * - Execute this method to get CActiveDataProvider instance which will filter
  165.      * models according to data in model fields.
  166.      * - Pass data provider to CGridView, CListView or any similar widget.
  167.      *
  168.      * @return CActiveDataProvider the data provider that can return the models
  169.      * based on the search/filter conditions.
  170.      */
  171.     public function search()
  172.     {
  173.         // @todo Please modify the following code to remove attributes that should not be searched.
  174.  
  175.         $criteria=new CDbCriteria;
  176.  
  177.         $criteria->compare('id',$this->id);
  178.         $criteria->compare('name',$this->name,true);
  179.         $criteria->compare('surname',$this->surname,true);
  180.         $criteria->compare('email',$this->email,true);
  181.         $criteria->compare('password',$this->password,true);
  182.         $criteria->compare('city',$this->city,true);
  183.  
  184.         return new CActiveDataProvider($this, array(
  185.             'criteria'=>$criteria,
  186.         ));
  187.     }
  188.  
  189.     /**
  190.      * Returns the static model of the specified AR class.
  191.      * Please note that you should have this exact method in all your CActiveRecord descendants!
  192.      * @param string $className active record class name.
  193.      * @return User the static model class
  194.      */
  195.     public static function model($className=__CLASS__)
  196.     {
  197.         return parent::model($className);
  198.     }
  199.        
  200.         protected function  beforeSave()
  201.         {
  202.             $this->password = sha1($this->salt . $this->password);
  203.             $this->password_repeat = sha1($this->salt . $this->password_repeat);
  204.             return parent::beforeSave();
  205.         }
  206. }
  207.  
  208. Function in the Controller:
  209.  
  210.        public function actionRegister()
  211.     {
  212.                 $model = new User();
  213.                 $header = Yii::t('svsite','Register');
  214.                 if (Yii::app()->request->isAjaxRequest) {
  215.  
  216.                     // to avoid jQuery and other core scripts from loading when the fourth parameter of renderPartial() is TRUE.
  217.                     // this is useful if you want another ajaxButton in the modal or anything with scripts.
  218.                     // http://www.yiiframework.com/forum/index.php/topic/5455-creating-ajax-elements-from-inside-ajax-request/page__p__30114#entry30114
  219.                     Yii::app()->clientscript->scriptMap['jquery.js'] = false;
  220.                    
  221.                      $this->performAjaxValidation($model);
  222.                    
  223.                     if(isset($_POST['User']))
  224.                     {
  225.                             $model->attributes=$_POST['User'];
  226.                            
  227.                            
  228.                         if($model->validate()) {
  229.                             if($model->save()) {
  230.                                 $header = 'Registration was successful';
  231.                             }
  232.                         }
  233.                     }
  234.                    
  235.                     $body = $this->renderPartial('register', 'model'=>$model), true, true); // processOutput
  236.                    
  237.                     echo CJSON::encode(array(
  238.                         // this will be used in the Modal header
  239.                         'header' => $header,
  240.  
  241.                         // this will be used in the Modal body
  242.                         'body' => $body,
  243.                     ));
  244.                     exit;
  245.                 }
  246.                 else
  247.                     throw new CHttpException('403', 'Forbidden access.');
  248.     }
Advertisement
Add Comment
Please, Sign In to add comment