Isi*_*lor 9 php symfony-forms symfony
文件说:
In any form, the three different types of data are:
Model data - This is the data in the format used in your application (e.g. an Issue object). If you call Form::getData() or Form::setData(), you're dealing with the "model" data.
Norm Data - This is a normalized version of your data and is commonly the same as your "model" data (though not in our example). It's not commonly used directly.
View Data - This is the format that's used to fill in the form fields themselves. It's also the format in which the user will submit the data. When you call Form::submit($data), the $data is in the "view" data format.
Run Code Online (Sandbox Code Playgroud)
但我不明白规范数据和视图数据之间的区别.规范数据的用例是什么?文档中的下一段"那么为什么要使用模型变换器?" 对我没有任何意义.
Mat*_*alo 16
我发现的最佳解释是在Symfony的Form类描述中:
<?php
...
namespace Symfony\Component\Form;
use ...
/**
* Form represents a form.
*
* To implement your own form fields, you need to have a thorough understanding
* of the data flow within a form. A form stores its data in three different
* representations:
*
* (1) the "model" format required by the form's object
* (2) the "normalized" format for internal processing
* (3) the "view" format used for display
*
* A date field, for example, may store a date as "Y-m-d" string (1) in the
* object. To facilitate processing in the field, this value is normalized
* to a DateTime object (2). In the HTML representation of your form, a
* localized string (3) is presented to and modified by the user.
*
* In most cases, format (1) and format (2) will be the same. For example,
* a checkbox field uses a Boolean value for both internal processing and
* storage in the object. In these cases you simply need to set a value
* transformer to convert between formats (2) and (3). You can do this by
* calling addViewTransformer().
*
* In some cases though it makes sense to make format (1) configurable. To
* demonstrate this, let's extend our above date field to store the value
* either as "Y-m-d" string or as timestamp. Internally we still want to
* use a DateTime object for processing. To convert the data from string/integer
* to DateTime you can set a normalization transformer by calling
* addNormTransformer(). The normalized data is then converted to the displayed
* data as described before.
*
* The conversions (1) -> (2) -> (3) use the transform methods of the transformers.
* The conversions (3) -> (2) -> (1) use the reverseTransform methods of the transformers.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class Form implements \IteratorAggregate, FormInterface
{
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是来自Sf form component(co)作者的视频,他解释了数据格式并给出了标准化格式使用的示例 - > https://youtu.be/Q80b9XeLUEA?t=7m6s
所以,基本上,每个表单都有三种不同格式的数据.前两个是:模型数据 - 您在域模型中使用的数据,以及视图数据 - 您通常以HTML格式输出的数据,或者在提交之前输入表单字段.
两个简单的例子是TextType和NumberType.在TextType中,模型数据是字符串,视图数据是字符串.
在NumberType中,模型数据是浮点数 - 在您的应用程序中,您正在处理浮点类型,而视图数据是本地化字符串.
但是,另一方面,如果我们看一下DateType,事情有点复杂,因为模型数据可能是字符串('YYYY-MM-DD'),整数(unix时间戳),数组或DateTime对象.视图数据可以是本地化字符串或数组.
例如,如果要编写事件侦听器,则无法确定将获得哪种数据格式:
class MyDateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add...
->addEventListener(
FormEvents::FormEvents::SUBMIT,
array($this, 'onSubmit')
)
;
}
public function onSubmit(FormEvent $event)
{
// BUT WHAT FORMAT DO I GET???
$data = $event->getForm()->getData();
}
}
Run Code Online (Sandbox Code Playgroud)
幸运的是,Sf表格有第三种数据格式 - 规范化格式.这种格式是静态的 - 你总是知道你会得到什么,它总是相同的数据类型!
public function onSubmit(FormEvent $event)
{
// $data will be DateTime object!
$data = $event->getForm()->getNormData();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2103 次 |
最近记录: |