Files
huangjingfen/pro_v3.5.1/crmeb/form/BaseComponent.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

116 lines
2.3 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace crmeb\form;
/**
* 基础组件集成
* Class BaseComponent
* @package crmeb\form
*/
abstract class BaseComponent
{
/**
* @var bool
*/
protected $init = false;
/**
* @var array
*/
protected $rule = [];
/**
* 数据库验证
* @var array
*/
protected $validate = [];
/**
* @var CommonRule
*/
protected $validataRule;
/**
* 是否实例化
*/
protected function init()
{
if (!$this->init) {
$this->validataRule = new CommonRule;
$this->init = true;
}
}
/**
* 多个验证规则
* @param array $validate
* @return $this
*/
public function validates(array $validate)
{
$this->validate = $validate;
return $this;
}
/**
* 单个验证规则
* @param CommonRule $validate
* @return $this
*/
public function validate(CommonRule $validate)
{
$this->validate[] = $validate;
return $this;
}
/**
* 是否必填
* @return $this
*/
public function required()
{
$this->init();
$this->validataRule->required();
return $this;
}
/**
* 设置提示消息
* @param string $message
* @return $this
*/
public function message(string $message)
{
$this->init();
$this->validataRule->message($message);
return $this;
}
/**
* 数据写入
*/
protected function before()
{
if (!$this->validate && $this->validataRule instanceof CommonRule) {
if (!$this->validataRule->getMessage() && $this->rule['title']) {
$this->validataRule->message('请输入' . $this->rule['title']);
}
$this->validate[] = $this->validataRule->toArray();
}
$validate = [];
foreach ($this->validate as $item) {
if ($item instanceof CommonRule) {
$validate[] = $item->toArray();
} else {
$validate[] = $item;
}
}
$this->rule['validate'] = $validate;
}
}