Integer 整数插件

Integer 整数插件支持Field 的所有属性设置,只能在 int 类型的Model属性字段中注解。

使用示例如下:

Model

namespace app\home\model;


use beacon\core\Form;
use beacon\widget\Integer;


#注意 这里需要使用表单注解
#[Form(title: '用户表单', table: '@pf_user', template: 'user.form.tpl')]
class UserModel
{

    #[Integer(
        label: '排序权重',
        validRule: ['r' => '请填写数值', 'int' => '只能是整数形式的数值'],
        defaultFunc: [self::class, 'getMaxSort']
    )]
    public ?int $sort = null;

    public static function getMaxSort(): int
    {
        //可以从数据库中查询,这里为了演示直接返回一个值
        return 10;
    }
}


由于使用到验证,所以在表单中我们做相应的修改,使用YeeUI 框架 使用 ajax 提交表单,并添加验证。
主要是在 <form> 标签上加入

yee-module="ajax validate"

修改后的模板代码如下:

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>{$form->title}</title>
    <link type="text/css" rel="stylesheet" href="/yeeui/css/yeeui.css"/>
    <link type="text/css" rel="stylesheet" href="/icofont/icofont.css"/>
    <script src="/yeeui/third/jquery-3.3.1.min.js"></script>
    <script src="/yeeui/yee.js"></script>
</head>
<body>
<div style="margin: 20px">
    <form method="post" yee-module="ajax validate">
        <div class="yee-panel">
            <div class="panel-caption">
                {if $form->getType()=='add'}添加{elseif $form->getType()=='edit'}编辑{/if}{$form->title}
            </div>
            <div class="panel-content">
                {foreach from=$form->getViewFields() item=field}
                    {field_row field=$field}
                {/foreach}
            </div>
            <div class="yee-submit">
                <label class="submit-label"></label>
                <div class="submit-cell">
                    {*输出隐藏域*}
                    {$form->fetchHideBox()|raw}
                    <input type="submit" class="form-btn red" value="提交">
                </div>
            </div>
        </div>
    </form>
</div>
</html>


控制器:

namespace app\home\controller;


use app\home\model\UserModel;
use beacon\core\Controller;
use beacon\core\Form;
use beacon\core\Logger;
use beacon\core\Method;

class User extends Controller
{
    #[Method(act: 'index', method: Method::GET)]
    public function index()
    {
        $form = Form::create(UserModel::class, 'add');
        $this->displayForm($form);
    }

    #[Method(act: 'index', method: Method::POST)]
    public function add()
    {
        $user = new UserModel();
        $form = Form::create($user, 'add');
        $input = $this->completeForm($form);
        Logger::log($input); #input 可以用用于插入或者更新数据库。
        Logger::log($user);
        //这里需要有返回值 否则 ajax 提交会没有任何内容返回
        $this->success('ok');
    }
}


显示效果如下:

如果 清空排序权重的值后提交会提示输入。


输入正确的值后 提交成功。

上一篇:Hidden 隐藏域
下一篇:Number 数值插件
Copyright © 2021 海南的叶子 All Rights Reserved 琼ICP备2021000725号

琼公网安备 46900702000037号