Wednesday, February 15, 2012

Simple tricks for creating repeating functions in form

This just a simple trick for writing your code in yiiframework for not repeating the same work.

If we have a functions that has a repeating use in our view for example you have a project that has many of date picker in a project management applications, so you have to write this in your form repeatly.


$this->widget('zii.widgets.jui.CJuiDatePicker',
 array(
  // you must specify name or model/attribute
  //'model'=>$model,
  //'attribute'=>'projectDateStart',
  'name'=>'Project[projectDateStart]',

  // optional: what's the initial value/date?
  //'value' => $model->projectDateStart
  'value' => '08/20/2010',

  // optional: change the language
  //'language' => 'de',
  //'language' => 'fr',
  //'language' => 'es',
  'language' => 'pt-BR',

  /* optional: change visual
   * themeUrl: "where the themes for this widget are located?"
   * theme: theme name. Note that there must be a folder under themeUrl with the theme name
   * cssFile: specifies the css file name under the theme folder. You may specify a
   *          single filename or an array of filenames
   * try http://jqueryui.com/themeroller/
  */
  'themeUrl' => Yii::app()->baseUrl.'/css/jui' ,
  'theme'=>'pool', //try 'bee' also to see the changes
  'cssFile'=>array('jquery-ui.css' /*,anotherfile.css, etc.css*/),


  //  optional: jquery Datepicker options
  'options' => array(
   // how to change the input format? see http://docs.jquery.com/UI/Datepicker/formatDate
   'dateFormat'=>'mm/dd/yy',

   // user will be able to change month and year
   'changeMonth' => 'true',
   'changeYear' => 'true',

   // shows the button panel under the calendar (buttons like "today" and "done")
   'showButtonPanel' => 'true',

   // this is useful to allow only valid chars in the input field, according to dateFormat
   'constrainInput' => 'false',

   // speed at which the datepicker appears, time in ms or "slow", "normal" or "fast"
   'duration'=>'fast',

   // animation effect, see http://docs.jquery.com/UI/Effects
   'showAnim' =>'slide',
  ),


  // optional: html options will affect the input element, not the datepicker widget itself
  'htmlOptions'=>array(
  'style'=>'height:30px;
   background:#ffbf00;
   color:#00a;
   font-weight:bold;
   font-size:0.9em;
   border: 1px solid #A80;
   padding-left: 4px;'
  )
 )
);
If we want to create this instance of date picker in our form repeatly, it can be so inconvenience to write the same work. The alternative way you can create your own function in your Controller based on Components/controller.php. For example place the code for creating date picker inside of it's class. For example below code.


public function createDatePicker($model,$attribute){
  $m=get_class($model);
  $t=$m.'['.$attribute.']';//remember for the structure object in yii
  $this->widget('zii.widgets.jui.CJuiDatePicker',array(
     'name'=>$t,
     'model'=>$model,
     'value'=>$model->$attribute,

     'options'=>array(
             'showAnim'=>'fold',
       'dateFormat' => 'yy-mm-dd', // save to db format
       'themeUrl' => Yii::app()->baseUrl.'/css/jui' ,

             'theme'=>'pool', //try 'bee' also to see the changes

             'cssFile'=>array('jquery-ui.css' /*,anotherfile.css, etc.css*/),

     ),
        'htmlOptions'=>array(
            'style'=>'height:20px;'
     ),
    )
  );
After that use can use this code in all of your view in your form like this.
<div class="row">
  <?php echo $form->labelEx($model,'end_time_planned'); ?>
  <?php echo $this->createDatePicker($model,'end_time_planned'); ?>
  <?php echo $form->error($model,'end_time_planned'); ?>
 </div>

That's simple and can reduce your hard work in getting the same purpose

YIi is Rock ..


No comments:

Post a Comment