@ember-intl/cp-验证
添加对 嵌入层 在嵌入式验证中
安装
ember install @ember-intl/cp-validations
配置
在您的翻译中执行下列验证信息:
# <project root>/translations/en.yaml
errors:
description: "This field"
inclusion: "{description} is not included in the list"
exclusion: "{description} is reserved"
invalid: "{description} is invalid"
confirmation: "{description} doesn't match {on}"
accepted: "{description} must be accepted"
empty: "{description} can't be empty"
blank: "{description} can't be blank"
present: "{description} must be blank"
collection: "{description} must be a collection"
singular: "{description} can't be a collection"
tooLong: "{description} is too long (maximum is {max} characters)"
tooShort: "{description} is too short (minimum is {min} characters)"
before: "{description} must be before {before}"
after: "{description} must be after {after}"
wrongDateFormat: "{description} must be in the format of {format}"
wrongLength: "{description} is the wrong length (should be {is} characters)"
notANumber: "{description} must be a number"
notAnInteger: "{description} must be an integer"
greaterThan: "{description} must be greater than {gt}"
greaterThanOrEqualTo: "{description} must be greater than or equal to {gte}"
equalTo: "{description} must be equal to {is}"
lessThan: "{description} must be less than {lt}"
lessThanOrEqualTo: "{description} must be less than or equal to {lte}"
otherThan: "{description} must be other than {value}"
odd: "{description} must be odd"
even: "{description} must be even"
positive: "{description} must be positive"
date: "{description} must be a valid date"
email: "{description} must be a valid email address"
phone: "{description} must be a valid phone number"
url: "{description} must be a valid url"
翻译验证器描述
翻译验证人的描述时具体说明: descriptionKey 以匹配翻译中的密钥.
// app/models/user.js
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
username: validator('presence', {
presence: true,
descriptionKey: 'key.for.username'
})
});
# translations/en-us.yml
key:
for:
username: 'Username'
# translations/sv-se.yml
key:
for:
username: 'Användarnamn'
自定义前缀
更改错误前缀密钥 errors 对任何其他键,例如: validationErrors 您只需在 app/validators/messages.js现在只需要修改您的翻译文件, 以嵌入在 validationErrors 对象代替 errors。 。 。 .
// app/validators/messages.js
import ValidatorsMessages from '@ember-intl/cp-validations/validators/messages';
export default ValidatorsMessages.extend({
prefix: 'validationErrors'
});
覆盖默认翻译密钥
默认情况下,将解决翻译问题 validatorPrefix.validatorType。如果需要完全覆盖此功能并指定您自己的信件密钥,您可以使用 messageKey 在验证对象上.
// app/models/user.js
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
username: validator('presence', {
presence: true,
messageKey: 'username.missing'
})
});
# translations/en-us.yml
username:
missing: '{description} is missing'
# translations/sv-se.yml
username:
missing: '{description} saknas'
禁用缺失的翻译警告
要压制控制台对缺少翻译的警告, 您可以设置 intl_cp_validations.suppressWarnings 内 config/environment· ;
// config/environment.js
module.exports = function(environment) {
const ENV = {};
if (environment === 'test') {
ENV.intl_cp_validations = ENV.intl_cp_validations || {};
ENV.intl_cp_validations.suppressWarnings = true;
}
return ENV;
}
兼容性
ember-cp-validationsưμ㼯aember-intlv8 或以上区域- Ember.js v4.12 或以上 (中文(简体) )
- 节点.js v22 或以上
捐款
见 捐款 详细指南.
许可证
本项目根据以下规定获得许可证: MIT 许可证。 。 。 .