mvccore/ext-form-field-selection

MvcCore - 扩展 - 表单 - 字段 - 选择 - 表单字段类型 - 选择框、国家选择、复选框、单选按钮、颜色和复选框组。

v5.2.8 2024-09-04 08:22 UTC

README

Latest Stable Version License PHP Version

MvcCore 表单扩展,包括字段选择、国家选择、复选框、单选按钮和颜色。

安装

composer require mvccore/ext-form-field-selection

字段和默认验证器

  • select,国家 select
    • ValueInOptions
      • 默认配置
      • 验证提交的字符串是否存在于选择框选项的键中。
  • input:checkbox
    • SafeString
      • 默认配置
      • 防止XSS字符串攻击,安全显示提交的值,默认配置
  • input:radio - 单选按钮组 input:checkbox - 复选框组
    • ValueInOptions - 默认配置,...以上描述
  • input:color
    • 颜色
      • 默认配置
      • 验证十六进制颜色,不包括透明度,包括前导哈希字符 #

特性

  • 始终在服务器端检查属性 requireddisabledreadonly
  • 所有HTML5特定和全局属性(由 Mozilla 开发者网络文档
  • 每个字段都有其内置特定验证器,如上所述
  • 每个内置验证器在必要时将表单错误添加到会话中,然后所有错误都在错误页面上显示/渲染,并在用户提交后从会话中清除
  • 任何字段都可以自然渲染或使用特定字段类/实例的自定义模板进行渲染
  • 非常可扩展的字段类 - 每个字段都有公共模板方法
    • SetForm() - 在字段实例被添加到表单实例后立即调用
    • PreDispatch() - 在任何字段实例渲染类型之前立即调用
    • Render() - 在表单实例渲染过程中对每个实例调用
      • 子方法:RenderNaturally()RenderTemplate()RenderControl()RenderLabel() ...
    • Submit() - 在表单提交时对每个实例调用

示例

基本示例

$form = (new \MvcCore\Ext\Form($controller))->SetId('job_hunting');
...
$jobQual = new \MvcCore\Ext\Forms\Fields\Select;
$jobQual
	->SetName('job_qualification')
	->SetLabel('Job Qualificatio:')
	->SetOptions([
		'junior'	=> 'Junior Developer',
		'senior'	=> 'Senior developer',
		'manager'	=> 'IT Manager',
	]);
$gender = new \MvcCore\Ext\Forms\Fields\Radio([
	'name'		=> 'gender',
	'label'		=> 'Gender:',
	'options'	=> [
		'M'			=> 'Male',
		'F'			=> 'Female',
		'O'			=> 'Other',
	],
]);
$country = new \MvcCore\Ext\Forms\Fields\CountrySelect([
	'name'		=> 'country',
	'label'		=> 'Country:',
	'filter'	=> ['DE', 'AT', 'FR', 'NL'],
]);
$skills = new \MvcCore\Ext\Forms\Fields\CheckboxGroup([
	'name'		=> 'skills',
	'label'		=> 'I control programming languages:',
	'options'	=> [
		'html'		=> 'HTML5',
		'css'		=> 'CSS3',
		'js'		=> 'Javascript',
		'ts'		=> 'Typescript',
		'php'		=> 'PHP',
		'cs'		=> 'C#',
		'vb'		=> 'Visual Basic',
		'java'		=> 'Java',
		'py'		=> 'Python',
		'pl'		=> 'Perl',
	],
]);
$public = new \MvcCore\Ext\Forms\Fields\Checkbox([
	'name'		=> 'public',
	'label'		=> 'Yes, anybody could see my profile.',
	'checked'	=> TRUE,
]);
$color = new \MvcCore\Ext\Forms\Fields\Color([
	'name'		=> 'profile_color',
	'label'		=> 'My profile color:',
	'value'		=> '#0000FF',
]);

...
$form->AddFields($jobQual, $gender, $country, $skills, $public, $color);