Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- View you want to call modal at:
- /* Some styles for optimal form view */
- <style>
- #myModal.modal-body{
- max-height: 650px;
- overflow-y: auto;
- }
- #myModal{
- top: 50px;
- }
- </style>
- /*End of styles*/
- /* Start of Modal code */
- <?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal',
- 'htmlOptions'=>array(
- 'aria-hidden'=>true,
- 'role'=>'dialog',
- ))); ?>
- <div class="modal-header">
- <a class="close" data-dismiss="modal">×</a>
- <h4><?php Yii::t('svsite','Register') ?></h4>
- </div>
- <div class="modal-body">
- </div>
- <div class="modal-footer">
- <?php $this->widget('bootstrap.widgets.TbButton', array(
- 'type'=>'primary',
- 'label'=>Yii::t('svsite','Register'),
- 'url'=>'#',
- 'htmlOptions'=>array('onclick' => '$("#registration-form").submit()'),
- 'ajaxOptions'=>array(
- 'type'=>'POST',
- )
- )); ?>
- <?php $this->widget('bootstrap.widgets.TbButton', array(
- 'label'=>'Close',
- 'url'=>'#',
- 'htmlOptions'=>array('data-dismiss'=>'modal'),
- )); ?>
- </div>
- <?php $this->endWidget(); ?>
- /* End of Modal code */
- /* Button that invokes Modal */
- <?php $this->widget('bootstrap.widgets.TbButton',
- array('label'=>'Click me',
- 'ajaxOptions'=>array(
- 'type'=>'post',
- 'dataType'=>'json',
- 'url'=>array(controller/register),
- 'success'=>'function(data){
- $("#myModal .modal-header h4").text( data.header);
- $("#myModal .modal-body").html(data.body);
- }'),
- 'type'=>'primary',
- 'htmlOptions'=>array(
- 'data-toggle'=>'modal',
- 'data-target'=>'#myModal',
- 'id' => 'open-modal-'.uniqid(),
- ),
- )); ?>
- /* End of buton */
- Form view file:
- <?php
- $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
- 'id'=>'registration-form',
- 'action'=>array('site/register'),
- 'enableAjaxValidation'=>true,
- 'clientOptions'=>array(
- 'validateOnSubmit'=>true, //Validation options
- 'validateOnChange'=>true,
- 'validateOnType'=>true,
- ),
- )); ?>
- <p class="help-block">Fields with <span class="required">*</span> are required.</p>
- <?php echo $form->errorSummary($model); ?>
- <?php echo $form->textFieldRow($model,'name',array('class'=>'span5','maxlength'=>255, 'labelOptions'=>array('label'=>false), 'placeholder'=>'Enter your name')); ?>
- <?php echo $form->textFieldRow($model,'surname',array('class'=>'span5','maxlength'=>255, 'placeholder'=>'surname')); ?>
- <?php echo $form->textFieldRow($model,'email',array('class'=>'span5','maxlength'=>255)); ?>
- <?php echo $form->passwordFieldRow($model,'password',array('class'=>'span5','maxlength'=>255)); ?>
- <?php echo $form->passwordFieldRow($model,'password_repeat',array('class'=>'span5','maxlength'=>255)); ?>
- <?php echo $form->textFieldRow($model,'city',array('class'=>'span5','maxlength'=>255)); ?>
- <?php echo $form->captchaRow($model, 'captcha', array('showRefreshButton' => true) ); ?>
- <?php $this->endWidget(); ?>
- Model of the form:
- class User extends CActiveRecord
- {
- public $password_repeat;
- private $salt = $this->email;
- public $captcha;
- /**
- * @return string the associated database table name
- */
- public function tableName()
- {
- return 'base_user';
- }
- /**
- * @return array validation rules for model attributes.
- */
- public function rules()
- {
- // NOTE: you should only define rules for those attributes that
- // will receive user inputs.
- return array(
- array('email, password, password_repeat, captcha', 'required'),
- array('email', 'unique'),
- array('email', 'email'),
- array('name, surname, email, password', 'length', 'max'=>255),
- array('captcha', 'captcha'),
- array('password_repeat', 'safe'),
- array('password', 'compare', 'compareAttribute' => 'password_repeat'),
- array('id, name, surname, email, password, city', 'safe', 'on'=>'search'),
- );
- }
- /**
- * @return array relational rules.
- */
- public function relations()
- {
- // NOTE: you may need to adjust the relation name and the related
- // class name for the relations automatically generated below.
- return array(
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels()
- {
- return array(
- 'id' => 'ID',
- 'name' => 'Name',
- 'surname' => 'Surname',
- 'email' => 'E-mail',
- 'password' => 'Password',
- 'city' => 'City',
- );
- }
- /**
- * Retrieves a list of models based on the current search/filter conditions.
- *
- * Typical usecase:
- * - Initialize the model fields with values from filter form.
- * - Execute this method to get CActiveDataProvider instance which will filter
- * models according to data in model fields.
- * - Pass data provider to CGridView, CListView or any similar widget.
- *
- * @return CActiveDataProvider the data provider that can return the models
- * based on the search/filter conditions.
- */
- public function search()
- {
- // @todo Please modify the following code to remove attributes that should not be searched.
- $criteria=new CDbCriteria;
- $criteria->compare('id',$this->id);
- $criteria->compare('name',$this->name,true);
- $criteria->compare('surname',$this->surname,true);
- $criteria->compare('email',$this->email,true);
- $criteria->compare('password',$this->password,true);
- $criteria->compare('city',$this->city,true);
- return new CActiveDataProvider($this, array(
- 'criteria'=>$criteria,
- ));
- }
- /**
- * Returns the static model of the specified AR class.
- * Please note that you should have this exact method in all your CActiveRecord descendants!
- * @param string $className active record class name.
- * @return User the static model class
- */
- public static function model($className=__CLASS__)
- {
- return parent::model($className);
- }
- protected function beforeSave()
- {
- $this->password = sha1($this->salt . $this->password);
- $this->password_repeat = sha1($this->salt . $this->password_repeat);
- return parent::beforeSave();
- }
- }
- Function in the Controller:
- public function actionRegister()
- {
- $model = new User();
- $header = Yii::t('svsite','Register');
- if (Yii::app()->request->isAjaxRequest) {
- // to avoid jQuery and other core scripts from loading when the fourth parameter of renderPartial() is TRUE.
- // this is useful if you want another ajaxButton in the modal or anything with scripts.
- // http://www.yiiframework.com/forum/index.php/topic/5455-creating-ajax-elements-from-inside-ajax-request/page__p__30114#entry30114
- Yii::app()->clientscript->scriptMap['jquery.js'] = false;
- $this->performAjaxValidation($model);
- if(isset($_POST['User']))
- {
- $model->attributes=$_POST['User'];
- if($model->validate()) {
- if($model->save()) {
- $header = 'Registration was successful';
- }
- }
- }
- $body = $this->renderPartial('register', 'model'=>$model), true, true); // processOutput
- echo CJSON::encode(array(
- // this will be used in the Modal header
- 'header' => $header,
- // this will be used in the Modal body
- 'body' => $body,
- ));
- exit;
- }
- else
- throw new CHttpException('403', 'Forbidden access.');
- }
Advertisement
Add Comment
Please, Sign In to add comment