定制 anlutro/form 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

anlutro/form

Composer 安装命令:

composer require anlutro/form

包简介

Dynamic form builder.

README 文档

README

Sick of the bugs and quirks present in the default Laravel 4 form builder, I wrote my own and made it framework-agnostic. Features include:

  • Each form is its own object with (optionally) its own behaviours
  • Set a model for the form object to pre-fill the form inputs with
  • Get old input from session
  • Validate input

WARNING: Backwards compatibility is not guaranteed during version 0.x.

Installation

composer require anlutro/form

Pick the latest stable version from packagist or the GitHub tag list.

Laravel 4

Add 'anlutro\Form\ServiceProvider' to the list of providers in app/config/app.php.

Other frameworks/raw PHP

You will need to set up a shared instance of anlutro\Form\Builder, and this should be injected into all Form instances.

In order to get input from a Form instance, you should setRequest on the Builder instance. The request should be an instance of Symfony\Component\HttpFoundation\Request.

For old input from session, CSRF tokens and validation to work, you need to construct and set a session and/or validation service on the Builder instance. The interfaces are located in the Adapters namespace, and once you have an object that implements these you can set them via setSessionAdapter and setValidationAdapter.

If you have written an adapter for popular libraries, please consider a pull request so it can be added to the package!

Usage

For simple forms, we'll use the class anlutro\Form\DefaultForm. Inject this into your controller...

use anlutro\Form\DefaultForm;
public function __construct(DefaultForm $form)
{
	$this->form = $form;
}

Or just construct it, if you have an instance of the Form\Builder available.

$this->form = new DefaultForm($formBuilder);

In your controller action you can define the behaviour of the form. All of the following are optional.

// The model is where the form gets its data. It can be an existing active-
// record model, entity or an array of dummy data.
$this->form->setModel($myModel);
$this->form->setAction('http://mysite.com/my-route');
$this->form->setMethod('PUT');

Pass the form to your view in the controller action that shows the form. In your view - using whatever templating engine you have available:

<?= $form->open(['class' => 'form-horizontal']) ?>
<div class="form-group">
  <?= $form->text('my_field', ['class' => 'form-control']) ?>
</div>
<?= $form->close() ?>

In the controller action that handles the POST request of the form:

$input = $this->form->getInput();
$this->service->doStuff($input);

If your form has custom behaviour - getters, setters etc., you can extend the AbstractForm class and inject your custom class instead of the DefaultForm.

use anlutro\Form\AbstractForm;
class MyForm extends AbstractForm {}

Check the examples directory for more information.

Contact

Open an issue on GitHub if you have any problems or suggestions.

License

The contents of this repository is released under the MIT license.

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固