GVKun编程网logo

yii2之ActiveForm表单使用(easyui表单设计器)

25

在本文中,我们将给您介绍关于yii2之ActiveForm表单使用的详细内容,并且为您解答easyui表单设计器的相关问题,此外,我们还将为您提供关于html–Yii–CActiveForm–附加表单

在本文中,我们将给您介绍关于yii2之ActiveForm表单使用的详细内容,并且为您解答easyui表单设计器的相关问题,此外,我们还将为您提供关于html – Yii – CActiveForm – 附加表单属性、oracle 创建表空间 Yii2创建表单ActiveForm方法详解、php – Yii2 ActiveForm字段选项不起作用、php – Yii2:ActiveForm字段数字,长度=> 8的知识。

本文目录一览:

yii2之ActiveForm表单使用(easyui表单设计器)

yii2之ActiveForm表单使用(easyui表单设计器)

因目前项目并非前后端分离模式,且用到PHP的yii2框架(所有html代码,js较多内嵌在.php文件内多少采用同步提交【喷墨中...】),遂对于前端面上需要用到的yii2小组件一些整理(因是前端若涉及到php写法错误或者风格问题,敬请指点)

 使用场景尽量为表单

  基础注册调用小组件

  

<?php
  use yii\helpers\Html;
  use yii\widgets\ActiveForm;
?>
  //首先注册activeForm小部件,并赋值给$form(php中的声明变量方法用$ 等价于js中的var let)
  //begin 标志小部件开始   <?php
$form = ActiveForm::begin([   ''id'' => ''login-form'', //声明小部件的id 即form的id
     //声明需要添加的属性 ,例如class , data-x等   ''options'' => [''class'' => ''form-horizontal''],   ]) ?>

    //注册完小部件后可以在 activeForm小部件声明块中调用小部件的方法    <?= $form->field($model, ''password'')->passwordInput() ?>
//::end标识小部件结束 <?php ActiveForm::end() ?>

 

 

 

1.首先就列出activeForm的一些基本方法:

  自定义input框:input();

  文本框:textInput(); 

  密码框:passwordInput();

  单选框:radio(),radioList(); 

  复选框:checkbox(),checkboxList();

  下拉框:dropDownList(); 

  多选列表:listBox(); 

  隐藏域:hiddenInput(); 

  文本域:textarea([''rows''=>3]); 

  文件上传:fileInput(); 

  widget扩展 <?= $form->field($model, ''username'')->widget(\yii\widgets\MaskedInput::className(), [''mask'' => ''9999/99/99'',]); ?>

 

 2.下面我就逐一描述下各个方法的基本调用以及如何自定义所需(上述各方法中input之前均是描述的是input标签的类型)

  2.1 input 文本框/密码框以及各指定类型框

<?php $form=ActiveForm::begin([''id''=>''login'',''class''=>''login''])?>
    <!-- 简易用法 使用activeForm 的 fiedld方法  -->
    <!-- 其中 该方法下有 
        textInout/passwordInput 等一些常用input类型方法
        hint 输入前的提示内容
        error 错误内容 //一般由后台生成
        label 可以更改label内的内容
        在hint,error,label设置class后将会重置了 这些方法内原来属于容器上的class若需要可以原样赋回去
    -->
    <!-- 这里的
        $mode为跟字段有关的数据模型 ,
        第二个参数为关系模型中的字段不存在将报错,
        第三个参数为模板内的一些内容的进行自定义
    -->
    <?= $form->field($model, ''username'',[
        ''options''=>[],//数组里面可以设置自需属性
        // template 为字符串模板可自定义模板 ,
        // 其中 {label} {input} {hint} {error} 存在是会调用对应封装好的html模板 当然你也可以不写这样就不会生成yii2内置小部件模板
        ''template'' => ''{label} {input} {hint} {error}'',
        // 以下三个分别可以设置label ,input ,hint,error的属性(都是选填项)
        // 其中如果后面有使用->input...,label(...)等将会将这些里面的配置合并值对应的xxxOptions 内
        ''labelOptions'' => [
            ''class''=>''需要在label上添加的类名''
            //....其他属性集
        ],
        ''inputOptions'' => [],
        ''hintOptions'' => [],
        ''errorOptions'' => [],
    ])->textInput([
        // 在options数组内可以设置任意属性
        ''class''=>''testClass'',
        ''value''=>''测试''
    ])->hint(
        // 设置提示内容,当只有一个参数切为false(boolean)用于显示提示的标签
        ''Please enter your name'',
        [
            // 设置任意属性
            ''class'' => ''testHint''
    ])->label(
        // 设置label显示内容,当只有一个参数切为false(boolean)label标签将不会被渲染
        ''Name'',
        [
            // 设置任意属性
            ''class'' =>''testLabel''
    ])->error([
        // 任意属性,当只有一个参数切为false(boolean)用于显示错误的标签
        ''class''=>''errors''
    ]) ?>

    <!-- 可自定义类型input 这里只描述了input的参数  其余参数参考上个示例 -->
    <?= $form->field($model, ''username'')->input(
        // input内只允许放置两个参数即[type ,options]
        ''email'',//该处为指定type="xxxx"的input类型
        [''class''=>''tests'',''value''=>''值'']//可在内部定义任何属性
    ) ?>
                       
<?php ActiveForm::end();?>

 

   2.2 radio 单选框系列

<?php $form=ActiveForm::begin([''id''=>''login'',''class''=>''login''])?>

    <!-- 
        老实说对这个radio方法相当迷惑  一个单选按钮选择而且一旦选择无法取消,无法一次柑橘属性放置多个值 在有radioList方法的前提下觉得相当鸡肋
        第二个参数中false为是否开启label标签若没开启 labelOption 将无效  ,label设置的值直接显示在容器内
     -->
    <?= $form->field($model, ''username'')->radio([
        // 隐藏域中的值
        ''uncheck'' =>''test1'',
        // 定义lebal的内容
        ''label'' =>''test'',
        // label上的任意属性
        ''labelOptions''=>[
            ''gs''=>''test''
        ]
    ],false)?>

    <!-- 
        单选框组 

        若要设置默认值,则在对应控制器中将指定字段设置为 需要选择的值
        $model->username = 1;
    -->

    <?= $form->field($model, ''username'')->radioList([
        ''0''=>''a'',
        ''1''=>''b'',
        ''2''=>''c''
    ],[
        // tag声改变的父级标签 若tag设置为h3 
        //    则 <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true"> 
        //    => 转为 <h3 id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        // <div>
        //     <label>Username</label>
        //     <input type="hidden" name="LoginForm[username]" value="">
        //     <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        //         <div><label><input type="radio" name="LoginForm[username]" value="0"> a</label></div>
        //         <div><label><input type="radio" name="LoginForm[username]" value="1"> b</label></div>
        //         <div><label><input type="radio" name="LoginForm[username]" value="2"> c</label></div>
        //     </div>

        //     <p></p>
        // </div>
        ''tag''=>''h3'',
        // 未选择是默认提交的值
        ''unselect''=>''1'',
        // 如果设置了item选项,则忽略此选项
        ''encode''=>false,
        // 每个单选块之间的内容 写的是什么字符串输出就什么字符串
        ''separator''=>'''',
        // 定义在每个input单选按钮上的属性
        ''itemOptions''=>[
            ''tess''=>''jzq''
        ],
      //可调用的回调,可用于自定义与$Item中单个项对应的HTML代码的生成。此回调的签名必须是:函数($index、$Label、$name、$check、$value),
      //其中$index是整个列表中单选按钮的基于零的索引;$Label是单选按钮的标签;$name、$value和$check表示单选按钮输入的名称、值和选中状态。 ''item''=>function($index, $label, $name, $checked, $value){ // 这块跟encode是在下才疏学浅暂时还未明白啥子用处,待弄明白后在补上,若有码友知晓这块具体作用用法,希望不吝赐教,感激 // echo $index, $label, $name, $checked, $value; }, // 除此yii2有规定属性之外还可自定义任意属性 且上述属性均不是必填 ])?>

  2.3 checkbox多选框系列

  

<?php $form=ActiveForm::begin([''id''=>''login'',''class''=>''login''])?>

    <!-- 
        checbox方法
        该方法与radio 方法近似就不多说了 直接撸代码 具体可参考 radio
     -->
    <?= $form->field($model, ''username'')->checkbox([
        // 隐藏域中的值
        ''uncheck'' =>''test1'',
        // 定义lebal的内容
        ''label'' =>''test'',
        // label上的任意属性
        ''labelOptions''=>[
            ''gs''=>''test''
        ]
    ],true)?>

    <!-- 
        checkboxList方法
        多选框
     -->
     <?= $form->field($model, ''username'')->checkboxList([
        ''1''=>''篮球'',
        ''2''=>''足球'',
        ''3''=>''游戏'',
        ''4''=>''读书''
    ],[
         // tag声改变的父级标签 若tag设置为h3 
        //    则 <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true"> 
        //    => 转为 <h3 id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        // <div>
        //     <label>Username</label>
        //     <input type="hidden" name="LoginForm[username]" value="">
        //     <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        //         <div><label><input type="radio" name="LoginForm[username]" value="0"> a</label></div>
        //         <div><label><input type="radio" name="LoginForm[username]" value="1"> b</label></div>
        //         <div><label><input type="radio" name="LoginForm[username]" value="2"> c</label></div>
        //     </div>

        //     <p></p>
        // </div>
        ''tag''=>''h3'',
        // 未选择是默认提交的值
        ''unselect''=>''1'',
        // 如果设置了item选项,则忽略此选项
        ''encode''=>false,
        // 每个单选块之间的内容 写的是什么字符串输出就什么字符串,建议如无特殊情况 请忽视该字段
        // ''separator''=>'','',
        // 定义在每个input单选按钮上的属性
        ''itemOptions''=>[
            ''tess''=>''jzq''
        ],
        // 用于替换html指向函数后若不做操作将会输出空
        // ''item''=>function($index, $label, $name, $checked, $value){
            // 这块跟encode是在下才疏学浅暂时还未明白啥子用处,待弄明白后在补上,若有码友知晓这块具体作用用法,希望不吝赐教,感激
            // echo $index, $label, $name, $checked, $value;
        // },
        // 除此yii2有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>

                       
<?php ActiveForm::end();?>

 

  2.4 select下拉列表系列

<?php $form=ActiveForm::begin([''id''=>''login'',''class''=>''login''])?>

    <!-- 
        dropDownList方法
        下拉列表
     -->
     <?= $form->field($model, ''username'')->dropDownList([
        //  二维数组直接回报上组标签
        ''test''=>[
            ''1''=>''篮球'',
            ''2''=>''足球'',
        ],
        ''3''=>''游戏'',
        ''4''=>''读书''
    ],[
        // 设置下拉列表的默认请选择选项
        ''prompt''=>[
            ''text'' => ''请选择'', 
            ''options'' => [''value'' => ''none'', ''class'' => ''prompt'', ''label'' => ''Select'']
        ],
        ''encode''=>false,
        // 对select option的设置条件以及更改内容
        ''options''=>[
            // 设置禁止选择项
            ''2'' => [''disabled'' => true],
            //替换或者追加指定key的内容,实际上原内容还在只是假设了 label 属性 和显示了 label的属性值
''4'' => [''label'' => ''value 2''],
        ],
        ''encodeSpaces''=>true
        // 除此yii2有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>

    

        
                       
<?php ActiveForm::end();?>

   2.5 widget 小部件

<?php $form=ActiveForm::begin([''id''=>''login'',''class''=>''login''])?>

    <!-- 
        小部件
        用于强制输入正确内容的input部件
     -->
    <?= $form->field($model, ''username'',[
        ''template''=>''<h2>test</h2> {label} {input} {error}''
    ])->widget(\yii\widgets\MaskedInput::className(), [
        // 指定input类型
        // ''type''=>''time'',
        // 指定必须输入的类型
        ''mask'' => ''999-9999-9999'',
        ''options''=>[''class'' => ''form-control test'']
    ]); ?>

    <!-- 
        用于生成带图片验证的input小部件
        -->
    <?= $form->field($model, ''verifyCode'')->widget(Captcha::className(), [
        ''captchaAction'' => ''login/captcha'',
        ''options'' => [
            ''class'' => ''two'',
            ''id''=>''two'',
            ''placeholder'' => ''请输入验证码'',
        ],
        ''template'' => ''{input}{image}'',
        ''imageOptions'' => [
            ''alt'' => ''images'',
        ]
    ])?>
  --------------------- 最后一个并未实测   -------------------------------
<!-- 自定义小部件 需在widget文件定义源文件 --> <?= $form->field($model, ''username'')->widget(''WidgetClassName'', [ // configure additional widget properties here ]) ?> <?php ActiveForm::end();?>

 以上是这段时间使用的一篇小总结 如有用法错误敬请指点

html – Yii – CActiveForm – 附加表单属性

html – Yii – CActiveForm – 附加表单属性

我需要将data-ajax =“false”属性传递给表单. jQuery Mobile需要此属性才能使其停止使用Ajax加载页面.

这是我的代码:

<div>
<?PHP $form=$this->beginWidget('CActiveForm',array(
    'id'=>'login-form','enableClientValidation'=>true,'clientOptions'=>array(
        'validateOnSubmit'=>true,),'focus'=>array($model,'username'),'data-ajax'=>false,)); ?>

......

<?PHP $this->endWidget(); ?>
</div><!-- form -->

所以我的html输出看起来像这样:

<form id="login-form" method="post" action="/*********" data-ajax ="false">

不幸的是,我得到一个CException“属性”CActiveForm.data-ajax“没有定义.”错误.我错过了什么吗?

解决方法

通过添加解决了这个问题

'htmlOptions' => array("data-ajax"=>"false")


<?PHP $form=$this->beginWidget('CActiveForm','htmlOptions' => array("data-ajax"=>"false"),)); ?>

oracle 创建表空间 Yii2创建表单ActiveForm方法详解

oracle 创建表空间 Yii2创建表单ActiveForm方法详解

php – Yii2 ActiveForm字段选项不起作用

php – Yii2 ActiveForm字段选项不起作用

根据Yii2的 official tutorial.我已经创建了一个视图的输入表单:
<?PHP
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    ?>
    <?PHP $form = ActiveForm::begin(); ?>
<!-- GET Attention for the next Line -->
    <?= $form->field($model,'name')->label('Your Name'); ?>
    <?= $form->field($model,'email'); ?>
    <div>
      <?=  Html::submitButton('Send!',['class' => 'btn btn-primary']); ?>
    </div>    
    <?PHP ActiveForm::end(); ?>

在这一点上,一切都很好.但是,当我尝试使用field方法的参数选项,如下所示:

<?= $form->field($model,'name',['style' => 'color:red'])->label('Your Name'); ?>

我有错误:

UnkNown Property – yii\base\UnkNownPropertyException

Setting unkNown property: yii\widgets\ActiveField::style

The official api documentation表示ActiveForm的方法采用第三个参数称为选项

有人可以解释一下为什么这个错误发生了吗?

尝试
<?= $form->field($model,'name')->textInput(['style' => 'color:red'])->label('Your Name'); ?>

当你做$form->字段($model,’name’),而不指定字段类型时,实际上是要求一个textInput,这是一个难点.但是这并不意味着你应该从 – >字段(以及> textInput相同的方式来获取参数)(如果你需要为这个字段添加一些特殊的参数,你必须使用显式的> textInput([‘style’=>’color:red’])

php – Yii2:ActiveForm字段数字,长度=> 8

php – Yii2:ActiveForm字段数字,长度=> 8

我正在尝试使yii2验证我的ActiveForm字段,该字段应为数字且长度为8个字符.

以下是我在yii2 / advanced / backend的默认LoginForm模型中尝试过的,但不幸的是isNumeric验证器根本没有启动:

public function rules()
{
    return [
        // username and password are both required
        [['username','password'],'required'],// username should be numeric
        ['username','isNumeric'],'string','length'=>8],// password is validated by validatePassword()
        ['password','validatePassword'],];
}

/**
 * Validates if the username is numeric.
 * This method serves as the inline validation for username.
 *
 * @param string $attribute the attribute currently being validated
 * @param array $params the additional name-value pairs given in the rule
 */
public function isNumeric($attribute,$params)
{
    if (!is_numeric($this->username))
        $this->addError($attribute,Yii::t('app','{attribute} must be numeric',['{attribute}'=>$attribute]));
}

/**
 * Validates the password.
 * This method serves as the inline validation for password.
 *
 * @param string $attribute the attribute currently being validated
 * @param array $params the additional name-value pairs given in the rule
 */
public function validatePassword($attribute,$params)
{
    if (!$this->hasErrors()) {
        $user = $this->getUser();
        if (!$user || !$user->validatePassword($this->password)) {
            $this->addError($attribute,'Incorrect username or password.');
        }
    }
}

我还尝试添加一个相关帖子(https://stackoverflow.com/a/27817221/2037924)中建议的场景,但只有在场景中没有包含密码字段时才会起作用(如显示错误).

这是实现这一目标的好方法,还是你能想到一个更好的方法吗?

注意:我将用户名定义为字符串的原因是因为数字可能包含前导0.

在此处阅读有关验证的更多信息: http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#number

这对我使用yii2-basic的联系表格很好

/**
 * @return array the validation rules.
 */
public function rules()
{
    return [
        // name,email,subject and body are required
        [['name','email','subject','body'],// email has to be a valid email address
        ['email','email'],['subject','is8NumbersOnly'],// verifyCode needs to be entered correctly
        ['verifyCode','captcha'],];
}

public function is8NumbersOnly($attribute)
{
    if (!preg_match('/^[0-9]{8}$/',$this->$attribute)) {
        $this->addError($attribute,'must contain exactly 8 digits.');
    }
}

关于yii2之ActiveForm表单使用easyui表单设计器的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于html – Yii – CActiveForm – 附加表单属性、oracle 创建表空间 Yii2创建表单ActiveForm方法详解、php – Yii2 ActiveForm字段选项不起作用、php – Yii2:ActiveForm字段数字,长度=> 8的相关信息,请在本站寻找。

本文标签: