以Yii Bootstrap形式显示所有表单错误

时间:2014-07-17 09:36:13

标签: php twitter-bootstrap yii frameworks yii-extensions

我有一个使用Yii Bootstrap库的Yii表单,如果在任何字段中突出显示该字段时出现错误,并且在相应的输入文本字段正下方以红色文本显示单个错误,则表单提交。 / p>

我希望在表单上方的页面顶部显示所有错误,我的代码如下所示:

我希望在表单顶部依次列出错误,有关实现此目标的最佳方法的任何想法吗?

<?php if (!$model->isNewrecord) $this->renderPartial('partials/_totals', compact('model'));?>

<?php $form = $this->beginWidget('application.widgets.bootstrap.TbActiveFormExt', array(
'id'                    => get_class($model),
'type'                  => 'horizontal',
'enableAjaxValidation'  => true,
'clientOptions'             => array(
    'validateOnSubmit'      => true,
    'validateOnChange'      => false,
    'validateOnType'        =>false,
),
'htmlOptions'           => array(
    'class'                 => '',
    'autocomplete'          => 'off'
),
)); ?>
<?php echo $form->hiddenField($model, 'activetab' ); ?>

<?php $this->beginWidget('CClipWidget', array('id'=>'t1')); ?>
    <div class="row-fluid">
        <div class="inner">
            <fieldset class="span12">
                <legend>User Details</legend>
                <div class="row-fluid">
                    <div class="span6">
                        <?php echo $form->textFieldRow($model, 'username', array('class'=>'span12')); ?>
                        <?php if ($model->isNewRecord):?>
                            <?php echo $form->passwordFieldRow($model, 'password', array('value'=>'','class'=>'span12')); ?>
                            <?php echo $form->passwordFieldRow($model, 'password_repeat', array('value'=>'', 'class'=>'span12', 'hint' => '<i class="icon-info-sign"></i> If you leave the password fields empty we will default it to \'mystickers\'.')); ?>
                        <?php else:?>
                            <?php echo $form->passwordFieldRow($model, 'new_password', array('value'=>'', 'class'=>'span12')); ?>
                            <?php echo $form->passwordFieldRow($model, 'new_password_repeat', array('value'=>'', 'class'=>'span12', 'hint' => '<i class="icon-info-sign"></i> Enter a new password only if you wish to change the current one.')); ?>
                        <?php endif; ?>
                        <?php echo $form->textFieldRow($model, 'firstname', array('class'=>'span8')); ?>
                        <?php echo $form->textFieldRow($model, 'surname', array('class'=>'span8')); ?>

                        </div>
                        <div class="span6">
                            <?php if ($model->isNewRecord):?>
                            <?php echo $form->dropDownListRow($model, 'gender', $model->genderOptions, array( 'empty' => '-- Select --', 'class'=>'span12')); ?>
                        <?php else: ?>
                            <?php echo $form->dropDownListRow($model, 'gender', $model->genderOptions, array('class'=>'span12')); ?>
                        <?php endif; ?>
                        <?php echo $form->textFieldRow($model, 'year_id', array('class'=>'span2')); ?>
                        <?php echo $form->textFieldRow($model, 'form_name', array('class'=>'span12')); ?>
                        <?php echo $form->textFieldRow($model, 'House', array('class'=>'span12')); ?>
                        <?php if ($model->isNewRecord): ?>
                            <?php if (user()->hasAuth(Group::GROUP_ADMIN, 'equal') && !user()->data->organisation->hasViewOrg): ?>
                                <?php echo $form->dropDownListRow($model, 'organisation_id', CHtml::listData($model->organisations, 'organisation_id', 'organisation_name'), array('prompt' => '-- Select --', 'class'=>'span9', 'value' => user()->getState('view_org'))); ?>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
<?php $this->endWidget(); ?>

<?php $this->beginWidget('CClipWidget', array('id'=>'t2')); ?>
    <div class="row-fluid">
        <div class="inner">
            <fieldset class="span12">
                <legend>Unique Label Information</legend>
                <div class="row-fluid">
                    <div class="span6">
                        <?php echo $form->textFieldRow($model, 'Unique1', array('class'=>'span12')); ?>
                        <?php echo $form->textFieldRow($model, 'Unique2', array('class'=>'span12') ); ?>
                        <?php echo $form->textFieldRow($model, 'Unique3', array('class'=>'span12') ); ?>
                        <?php echo $form->textFieldRow($model, 'Unique4', array('class'=>'span12') ); ?>
                        <?php echo $form->textFieldRow($model, 'Unique5', array('class'=>'span12')); ?>
                        <?php echo $form->textFieldRow($model, 'Unique6', array('class'=>'span12') ); ?>
                        <?php echo $form->textFieldRow($model, 'Unique7', array('hint' => '<i class="icon-info-sign"></i> You can change these unique label titles on your settings page.', 'class'=>'span12') ); ?>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
<?php $this->endWidget(); ?>

<?php $this->widget('bootstrap.widgets.TbTabs', array(
        'type'          => 'tabs',
        'placement'     => 'left',
        'htmlOptions'   => array(
            'class'         => 'clearfix',
        ),
        'events'    => array(
            'click' => 'js:function(e){$("#StudentDetailsForm_activetab").val(e.target.hash.substring(1))}'
        ),
        'tabs'          => array(
            array(
                'label'     => 'Main',
                'content'   => $this->clips['t1'],
                'active'    => !isset($activeTab) || $activeTab == 'yw0_tab_1',
            ),
            array(
                'label'     => 'Unique Label Information',
                'content'   => $this->clips['t2'],
                'active'    => isset($activeTab) && $activeTab == 'yw0_tab_2',
            )
        )
    )); ?>

    <div class="form-actions">
        <?php $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType'    => 'submit',
            'type'          => 'success',
            'icon'          => 'ok white',
            'label'         => 'Save Settings',
            'htmlOptions'   => array(
                'class'         => 'pull-right'
            )
        )); ?>

        <?php $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType'    => 'reset',
            'icon'          => 'remove',
            'label'         => 'Reset',
            'htmlOptions'   => array(
                'class'         => 'pull-right',
                'style'         => 'margin:0 10px;'
            )
        )); ?>
        <?php $this->widget('bootstrap.widgets.TbButton', array(
            'icon'          => 'arrow-left',
            'label'         => 'Cancel',
            'url'           => $overview ? '/site/overview/'.$overview : '/site/myuser/index',
            'htmlOptions'   => array(
                'style'         => 'margin:0 10px;'
            )
        )); ?>
    </div>
<?php $this->endWidget(); ?>

1 个答案:

答案 0 :(得分:1)

你可以尝试

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

在你的引导程序窗体小部件之后。