我的页面上有大约50个控件(使用选项卡),我更担心的一件事是验证部分,在该部分中我可以看到相同的source_file.cpp:17:37: error: no matching function for call to ‘radix_sort(std::vector<unsigned int>::iterator, std::vector<unsigned int>::iterator)’
radix_sort(vec.begin(), vec.end());
^
source_file.cpp:6:6: note: candidate: template<class ForwardIt, class auto:1> void radix_sort(ForwardIt, ForwardIt, std::function<auto:1(typename std::iterator_traits<_Iter>::value_type)>)
void radix_sort(
^
source_file.cpp:6:6: note: template argument deduction/substitution failed:
source_file.cpp:17:37: note: couldn't deduce template parameter ‘auto:1’
radix_sort(vec.begin(), vec.end());
对所有控件再次重复。
我可以将验证者分为2个,
Validators
但是我使用常规的验证方法,因此我的代码变得更长。无论如何,我可以使用2 1. [Validators.required, Validators.maxLength(50)]
2. Validators.required
来包含控件并应用验证吗?
FormArray
可以使此代码更易于阅读吗?
类似
this.candidateForm = new FormGroup({
firstName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
lastName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
fatherName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
motherName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
placeOfBirth: new FormControl('', Validators.required),
nationalityList: new FormControl('', Validators.required),
dateOfBirth: new FormControl('', [Validators.required, Validators.maxLength(50)]),
genderList: new FormControl('', Validators.required),
});
通过这种方式,我可以添加控件,如果它增加了。
答案 0 :(得分:0)
我将创建一个函数:例如
function createFormControl(maxLen: number = 0) {
const validators = [Validators.required];
if (maxLen > 0) validators.push(Validators.maxLength(maxLen));
return new FormControl('', validators)
}
并像这样使用它:
const MAX_LEN = 50;
this.candidateForm = new FormGroup({
firstName: createFormControl(MAX_LEN),
lastName: createFormControl(MAX_LEN),
...
placeOfBirth: createFormControl(),