Laravel 如何在表单上自动添加id?
手动添加id如下所示Laravel 如何在表单上自动添加id?,laravel,Laravel,手动添加id如下所示 {!! Form::text('email', null, ['id' => 'email', 'class' => 'active']) !!} 如何自动添加id 有使用宏的示例吗?创建新的FromBuilder类: class MyFormBuilder extends \Collective\Html\FormBuilder /** Original Form Builder*/{ // Override the text function
{!! Form::text('email', null, ['id' => 'email', 'class' => 'active']) !!}
如何自动添加id
有使用宏的示例吗?创建新的FromBuilder类:
class MyFormBuilder extends \Collective\Html\FormBuilder /** Original Form Builder*/{
// Override the text function
public function text($name, $value = null, $options = []){
// If the ID is not explicitly defined in the call
if(!isset($options['id'])){
// Set ID equal to the name
$options['id'] = $name;
}
// Call the original text function with the new ID set
parent::text($name,$value,$options);
}
}
然后创建一个新的服务提供商
<?php
namespace My\Provider\Space;
use Illuminate\Support\ServiceProvider;
class MyHtmlServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerHtmlBuilder();
$this->registerFormBuilder();
$this->app->alias('html', 'Collective\Html\HtmlBuilder');
$this->app->alias('form', 'My\Class\Space\MyFormBuilder');
}
/**
* Register the HTML builder instance.
*
* @return void
*/
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function ($app) {
return new \Collective\Html\HtmlBuilder($app['url'], $app['view']);
});
}
/**
* Register the form builder instance.
*
* @return void
*/
protected function registerFormBuilder()
{
$this->app->singleton('form', function ($app) {
$form = new \My\Class\Space\MyFormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken());
return $form->setSessionStore($app['session.store']);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['html', 'form', 'Collective\Html\HtmlBuilder', 'My\Class\Space\FormBuilder'];
}
}
您要添加哪些ID?它们是数字还是字符串?他们的模式是什么?直接的想法。扩展表单生成器,使用所有要呈现的输入,设置属性并调用父方法。将外观更改为指向您的。@Peyman.H我想对属性进行相同的更改name@Chris你能帮我举个例子吗?我必须补充什么?