diff --git a/public/docs/ts/latest/cookbook/form-validation.jade b/public/docs/ts/latest/cookbook/form-validation.jade index 2aad26febf..c483d67137 100644 --- a/public/docs/ts/latest/cookbook/form-validation.jade +++ b/public/docs/ts/latest/cookbook/form-validation.jade @@ -74,7 +74,7 @@ a#template1 to the elements. Angular interprets those as well, adding validator functions to the control model. 要验证用户输入,你添加[HTML验证属性](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation)到元素中。 - Angular拦截这些元素,添加验证器功能到控制模型中。 + Angular拦截这些元素,添加验证器函数到控制模型中。 Angular exposes information about the state of the controls including whether the user has "touched" the control or made changes and if the control values are valid. @@ -442,7 +442,7 @@ a#reactive * add, change, and remove validation functions on the fly - * 随时添加、修改和删除验证功能 + * 随时添加、修改和删除验证函数 * manipulate the control model dynamically from within the component @@ -489,6 +489,10 @@ a#reactive :marked Then we modify the template HTML elements to match the _reactive forms_ style. Here is the "name" portion of the template again, revised for reactive forms and compared with the template-driven version: + + 接下来,我们修改模板HTML元素,来匹配**响应式表单**样式。 + 下面又是“name”部分的模板,响应式表单修改版本和模板驱动版本的比较: + +makeTabs( `cb-form-validation/ts/app/reactive/hero-form-reactive.component.html, cb-form-validation/ts/app/template/hero-form-template2.component.html`, @@ -498,39 +502,66 @@ a#reactive :marked Key changes: + + 关键变化: + - the validation attributes are gone (except `required`) because we'll be validating in code. + - 验证属性没有了(除了`required`),因为我们将在代码中验证。 + - `required` remains, not for validation purposes (we'll cover that in the code), but rather for css styling and accessibility. + - 保留了`required`,不是为了验证目的(我们将在代码中再行解释),而是为了CSS样式和可访问性。 + .l-sub-section :marked A future version of reactive forms will add the `required` HTML validation attribute to the DOM element - (and perhaps the `aria-required` attribute) when the control has the `required` validator function. + (and perhaps the `aria-required` attribute) when the control has the `required` validator function. + + 未来版本的响应式表单将会在控制器有`required`验证器函数时,添加`required` HTML验证属性到DOM元素(也可能添加`aria-required`属性)。 Until then, apply the `required` attribute _and_ add the `Validator.required` function to the control model, as we'll do below. + 在此之前,添加`required`属性**以及**添加`Validator.required`函数到控制器模型,像我们下面这样做: + :marked - the `formControlName` replaces the `name` attribute; it serves the same purpose of correlating the input box with the Angular form control. + - `formControlName`替换了`name`属性;它起到了关联输入框和Angular表单控制器的同样作用。 + - the two-way `[(ngModel)]` binding is gone. The reactive approach does not use data binding to move data into and out of the form controls. We do that in code. + - 双向`[(ngModel)]`绑定消失了。 + 响应式表单方法不使用数据绑定从表单控制器移入和移出数据。我们在代码中做这些。 + .l-sub-section :marked The retreat from data binding is a principle of the reactive paradigm rather than a technical limitation. + + 不适用表单数据绑定是响应式模式的原则,而非技术局限。 :marked ### Component class - The component class is now responsible for defining and managing the form control model. + ### 组件类 + + The component class is now responsible for defining and managing the form control model. + + 组件类现在负责定义和管理表单控制器模型。 Angular no longer derives the control model from the template so we can no longer query for it. We create the Angular form control model explicitly with the help of the `FormBuilder`. + Angular不再从模板衍生控制器模型,所以我们不能再查询它。 + 我们利用`FormBuilder`来显式创建Angular表单控制器模型。 + Here's the section of code devoted to that process, paired with the template-driven code it replaces: + + 下面是负责该进程的代码部分,与被它取代的模板驱动代码相比: +makeTabs( `cb-form-validation/ts/app/reactive/hero-form-reactive.component.ts, cb-form-validation/ts/app/template/hero-form-template2.component.ts`, @@ -540,60 +571,114 @@ a#reactive :marked - we inject the `FormBuilder` in a constructor. + - 我们注入`FormBuilder`到构造函数中。 + - we call a `buildForm` method in the `ngOnInit` [lifecycle hook method](../guide/lifecycle-hooks.html#hooks-overview) because that's when we'll have the hero data. We'll call it again in the `addHero` method. + + - 我们在`ngOnInit`[生命周期钩子方法](../guide/lifecycle-hooks.html#hooks-overview)中调用`buildForm`方法, + 因为这正是我们拥有英雄数据的时刻。我们将在`addHero`方法中再次调用它。 + .l-sub-section :marked A real app would retrieve the hero asynchronously from a data service, a task best performed in the `ngOnInit` hook. + + 真实的应用很可能从数据服务异步获取英雄,这个任务最好在`ngOnInit`生命周期钩子中进行。 :marked - the `buildForm` method uses the `FormBuilder` (`fb`) to declare the form control model. Then it attaches the same `onValueChanged` handler (there's a one line difference) to the form's `valueChanged` event and calls it immediately to set error messages for the new control model. + - `buildForm`方法使用`FormBuilder`(`fb`)来声明表单控制器模型。 + 然后它将相同的`onValueChanged`(有一行代码不一样)处理器附加到表单的`valueChanged`事件, + 并立刻为新的控制器模型设置错误消息。 + :marked #### _FormBuilder_ declaration - The `FormBuilder` declaration object specifies the three controls of the sample's hero form. + + #### _FormBuilder_声明 + + The `FormBuilder` declaration object specifies the three controls of the sample's hero form. + + `FormBuilder`声明对象指定了本例英雄表单的三个控制器。 Each control spec is a control name with an array value. The first array element is the current value of the corresponding hero field. The (optional) second value is a validator function or an array of validator functions. + + 每个控制器的设置都是控制器名字和数组值。 + 第一个数组元素是英雄控件对应的当前值。 + 第二个值(可选)是验证器函数或者验证器函数数组。 Most of the validator functions are stock validators provided by Angular as static methods of the `Validators` class. Angular has stock validators that correspond to the standard HTML validation attributes. + 大多数验证器函数是Angular以`Validators`类的静态方法的形式提供的原装验证器。 + Angular有一些原装验证器,与标准HTML验证属性一一对应。 + The `forbiddenNames` validator on the `"name"` control is a custom validator, discussed in a separate [section below](#custom-validation). + `"name"`控制器上的`forbiddenNames`验证器是自定义验证器,在下面单独的[小结](#custom-validation)有所讨论。 + .l-sub-section :marked Learn more about `FormBuilder` in a _forthcoming_ chapter on reactive forms. + 到**即将到来**的响应式表单章,学习更多关于`FormBuilder`的知识。 :marked #### Committing hero value changes + + #### 提交英雄值的更新 In two-way data binding, the user's changes flow automatically from the controls back to the data model properties. Reactive forms do not use data binding to update data model properties. The developer decides _when and how_ to update the data model from control values. + 在双向数据绑定时,用户的修改自动从控制器流向数据模型属性。 + 响应式表单不适用数据绑定来更新数据模型属性。 + 开发者决定**何时**与**如何**从控制器的值更新数据模型。 + This sample updates the model twice: + + 本例更新模型两次: + 1. when the user submits the form + + 1. 当用户提交标单时 + 1. when the user chooses to add a new hero + 1. 当用户选择添加新英雄 + The `onSubmit` method simply replaces the `hero` object with the combined values of the form: + + `onSubmit`方法直接使用表单的值得合集来替换`hero`对象: + +makeExample('cb-form-validation/ts/app/reactive/hero-form-reactive.component.ts','on-submit')(format='.') .l-sub-section :marked This example is "lucky" in that the `heroForm.value` properties _just happen_ to correspond _exactly_ to the hero data object properties. + + 本例非常“幸运”,因为`heroForm.value`属性**正好**与英雄数据对象属性对应。 :marked The `addHero` method discards pending changes and creates a brand new `hero` model object. + + `addHero`方法放弃未处理的变化,并创建一个崭新的`hero`模型对象。 + +makeExample('cb-form-validation/ts/app/reactive/hero-form-reactive.component.ts','add-hero')(format='.') :marked Then it calls `buildForm` again which replaces the previous `heroForm` control model with a new one. The `