【问题标题】:Yii - CActiveForm client-side not workingYii - CActiveForm 客户端不工作
【发布时间】:2015-12-20 16:21:39
【问题描述】:

我需要在客户端使用 CActiveForm 验证表单。

这是我初始化小部件和错误的代码:

<?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'idX',
        'enableAjaxValidation' => false,
        'enableClientValidation'=> true,
        'clientOptions'=>array('onSubmit'=>true),
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data'
        ),
    ));
    ?>

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

在此之后,我有输入,最后我有这个:

<?php $this->endWidget(); ?>

当我提交表单并按下F12(mozilla 中的错误检查器)时,未加载“jquery.yiiactiveform.js”。当我使用这个小部件时,应该包含这个 js 文件。

-----更新----

public function rules() {
    return array(
        //Always required
        array('p_first_name, p_last_name, p_title, p_phone, p_phone2, p_fax, p_email, user, password, clientType', 'required'),
        //just company
        array('c_name, c_postal_code, c_location, c_country, c_activity, c_nif, c_website', 'required', 'on' => 'company'),
        array('c_country, c_activity', 'numerical', 'integerOnly' => true),
        array('c_name, c_location, c_website, p_first_name, p_last_name, p_email', 'length', 'max' => 255),
        array('c_postal_code', 'length', 'max' => 8),
        array('c_nif', 'length', 'max' => 9),
        array('p_title', 'length', 'max' => 25),
        array('p_phone, p_phone2, p_fax', 'length', 'max' => 15),
        array('user, password', 'length', 'max' => 100),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id_client, c_name, c_postal_code, c_location, c_country, c_activity, c_nif, c_website, p_first_name, p_last_name, p_title, p_phone, p_phone2, p_fax, p_email, user, password', 'safe', 'on' => 'search'),
    );
}

还有一个输入示例:

<div class="form-group">
            <?= $form->labelEx($model, 'p_phone', array('class' => 'col-sm-2 control-label')); ?>
            <div class="col-sm-10">
                <?= $form->textField($model, 'p_phone', array('class' => 'form-control', 'placeholder' => Yii::t('clients/register', 'Insira o contacto telefónico do empresário.'))); ?>
            </div>
        </div>

----- 更新 2 --------

现在出现这个错误:

TypeError: jQuery(...).yiiactiveform 不是函数

【问题讨论】:

  • 请提供您的输入和规则

标签: php forms validation yii submit


【解决方案1】:

我认为您错过了 error 输入字段:

<div class="form-group">
    <?= $form->labelEx($model, 'p_phone', array('class' => 'col-sm-2 control-label')); ?>
    <div class="col-sm-10">
        <?= $form->textField($model, 'p_phone', array('class' => 'form-control', 'placeholder' => Yii::t('clients/register', 'Insira o contacto telefónico do empresário.'))); ?>
    </div>
    <?= $form->error($model, 'p_phone'); ?>
</div>

【讨论】:

  • @Criesto 抱歉,我无法理解您的意思?
  • @AndréOliveira 也许您“手动”包含 jQuery,而 CActiveForm“现在不能”并加载第二个 jQuery。那么如何包含 jQuery 脚本呢?最好包含这样的Yii::app()-&gt;clientScript-&gt;registerCoreScript('jquery')
  • 我通过从 main.php 中删除我所有的 jQuery 来修复,因为 yii 框架自己在资产中添加了 jQuery。
  • @AndréOliveira Yii 仅在必要时添加 jQuery 本身:f。 e.在页面中使用任何需要 jQuery(CActiveForm、CGridView、CListView、...)的小部件,在其他情况下,yii 不会添加 jQuery,但如果您使用组件clientScript 注册 jQuery(或任何脚本),则它不能再次包含
  • 这是真的!再次感谢!
【解决方案2】:

修好了!

我有两个版本的 jQuery,所以会导致冲突。因此,如果您在 main.php 上有一两个 jQuery 版本,请全部删除!

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多