验证部分

时间:2014-04-09 09:20:37

标签: ajax yii

我的表单内部是部分地图,负责在地图上生成一些数据。如果未填写主表单字段(磁贴,描述等),则生成不起作用。我希望验证不仅在按下主窗体上的SAVE按钮时发生,而且当您单击部分贴图的主要形式中包含的GENERATE按钮时......如何解决问题?

主要表格

<?php
$form = $this->beginWidget(
    'VMActiveForm',
    array(
        'type' => 'horizontal',
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'validateOnChange'=>true),
        'id' => 'coupon-form',
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
        'action' => $this->createUrl('campaigns/saveCoupon'),
    )
);
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
?>

<?= $form->errorSummary($model); ?>

<?=
$form->textFieldRow(
    $model,
    'title',
    array(
        'class' => 'span3',
        'hint' => 'Fill in a short selling title - max 40 chars, <span id="CouponExt_title_chars_left">40</span> chars left.',
        'placeholder' => 'Please enter title of coupon...'
    )
);
?>
<script type="text/javascript">
    $(function() {
        $('#CouponExt_title').VMSymbolCounter({'maxCharsCount': 40, 'counter': $('#CouponExt_title_chars_left')});
    });
</script>

<?=
$form->textFieldRow(
    $model,
    'value',
    array(
        'class' => 'span3',
        'placeholder' => 'Please enter value of coupon...'
    )
);
?>

<?=
$form->textAreaRow(
    $model,
    'description',
    array(
        'rows' => 10,
        'class' => 'span3',
        'hint' => 'Short description of your offer. Fill in a short selling title - max 250 chars, <span id="CouponExt_description_chars_left">255</span> chars left.'
    )
);
?>

<script type="text/javascript">
    $(function() {
        $('#CouponExt_description').VMSymbolCounter({'maxCharsCount': 255, 'counter': $('#CouponExt_description_chars_left')});
    });
</script>

<? $categories = CHtml::listData($categories, 'id', 'title'); ?>
<?= $form->dropDownListRow($model, 'category_id', $categories); ?>

<? $campaigns = CHtml::listData($campaigns, 'id', 'name'); ?>
<?= $form->dropDownListRow($model, 'campaign_id', $campaigns); ?>

<?=
$form->textFieldRow(
    $model,
    'amount',
    array(
        'class' => 'span3',
        'placeholder' => 'Please enter number of coupons...',
        'disabled' => $model->amount > 0
    )
);
?>

<?=
$form->textFieldRow(
    $model,
    'end_value',
    array(
        'class' => 'span3',
        'placeholder' => 'Please enter number of coupons for end...',
    )
);
?>


<?= $form->fileFieldRow($model, 'avatar'); ?>
<div class="controls">
    <?=CHtml::image($model->avatar_filename, '');?>
</div>
<?=
$form->textFieldRow(
    $model,
    'url',
    array(
        'class' => 'span3',
        'placeholder' => 'http://www.yourshop.com',
        'hint' => 'Set an URL'
    )
);
?>
<?php
    $this->renderPartial("partials.map", array(
        'model' => $model,
        'count' => $count,
    ));
    $this->renderPartial("partials.progress", array(
        'model' => $model,
        'count' => $count,
    ));
?>

映射部分

<?php
/**
 * @var CouponExt $model
 */
if(($model->amount > $count) || (!$model->amount)) {
        if($model->isNewRecord) {
            echo CHtml::button('Set radius', array(
                'id' => 'set-radius',
                'class' => 'btn',
            ));
        }

        echo CHtml::ajaxSubmitButton(
            'Generate',
            CHtml::normalizeUrl(array('campaigns/saveCoupon')),
            array(
                'success' => new CJavaScriptExpression("function(data){
                            if(parseInt(data)) {
                                $('#CouponExt_id').val(data);
                                $('#map-block-group').hide();
                                $('#progress-bar-group').show();
                                changeProgress();
                            }}")
            ),
            array(
                'class' => 'btn btn-primary',
                'id' => 'generate-mines'
            )
        );
    }
?>
</div>
</div>
<div id="map-area">
<input id="search-area" type="text" placeholder="Search address..." />
<div id="google-map-area" class="google-map" style="width: 700px; height: 350px; border: 1px solid #cccccc"></div>
<div class="map-attributes">
<?php
    if(($model->amount > $count) || (!$model->amount)) {
        echo CHtml::ajaxSubmitButton(
            'Generate',
            CHtml::normalizeUrl(array('campaigns/saveCoupon')),
            array(
                'success' => new CJavaScriptExpression("function(data){
                            if(parseInt(data)) {
                                $('#CouponExt_id').val(data);
                                $('#map-block-group').hide();
                                $('#progress-bar-group').show();
                                changeProgress();
                            }}")
            ),
            array(
                'class' => 'btn btn-primary',
                'id' => 'scatterArea'
            )
        );

        if($model->isNewRecord) {
            echo CHtml::button('Clear', array(
                'id' => 'clearDotes',
                'class' => 'btn'
            ));

            echo CHtml::button('Remove last', array(
                'id' => 'removeDotes',
                'class' => 'btn'
            ));
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

设置生成按钮的onclick事件以提交优惠券表格

echo CHtml::ajaxSubmitButton(
   'Generate',
    ...
    array(
       'class' => 'btn btn-primary',
       'id' => 'scatterArea'
       'onclick'=>'$("#coupon-form").submit();'
    )
);