gpt4 book ai didi

php - 表单规范数据与 View 数据——有什么区别?

转载 作者:可可西里 更新时间:2023-10-31 23:06:00 26 4
gpt4 key购买 nike

The documentation说:

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.

但是我不明白常模数据和 View 数据的区别。常模数据的用例是什么?文档的下一段 "So why Use the Model Transformer?"对我来说没有任何意义。

最佳答案

我找到的最好的解释是在 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
{
...
}

编辑:这是来自 Sf 表单组件(合)作者的视频,他在其中解释了数据格式并给出了规范化格式使用的示例 -> https://youtu.be/Q80b9XeLUEA?t=7m6s


所以,基本上,每个表单都有三种不同格式的数据。前两个是:模型数据 - 您在领域模型中使用的数据,以及 View 数据 - 您通常在 HTML 中输出为字符串或在提交前输入表单字段的数据。

两个简单的示例是 TextType 和 NumberType。在TextType中,模型数据是字符串, View 数据是字符串。

在 NumberType 中,模型数据是一个 float - 在您的应用程序中,您处理的是 float 类型,而 View 数据是本地化的字符串。

但是,另一方面,如果我们看一下 DateType,事情会有点复杂,因为模型数据可能是字符串 ('YYYY-MM-DD')、整数(unix 时间戳),数组或 DateTime 对象。 View 数据可以是本地化的字符串或数组。

enter image description here

例如,如果你想写一个事件监听器,你不能确定你会得到什么数据格式:

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();
}
}

幸运的是,Sf form还有第三种数据格式——归一化格式。该格式是静态的 - 您始终知道会得到什么,它始终是相同的数据类型!

enter image description here

 public function onSubmit(FormEvent $event)
{
// $data will be DateTime object!
$data = $event->getForm()->getNormData();
}

关于php - 表单规范数据与 View 数据——有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41116099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com