首页 > 其他分享 >Yii2-助手类(Html)

Yii2-助手类(Html)

时间:2024-03-30 14:55:05浏览次数:19  
标签:control Yii2 form primary 助手 Html btn class

Yii2-助手类(Html)

引入命名空间

use yii\helpers\Html;

使用

<?php //【一】表单:Html::beginForm(提交地址,提交方法,属性数组);?>

<?=Html::beginForm('','post',['id'=>'form','class'=>'form','data'=>'myself']);?>

	<?php //【二】输入框:Html::input(类型,name值,默认值,属性数组);?>

	<?=Html::input('text','test','',['class' => 'form-control','placeholder'=>'hehe']);?>
	<?=Html::input('email','email','[email protected]',['class' => 'form-control']);?>
	<?=Html::input('password','pwd','',['class' => 'form-control']);?>
	<?=Html::input('hidden','hidden','',['class' => 'form-control']);?>

	<hr />
	<?php //Html::表单类型Input(name值,默认值,属性数组);?>

	<?=Html::textInput('test','hehe',['class' => 'form-control']);?>
	<?=Html::textInput('email','[email protected]',['class' => 'form-control']);?>
	<?=Html::passwordInput('pwd','',['class' => 'form-control']);?>
	<?=Html::hiddenInput('hidden','',['class' => 'form-control']);?>

	<hr />
	<?php //【三】文本域:Html::textarea(表单name,默认值,属性数组);?>
	<?=Html::textarea('area','',['class'=>'form-control','rows'=>'3']);?>

	<hr />
	<?php //【四】单选按钮:Html::radio(name值,是否选中,属性数组);?>
	<?=Html::radio('sex',true,['calss'=>'form-control']);?>
	<?php //单选按钮列表:Html:;radioList(name值,选中的值,键值对列表,属性数组); ?>
	<?=Html::radioList('height','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【五】复选框:Html::checkbox(name值,是否选中,属性数组);?>
	<?=Html::checkbox('haha',true,['calss'=>'form-control']);?>
	<?php //单选按钮列表:Html:;checkboxList(name值,选中的值,键值对列表,属性数组); ?>
	<?=Html::checkboxList('xixi','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【六】下拉列表:Html:;dropDownList(name值,选中的值,键值对列表,属性数组); ?>
	<?=Html::dropDownList('list','2',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【七】控制标签Label:Html::label(显示内容,for值,属性数组); ?>
	<?=Html::label('显示的','test',['style'=>'color:#ff0000']);?>

	<hr />
	<?php //【八】上传控件:Html::fileInput(name值,默认值,属性数组); ?>
	<?=Html::fileInput('img',null,['class'=>'btn btn-default']);?>

	<hr />
	<?php //【九】按钮:; ?>
	<?=Html::button('普通按钮',['class'=>'btn btn-primary']);?>
	<?=Html::submitButton('提交按钮',['class'=>'btn btn-primary']);?>
	<?=Html::resetButton('重置按钮',['class'=>'btn btn-primary']);?>

<?=Html::endForm();?>

与模型关联的Html

<?php
use yii\helpers\Html;
?>
<?=Html::beginForm('','post',['name'=>'article']);?>
	<?=Html::activeInput('text',$model,'title',['class'=>'form-control']);?>
	<hr />
	<?=Html::activeTextInput($model,'title',['class'=>'form-control']);?>
	<?=Html::activeTextArea($model,'desc',['class' => 'form-control']);?>
	<?=Html::activeTextArea($model,'content',['class'=>'form-control']);?>
<?=Html::endForm();?>

标签:control,Yii2,form,primary,助手,Html,btn,class
From: https://www.cnblogs.com/hu308830232/p/18105491

相关文章

  • yii2 小部件
    yii2小部件小部件基本上在views中使用,在视图中可调用yii\base\Widget::widget() 方法使用小部件。继承yii\base\Widget类并覆盖yii\base\Widget::init() 和/或yii\base\Widget::run() 方法可创建小部件。创建小部件<?phpnamespaceapp\widgets;useyii\base\Wid......
  • yii2依赖注入
    yii2依赖注入所谓依赖注入,实质上就是当某个类对象需要使用另一个类实例的时候,不在类内部实例化另一个类,而将实例化的过程放在类外面实现,实例化完成后再赋值给类对象的某个属性。 这样的话该类不需要知道赋值给它的属性的对象具体属于哪个类的,当需要改变这个属性的类型的时候,无需......
  • yii2事件
    yii2事件事件可以将自定义代码“注入”到现有代码中的特定执行点。1yii\base\Event封装了与时间相关的有关数据并提供了一些功能函数作为辅助classEventextendsObject{public$name;//事件名public$sender;//事件发布者,通常是......
  • Yii2验证器Validator
    Yii2验证器Validator控制器publicfunctionactionIndex(){$data=['LoginForm'=>['username'=>'huyongjian','password'=>'1234']];$model=newLoginForm();$model->load($......
  • yii2-队列使用
    yii2-队列使用安装yii2-queuecomposerrequireyiisoft/yii2-queue安装yii2-rediscomposerrequireyiisoft/yii2-redis控制台配置文件console.php和web.php'redis'=>['class'=>'yii\redis\Connection',&......
  • Yii2 过滤器
    Yii2过滤器过滤器AccessControllerAccessControl基于一组规则提供简单的访问控制/***{@inheritdoc}*/publicfunctionbehaviors(){return['access'=>['class'=>AccessControl::class,......
  • yii2数据库访问对象
    yii2数据库访问对象配置数据库链接$db=newyii\db\Connection(['dsn'=>'mysql:host=localhost;dbname=xhj','username'=>'root','password'=>'123456',......
  • yii2 扩展
    yii2扩展示例安装扩展composerrequire--prefer-distyiisoft/yii2-imagine使用扩展useyii\imagine\Image;publicfunctionactionIndex(){Image::thumbnail('@webroot/img/test.jpg',120,120)->save(Yii::getAlias('@runt......
  • yii2表单使用
    yii2表单使用模型models/form/EntryForm.php<?phpnamespaceapp\models\form;useyii\base\Model;classEntryFormextendsModel{public$name;public$email;publicfunctionrules(){return[[['name',&......
  • yii2 rules验证规则大全
    yii2rules验证规则大全required : 必须值验证属性[['name','email'],'required'][['name'],'required','message'=>'提示信息']email : 邮箱验证[['email'],'email','message'=......