我遇到Validators.compose
功能问题(没有错误,只是没有验证)
这是我的代码:
var validators = ()=> {
var validatorArr = [];
validatorArr.push(Validators.required);
validatorArr.push(Validators.maxLength(column.max));
return Validators.compose(validatorArr);
};
var ctrl: Control = new Control(row.items[column.mapper], validators);
row.form.addControl(column.mapper, ctrl);
当我放置Validators.required
而不是validators
函数时,验证会起作用。
答案 0 :(得分:5)
您应该使用此代码,而不是使用回调来定义验证器:
var validatorArr = [];
validatorArr.push(Validators.required);
validatorArr.push(Validators.maxLength(column.max));
var ctrl: Control = new Control(row.items[column.mapper],
Validators.compose(validatorArr));
row.form.addControl(column.mapper, ctrl);
Control
构造函数的第二个参数对应于可以使用compose
方法创建的函数...
答案 1 :(得分:1)
只使用它而不使用箭头功能:
new Control(row.items[column.mapper],Validators.compose([Validators.required, Validators.minLength(4)]));
或
var validators = ()=> {
return Validators.compose([Validators.required, Validators.minLength(4)]);
}
new Control(row.items[column.mapper], validators());
答案 2 :(得分:1)
Control构造函数需要private class LoginTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(StartActivity.this);
progressDialog.setTitle(“please wait..");
progressDialog.setMessage(“authorizing...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
mFirebase.authWithPassword(email, password, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
// System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
Intent intent2 = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent2);
finish();
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
// error encountered
switch (firebaseError.getCode()) {
case FirebaseError.USER_DOES_NOT_EXIST:
// handle a non existing user
Toast.makeText(getApplicationContext(), getString(R.string.error_user_not_exist)
Toast.LENGTH_LONG).show();
break;
case FirebaseError.INVALID_PASSWORD:
// handle an invalid password
Toast.makeText(getApplicationContext(), getString(R.string.error_invalid_password)
Toast.LENGTH_LONG).show();
break;
default:
// handle other errors
Toast.makeText(getApplicationContext(), getString(R.string.error_reset_password),
Toast.LENGTH_LONG).show();
break;
}
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
作为参数。 ValidatorFn
采用Validators.compose()
数组并返回一个ValidatorFn
。但是你没有传递ValidatorFn
作为论据。你传递的函数在被调用时返回ValidatorFn
。
用
替换你的代码ValidatorFn
或者,更短:
var validatorArr = [];
validatorArr.push(Validators.required);
validatorArr.push(Validators.maxLength(column.max));
var validatorFn = Validators.compose(validatorArr);
var ctrl: Control = new Control(row.items[column.mapper], validatorFn);
row.form.addControl(column.mapper, ctrl);