raftalks/form

使用PHP轻松创建表单。特别适合Laravel 4。

1.3.1 2013-06-25 08:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:43:49 UTC


README

Form Maker可以帮助在PHP中构建表单。专门为Laravel 4开发的包。

#更新到版本1.3.1

  • 修复了命名空间结构

##更新到版本1.2.3 ###变更日志

  • 添加了对创建表格、tr、th、td、thead、tbody等元素以生成HTML表格的支持
  • 添加了对HTML容器类型标签的额外支持
  • 添加了可选的第二个参数来设置容器的属性方法,例如 $form->setNgBind($value, 'ng-bind'); 这将强制属性设置为第二个参数中给出的值。

##更新到版本1.2.2 ###变更日志

  • 添加了对在嵌套闭包之间共享数据的支持
  • 添加了共享表单错误的方法
  • 添加了对在容器内部使用putText的支持
  • 添加了对使用setAttributes()方法填充属性数组的标签元素的支持
  • 添加了include_all(Closure $callback)方法,用于在生成的所有表单中包含模板
  • 修复了选择输入类型的问题

##更新到版本1.2.0 ###变更日志

  • 添加了对创建宏的支持
  • 添加了对创建全局Html标签装饰器的支持
  • 添加了对包含用于高级用户界面的模板结构的支持

###从版本1.0.0升级

  • 使用Composer update命令下载更新
  • 在Laravel 4中,添加额外的类Html别名

用于Laravel 4的使用

通过composer安装包。现在我们需要在L4 app/config/app.php文件中将类别名放入其中。找到在providers键下面的aliases键,并将其以下内容放入其数组中。

	'Form'	 => 'Form\Form',

现在您可以使用Form::make(function($form){ ...在这里您可以放置表单字段 ...});尝试使用它

#功能以下展示了如何使用此包库制作表单。

1.2.3版本新增功能

//making table based forms

Form::make('div', function($form))
{
	$form->table(function($table)
	{
		$table->thead(function($table)
		{
			$table->tr(function($tr)
			{
				$tr->th('item');
				$tr->th('category');
			});
		});


		$table->tr(function($tr)
		{	
			$tr->td()->ng_bind('item.name','ng-bind');
			$tr->td()->ng_bind('item.category','ng-bind');

			$tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
		});

		$table->setClass('table');
	});
});

1.2.2版本新增功能

####所有生成的表单的常见表单元素当与像Laravel这样的框架一起使用时,您可能希望在所有表单中包含一些元素,如隐藏的csrf令牌。我们可以通过创建csrf模板并将其放入Form::include_all()方法中来实现这一点。

Form::include_all(function()
{
	return Form::template('div',function($form)
	{
		$form->hidden('csrf_token')->value(Session::getToken());
		$form->setClass('token');
	});
});

现在我们可以在嵌套闭包方法之间共享变量。例如,我们想要传递POST数据或错误消息,这些用于设置字段的值。

	
Form::make(function($form) use($usergroups, $validation_errors)
{
	
	$form->share('usergroups',$usrgroups);
	$form->share_errors($validation_errors);

	$form->div(function($form)
	{

		$usergroups = $form->get('usergroups');	
		$form->select('usergroup_id',trans('user.usergroup'))->options($usergroups);

	});

});

其他新增功能包括

//sample function to filter errors
function_to_filter_error_by_field($fieldname, $errors)
{
	//filter errors and return matched error
}

//Macro created to show error message for fields
Form::macro('show_error',function($fieldName, $message=null)
{
	return Form::template('span',function($form) use($fieldName, $message)
	{
		$error_messages = $form->get_errors(); 
		$error_message = function_to_filter_error_by_field($fieldName, $error_messages);			


		// the contaner is <span></span> and we are
		// adding the error message as text
		$form->putText($error_message);
		
		// set the container class
		$form->setClass('help-block text-error');
		
	});
});


// Now we are creating a nother styled input text field which uses
// the above show error macro 

Form::macro('input_text', function($name, $label, $value=null, $attr = array())
{
	return Form::template('div',function($form) use ($name, $label, $attr, $value)
	{
			//notice the setAttribute method used here can fill the element with an array
			//of attributes

		$form->text($name)->placeholder($label)->value($value)->setAttributes($attr);
		
		//we are calling the macro to run the template and show the error message for this input template
		$form->show_error($name);

		$form->setClass('input');
	});
});


// Now we use everything above like this to make a real form.

Form::make(function($form) use($validation_errors)
{
	$form->share_errors($validation_errors);

	//This will run the above macro and will show error messages if any exists
	$form->input_text('username','User Name');

});

####1.2.0版本新增功能

//globaly apply attributes to tag elements
	
	//apply attribute to all text input fields
	Form::decorate('text',function($tag)
	{		
		$tag->class('class decorated');
	});

	//Use Form::decorate to apply attribute to all text input fields in templates
	Form::decorate('text',function($tag)
	{		
		$tag->class('class decorated');
	});


//Create Form Macros with template

	//bootstrap controlgroup textfield
	Form::macro('group_text',function($name, $label=null)
	{
		return Form::template(function($form) use($name, $label)
		{
			$form->label($label)->class('control-label');

			$form->div(function($form) use($name)
			{
				$form->text($name);
				$form->setClass('controls');
			});

			$form->setClass('group-controls');
		});

	});

	//the above Macro is now available as a Form field type and can be called within a Form 
	
	Form::make(function($form))
	{
		$form->group_text('telephone','Telephone Number');
	}

// will include more use cases later

###1.0.0版本功能

echo Form::make(function($form)
{
		$form->div(function($form){ //makes a div container for the enclosed fields

			//creates a text input with label
			$form->text('username','User Name')->class('myname')->value('some name');  

			//creates a password input with label
			$form->password('password','Enter Password');

			$form->select('usergroup','User Group')->options(array('admin'=>'admin','manager'=>'manager','user'=>'user'),
									 array('user','admin'))->multiple('multiple');

			$form->setClass('input'); //sets container class
			$form->setId('UserAccount'); //sets container id
		});

		// creates an custom tag element like <group>dome</group> 
		$form->group('dome'); 

		//creates a fieldset container <fieldset></fieldset> and enclose the fields in it
		$form->fieldset(function($form) 
		{
			$form->legend('HelloWOrld');

			$form->label('Your Address')->for('address'); //create label field separately
			$form->text('address');
		});
		
		//create Angularjs type input
		$form->text('timer','Time')->ngmodel('time','ng-model');
		$form->select('countries','select country')->ngrepeat('country.name in countries','ng-repeat');

		$form->submit('Save');
		
		//sets container attributes, therefore, as this is form container, this sets the form attributes
		$form->setId('formIDhere');
		$form->setAction(URL::to('test'));
		$form->setMethod('POST');
		$form->setClass('fill-up');

});

文档

即将更新。

版权和许可证

FormMaker由Raftalks为Laravel框架编写。FormMaker在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。

版权 2011-2012 Raftalks