Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:51:18+00:00 2026-06-11T02:51:18+00:00

I need to have comments/hints for some fields in my Form. My idea is

  • 0

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:51:20+00:00Added an answer on June 11, 2026 at 2:51 am

    So I see two questions here:

    1. Describe hints in the model and display on the form.
    2. Get hints from mysql comment

    Displaying Hints from Model

    Here’s a slightly modified version of the default login.php file generated by Yiic

    <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 -->
    

    Let’s move that password hint into the model by adding a attributeHints() method and a getHint() method to the LoginForm.php model.

        /**
         * 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];
        }
    

    As you can see, we;ve moved the hint text from the view into the model and added a way to access it.

    Now, back in login.php, let’s add the hint tag based on data from the model.

    <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>
    

    So now we’ve changed the hardcoded hint into a generated element populated with data from the model.

    Now, moving on to the second question.

    Getting hints from mySQL comments

    Unforunately, I am not fammiliar enough with Gii to know how to automatically generate the hints from mySQL comments. However, getting the mySQL comment data into the model is fairly easy.

    To do this we can use the following mySQL query

    SHOW FULL COLUMNS FROM `tbl_user`
    

    So let’s add the comment to the password field

    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>.';
    

    And let’s add the code to fetch it into our attributeHints() method.

        /**
         * 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 );
        }
    

    We now have two ways to set comments, through mySQL comments or through hard coded statements.

    Let’s update our login.php file quick to include hints of both types.

    <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>
    

    And that’s it!

    Login page

    Now our login page will look like this, with a username hint from the model and a password hint from the mySQL comment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I get the variable value I need, I have selected comments and
Here's what I need to fetch: - posts that have comments - number of
I have a need to identify comments in different kinds of source files in
I have the need to create Sharepoint blog comments by code: SPSecurity.RunWithElevatedPrivileges(delegate() { sw.AllowUnsafeUpdates
I have a bunch of MP4-Files and need to add comments to them. They
I have some complex regular expressions which I need to comment for readability and
I need to relate a Comments model with two ids at the same time
I have a number of types of data fields on an input form, for
I have two tables called 'events' and 'topics' each table can have many comments.
I have an excel file with 85,000 rows and I need to extract just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.