Yii: model attribute comments / tooltips

I need to have comments / tips for some fields in my Form. My idea is to describe it in a model, just like attributeLabels. How can i do this?

And then that would be ideal if the Gii Model (and Crud) generator would take it directly from the mysql column comment

+5
source share
2 answers

So, I see two questions here:

  • Describe the hints in the model and display them in the form.
  • Get hints from mysql comment

Display tooltips from a model

Here's a slightly modified version login.phpof the default file createdYiic

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username'); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>
        <p class="hint">
            Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
        </p>
    </div>

    <div class="row rememberMe">
        <?php echo $form->checkBox($model,'rememberMe'); ?>
        <?php echo $form->label($model,'rememberMe'); ?>
        <?php echo $form->error($model,'rememberMe'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton('Login'); ?>
    </div>

<?php $this->endWidget(); ?>
</div><!-- form -->

Move the password hint to the model by adding a method attributeHints()and method getHint()to the model LoginForm.php.

    /**
     * Declares attribute hints.
     */
    public function attributeHints()
    {
        return array(
                'password'=>'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.',
        );
    }

    /**
     * Return a hint
     */
    public function getHint( $attribute )
    {
        $hints = $this->attributeHints();

        return $hints[$attribute];
    }

, .

, login.php, .

<div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password'); ?>
    <?php echo $form->error($model,'password'); ?>
    <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
</div>

, - , .

.

mySQL

, Gii, , mySQL. mySQL .

mySQL

SHOW FULL COLUMNS FROM `tbl_user`

,

ALTER TABLE  `tbl_user` 
CHANGE  `password`  `password` VARCHAR( 256 ) 
CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL 
COMMENT  'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.';

, attributeHints().

    /**
     * Declares attribute hints.
     */
    public function attributeHints()
    {
        $columns= Yii::app()->db->createCommand('SHOW FULL COLUMNS FROM `tbl_user`')->queryAll();

        $comments=array();
        foreach($columns as $column){
            if( isset( $column['Comment'] ) )
            {
                $comments[ $column['Field'] ] = $column['Comment'];
            }

        }

        //Add any hardcoded hints here
        $hints = array(
                'username'=>'Enter username above',
        );

        //Return merged array
        return array_merge( $comments, $hints );
    }

, mySQL .

login.php , .

<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username'); ?>
    <?php echo $form->error($model,'username'); ?>
    <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('username')); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password'); ?>
    <?php echo $form->error($model,'password'); ?>
    <?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
</div>

!

Login page

: mySQL.

+10

Yii-1.1.13, comment CMysqlColumnSchema, CDbColumnSchema:

. - , , . Null , RDBMS (SQLite) RDBMS .

. :

$modelObject->tableSchema->columns['column_name']->comment

// or if you are doing this within your model use
$this->tableSchema->columns['column_name']->comment

, tableSchema , CActiveRecord , , .


:

class MyModel extends CActiveRecord {
    // hints array will store the hints
    public $hints=array();

    public function init() {
        parent::init();
        $this->hints=self::initHints($this);
    }

    public static function initHints($model) {
        $comments=array();
        foreach ($model->tableSchema->columns as $aColumn){
            if(!empty($aColumn->comment))
                $comments["$aColumn->name"]=$aColumn->comment; 
        }
        return $comments;
    }

    public static function model($className=__CLASS__) {
        $model=parent::model($className);
        $model->hints=self::initHints($model);
        return $model;
    }

}

:

$model = new MyModel;
$single_column_hint=$model->hints['column_name'];

$model = MyModel::model();
$single_column_hint=$model->hints['column_name'];

gii , , , .

, .

+2

All Articles