sinevia / php-library-form
PHP 库表单
v0.1.0
2020-03-13 08:08 UTC
Requires
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-13 17:40:43 UTC
README
表单
表单助手
构建表单
$form = \Sinevia\Form::build($fields)->toHtml();
验证表单
$isValidOrErrors = \Sinevia\Form::validate($fields);
字段
字段是一个包含以下键值对的关联数组
- type - text、textarea、select、hidden、html 之一
- name - 请求中可见的输入字段名称
- label - 公开可见的名称
- width - 字段宽度 - 最小 1,最大 12
- rule - 字段的规则,用于验证时使用
- value - 字段的值
- options - 选项数组(用于 select 类型)
- html - 原始 HTML,将按原样显示(用于 html 类型)
示例
[
'type' => 'text',
'name' => 'FirstName',
'label' => 'First name',
'width' => 6,
'rule' => 'required',
'value' => $value,
]
完整示例
function formProfileFields($user) { $countriesList = \App\Models\Countries\Country::all()->pluck('Name', 'Iso2')->toArray(); asort($countriesList); $countries = ['' => '- country -'] + $countriesList; $daysRange = range(1, 12); $days = [ '' => '- day -' ]; foreach ($daysRange as $day) { $days[$day] = $day; } $monthsRange = range(1, 12); $months = [ '' => '- month -' ]; foreach ($monthsRange as $month) { $months[$month] = $month; } $yearsRange = range(1940, 2014); $years = [ '' => '- year -' ]; foreach ($yearsRange as $year) { $years[$year] = $year; } $fields = [ [ 'type' => 'html', 'html' => '<style>.btn-success { width:100%; padding:10px;}</style>', ], [ 'type' => 'html', 'html' => '<div class="col-sm-12"><h3>Profile</h3></div>', ], [ 'type' => 'text', 'name' => 'FirstName', 'label' => 'First name', 'width' => 6, 'rule' => 'required', 'value' => $user->FirstName, ], [ 'type' => 'text', 'name' => 'LastName', 'label' => 'last name', 'width' => 6, 'rule' => 'required', 'value' => $user->LastName, ], [ 'type' => 'select', 'name' => 'DayOfBirth', 'options' => $days, 'label' => 'Day of Birth', 'width' => 2, 'rule' => 'required', 'value' => is_null($user->Birthday) ? '' : date('d', strtotime($user->Birthday)), ], [ 'type' => 'select', 'name' => 'MonthOfBirth', 'options' => $months, 'label' => 'Month of Birth', 'width' => 2, 'rule' => 'required', 'value' => is_null($user->Birthday) ? '' : date('m', strtotime($user->Birthday)), ], [ 'type' => 'select', 'name' => 'YearOfBirth', 'options' => $years, 'label' => 'Year of Birth', 'width' => 2, 'rule' => 'required', 'value' => is_null($user->Birthday) ? '' : date('Y', strtotime($user->Birthday)), ], [ 'type' => 'html', 'html' => '<div class="col-sm-12"><h3>Address</h3></div>', ], [ 'type' => 'select', 'name' => 'Country', 'label' => 'Country', 'options' => $countries, 'width' => 6, 'rule' => 'required', 'value' => $user->Country, ], [ 'type' => 'text', 'name' => 'City', 'label' => 'City', 'rule' => 'required', 'width' => 6, 'value' => $user->City, ], [ 'type' => 'text', 'name' => 'AddressLine1', 'label' => 'Address Line 1', 'rule' => 'required', 'width' => 6, 'value' => $user->Address1, ], [ 'type' => 'text', 'name' => 'AddressLine2', 'label' => 'Address Line 2 (optional)', 'width' => 6, 'value' => $user->Address2, ], [ 'type' => 'text', 'name' => 'Province', 'label' => 'Province / State / County', 'rule' => 'required', 'width' => 6, 'value' => $user->Province, ], [ 'type' => 'text', 'name' => 'Postcode', 'label' => 'Postcode / Zip', 'rule' => 'required', 'width' => 6, 'value' => $user->Postcode, ], [ 'type' => 'hidden', 'name' => 'UserId', 'label' => 'UserId', 'rule' => 'required', 'width' => 6, 'value' => $user->Id, ], ]; return $fields; } function formProfile($user) { return \Sinevia\Form::build($this->formProfileFields($user))->toHtml(); } $isValidOrErrorArray = \Sinevia\Form::validate($this->formProfileFields($user)); if(is_array($isValidOrErrorArray)){ // Validation failed, show errors } else { // Validation was successful }