axn/laravelcollective-form-to-raw-html 问题修复 & 功能扩展

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

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

axn/laravelcollective-form-to-raw-html

Composer 安装命令:

composer require axn/laravelcollective-form-to-raw-html

包简介

Provides Artisan command to replace LaravelCollective Form:: syntax by raw HTML

README 文档

README

Provides Artisan command to replace LaravelCollective Form:: syntax by raw HTML

It searches for {!! Form::<method>(<arguments>) !!} or {{ Form::<method>(<arguments>) }}, then analyzes arguments to determine the HTML tag attributes.

Installation

With Composer, as dev dependency:

composer require axn/laravelcollective-form-to-raw-html --dev

Usage

Simply run this command:

php artisan laravelcollective-form-to-raw-html:run

By default, the command scans all files in resources/views/.

You can precise an other directory:

php artisan laravelcollective-form-to-raw-html:run resources/views/admin/users

Or a single file:

php artisan laravelcollective-form-to-raw-html:run resources/views/admin/users/create.blade.php

NOTE: The target path is always relative to the project root.

The supported methods are:

  • open(options)
  • close()
  • label(name, value, options, escape)
  • labelRequired(name, value, options, escape) (AXN-Informatique macro)
  • hiddenForm(options) (AXN-Informatique macro)
  • input(type, name, value, options)
  • text(name, value, options)
  • number(name, value, options)
  • date(name, value, options)
  • time(name, value, options)
  • datetime(name, value, options)
  • week(name, value, options)
  • month(name, value, options)
  • range(name, value, options)
  • search(name, value, options)
  • email(name, value, options)
  • tel(name, value, options)
  • url(name, value, options)
  • color(name, value, options)
  • hidden(name, value, options)
  • checkbox(name, value, checked, options)
  • radio(name, value, checked, options)
  • file(name, options)
  • password(name, options)
  • textarea(name, value, options)
  • select(name, list, selected, options)
  • button(name, options)
  • submit(name, options)

If a method is not supported, there is no replacement.

Warnings and limitations

Escaped echo without double-encode

LaravelCollective internally used most of the time escaped echo without double-encode:

e($value, false)

This prevents the encoded HTML entities from being encoded a second time (&amp; => &amp;amp;)

For example:

<label>
    {!! e('<strong>Name &amp; firstname</strong>', false) !!}
<label>

The HTML result will be:

&lt;strong&gt;Name &amp; firstname&lt;/strong&gt;

For simplicity and clarity purpose, this package use regular Blade echo syntax instead of escaped echo without double-encode.

If you want to keep the original behavior of LaravelCollective, use --escape-without-double-encode option to the command.

So instead of escaping this way:

{{ $value }}

The values ​​will be escaped like this:

{!! e($value, false) !!}

Automatically retrieve field value

LaravelCollective has a complex method to automatically retrieve the value of the field: it searches in "old" values and in the request. You can see it in the getValueAttribute method of the FormBuilder class.

This was to complex to implement entirely, so the converter only handles the value retrieving from the "old" values.

For example:

{!! Form::text('name') !!}

Will be replaced by:

<input
    type="text"
    name="name"
    id="name"
    value="{{ old('name') }}"
>

If you have fields with name as array, the converter will replace the array syntax by dot syntax for the old helper like LaravelCollective do.

For example:

{!! Form::text('name[0]') !!}

Will be replaced by:

<input
    type="text"
    name="name[0]"
    value="{{ old('name.0') }}"
>

If you already used old helper in the Form:: syntax, the converter will detect it and not doubling the use of the "old" helper.

WARNING 1: If a part of the name is in a variable (for example Form::text('name['.$index.']')), the converter will integrate the replacement function into the output result like this:

<input
    type="text"
    name="name[0]"
    value="{{ old(str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], 'name['.$index.']')) }}"
>

WARNING 2: If you have fields with name as array but with no explicit key (for example name[]), the converter cannot determine what index to use to get the proper value and will simply render old('name') instead of old('name.<index>'). You may need to check these cases to manually set the proper way to retrieve the value (for example: old('name.0')).

Automatically determine radio and checkbox checked state

Like for value retrieving, the converter only handles the checked state retrieving from the "old" values. It uses in_array in case where the "old" value is multiple.

For example:

{!! Form::checkbox('name[]') !!}

Will be replaced by:

<input
    type="checkbox"
    name="name[]"
    value="1"
    @checked(in_array('1', (array) old('name')))
>

If a default checked state is specify, for example:

{!! Form::checkbox('name[]', '1', true) !!}

It will appear like this:

<input
    type="checkbox"
    name="name[]"
    value="1"
    @checked(! old() ? true : in_array('1', (array) old('name')))
>

Select with optgroup

The Form::select method accepts grouped arrays of options to render <optgroup>.

This feature was to complex to implement: this would have required to add to much code in the HTML replacement whose will be not needed 99% of the time (unless you heavy used optgroups). The converter cannot detect if the list argument contains groups so it is not possible to detect cases where optgroups are used. You may need to manually review these cases.

However, the converter can detect if optionsAttributes or optgroupsAttributes arguments of Form::builder have been used. If so, the corresponding Form::select will not be replaced.

Comments

If there are comments in the original syntax, the converter will erase these but the original syntax will be preserve in a comment for manual check. You can retrieve these cases by searching for @TODO CHECK COMMENTS.

For example:

{!! Form::text('name', null, [
    'class' => 'form-control',
    // 'required',
]) !!}

Will be replaced by:

{{-- @TODO CHECK COMMENTS: {!! Form::text('name', null, [
    'class' => 'form-control',
    // 'required',
]) !!} --}}
<input
    type="text"
    name="name"
    id="name"
    value="{{ old('name') }}"
>

Not regular array syntax for options

If the options argument is not a regular array, the converter will process the replacement of the Form:: syntax but will preserve the options in a comment for manual check and manual replacements of the HTML tag attributes. You can retrieve these cases by searching for @TODO CHECK OPTIONS.

For example:

{!! Form::text('name', null,
    array_merge($defaultOptions, [
        'size' => 50,
        'required',
    ])
) !!}

Will be replaced by:

<input
    type="text"
    name="name"
    id="name"
    value="{{ old('name') }}"
    {{-- @TODO CHECK OPTIONS: array_merge($defaultOptions, [
        'size' => 50,
        'required',
    ]) --}}
>

Date fields

LaravelCollective date, time, datetime, week and month methods support a DateTime instance as value and format this internally. The converter cannot detect the value type so you may need to review these cases if DateTime instances have been used as value argument of these methods.

axn/laravelcollective-form-to-raw-html 适用场景与选型建议

axn/laravelcollective-form-to-raw-html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.02k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 axn/laravelcollective-form-to-raw-html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 axn/laravelcollective-form-to-raw-html 我们能提供哪些服务?
定制开发 / 二次开发

基于 axn/laravelcollective-form-to-raw-html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-24