Password 密码输入框

Password 密码输入框 与其他控件一样 支持Field 的所有属性设置,仅支持 string 类型的Model 属性字段注解。
除了 Field 的所有属性以外
Password 密码输入框 还有一个自己的属性:

public string|array|null $encodeFunc = 'md5';

用于计算加密字符串的加密函数,默认是系统自带的 md5 我们也可以更换成其他自定义的加密函数。
使用用例如下:

    #[Password(
        label: '用户密码',
        validRule: ['r' => '请输入密码信息', 'rangeLen' => [6, 30, '请输入6-30个字符']],
        encodeFunc: [self::class, 'pwdEncode']
    )]
    public string $password = '';

    public static function pwdEncode(string $value): string
    {
        return password_hash($value, PASSWORD_DEFAULT);
    }


完整演示示例如下:
UserModel


namespace app\home\model;


use beacon\core\Form;
use beacon\widget\Number;
use beacon\widget\Password;
use beacon\widget\Text;


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

    #[Text(
        label: '用户名',
        validRule: ['r' => '请填写用户名'],
    )]
    public string $username = '';

    #[Password(
        label: '用户密码',
        validRule: ['r' => '请输入密码信息', 'rangeLen' => [6, 30, '请输入6-30个字符']],
        encodeFunc: [self::class, 'pwdEncode']
    )]
    public string $password = '';

    #[Number(
        label: '金额',
        validRule: ['r' => '请填写数值', 'money' => '只能是最多两位小数的数值'],
    )]
    public ?float $money = null;


    public static function pwdEncode(string $value): string
    {
        return password_hash($value, PASSWORD_DEFAULT);
    }

}


模板页面:

<!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');
    }
}


最后呈现效果:



填写后提交 debug.php 打印的信息,其中 $form->getData() 打印出来的 password 已经被加密过了,而 model $user 上的值是没有加密的。

{
    "username": "wj008",
    "password": "$2y$10$R5SZjPinTpyox2KMdLEEP.pjBIOxPRF0g8q3VuxYs3cEDAI2vUbyG",
    "money": 10
}

{
    "___class_name": "app\\home\\model\\UserModel",
    "username": "wj008",
    "password": "123456",
    "money": "10"
}
Copyright © 2021 海南的叶子 All Rights Reserved 琼ICP备2021000725号

琼公网安备 46900702000037号