sinevia/php-library-form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sinevia/php-library-form

Composer 安装命令:

composer require sinevia/php-library-form

包简介

PHP Library Form

README 文档

README

Gitpod Ready-to-Code

Form

A form helper

Building Forms

$form = \Sinevia\Form::build($fields)->toHtml();

Validating Forms

$isValidOrErrors = \Sinevia\Form::validate($fields);

Fields

The field is an associative array consisting of the following key-value pairs:

  • type - one of text, textarea, select, hidden, html
  • name - name of the input field as seen in the request
  • label - publicly visible name
  • width - width of the field - min 1, max 12
  • rule - rules for the field, used when validating
  • value - value of the field
  • options - array of options (used by the select type)
  • html - raw HTML to be displayed as-is (used by the html type)

Example:

[
    'type' => 'text',
    'name' => 'FirstName',
    'label' => 'First name',
    'width' => 6,
    'rule' => 'required',
    'value' => $value,
]

Full Example

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
}

统计信息

  • 总下载量: 60
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2018-10-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固