Default The Value Of A Form Field

I’d like to default the value of a form field based on what the user selects from a prior field.

I have a Winery table with an attribute of "default_appellation". I have a form where the user enters details for a wine. I would like to set the wine.appellation = winery.default_appellation on the form but still allow the user to override the default with a value they select from the appellation dropdown.

I presume this will need client-side code so will require JQuery or some such.

I am using the gii created _form.php…

<?php $form=$this->beginWidget('CActiveForm', array(



        // Please note: When you enable ajax validation, make sure the corresponding

        // controller action is handling ajax validation correctly.

        // There is a call to performAjaxValidation() commented in generated controller code.

        // See class documentation of CActiveForm for details on this.


)); ?>

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

        <?php echo $form->errorSummary($model); ?>

        <div class="row">

                <?php echo $form->labelEx($model,'wine_name'); ?>

                <?php echo $form->textField($model,'wine_name',array('size'=>45,'maxlength'=>45)); ?>

                <?php echo $form->error($model,'wine_name'); ?>


        <div class="row">

                <?php echo $form->labelEx($model,'description'); ?>

                <?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>

                <?php echo $form->error($model,'description'); ?>


        <div class="row">

                <?php echo $form->labelEx($model,'winery_id'); ?>

                <?php $wineries_list = CHtml::listData(Wineries::model()->findAll(), 'id', 'winery_name'); ?>

                <?php echo $form->dropDownList($model, 'winery_id', $wineries_list, array('empty'=>'(Select a Winery)')); ?>

                <?php echo $form->error($model,'winery_id'); ?>


        <div class="row">

                <?php echo $form->labelEx($model,'appellation_id'); ?>

                <?php $appellation_list = CHtml::listData(Appellations::model()->findAll(), 'id', 'appellation'); ?>

                <?php echo $form->dropDownList($model, 'appellation_id', $appellation_list, array('empty'=>'(Select an Appellation)')); ?>

                <?php echo $form->error($model,'appellation_id'); ?>


Your code:

 <?php echo $form->dropDownList($model, 'appellation_id', $appellation_list, array('empty'=>'(Select an Appellation)')); ?>

Why not replace "(Select an Appellation)" to winery.default_appellation

@Johnny, that’s just way to obvious… Great idea. Thanks.