我正在尝试在我的项目中使用 Saripaar Validation,但它引发了以下错误:
java.lang.IllegalStateException:未找到规则。您必须至少有一个要验证的规则。如果您使用自定义注解,请确保您已使用 'Validator.register()' 方法注册它们。
我的架构将处理视图的职责分离到一个类中,例如对于 LoginActivity,我有 LoginView,这是我处理我的视图逻辑的地方,如 ButterKnife 注入、事件监听等。在这个类中,我有我的注释字段。我的 LoginView 看起来像这样:
import android.content.res.ColorStateList;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.ImageViewCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.serti.android.feenicia.validatortest.R;
import com.serti.android.feenicia.validatortest.screen.login.LoginActivity;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.subjects.PublishSubject;
public class LoginView implements Validator.ValidationListener {
public static final String LOGIN_ACTION = "LOGIN";
@BindView(R.id.login_button)
Button loginButton;
@BindView(R.id.username_edittext)
@NotEmpty
@Email
AppCompatEditText usernameEditText;
@BindView(R.id.password_edittext)
@Password(min = 8, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
AppCompatEditText passwordEditText;
@BindView(R.id.eye_imageview)
ImageView eyeImageView;
@BindView(R.id.loading_progressbar)
ProgressBar loadingProgressBar;
private boolean passwordIsVisible;
private AlertDialog confirmPaymentDialog;
private LoginActivity context;
private View rootView;
private PublishSubject<String> clickSubject = PublishSubject.create();
Validator validator;
public LoginView(LoginActivity context){
this.context = context;
FrameLayout parent = new FrameLayout(context);
parent.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
rootView = LayoutInflater.from(context).inflate(R.layout.activity_login, parent, true);
ButterKnife.bind(this, rootView);
validator = new Validator(context);
validator.setValidationListener(this);
eyeImageView.setOnClickListener(v -> handleClick(v.getId()));
loginButton.setOnClickListener(v -> handleClick(v.getId()));
passwordIsVisible = false;
}
@Override
public void onValidationSucceeded() {
String action = null;
Snackbar.make(rootView, "Validated", Snackbar.LENGTH_LONG).show();
loginButton.setVisibility(View.GONE);
loadingProgressBar.setVisibility(View.VISIBLE);
action = LOGIN_ACTION;
if(action != null){
clickSubject.onNext(action);
}
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
for(ValidationError error : errors){
View view = error.getView();
String message = error.getCollatedErrorMessage(context);
if(view instanceof AppCompatEditText){
((AppCompatEditText) view).setError(message);
} else {
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
}
}
}
public String getUserName(){
return usernameEditText.getText().toString();
}
public String getPassword(){
return passwordEditText.getText().toString();
}
private void handleClick(int id){
String action = null;
switch (id){
case R.id.login_button:
validator.validate();
break;
case R.id.eye_imageview:
togglePasswordVisibility();
}
}
private void togglePasswordVisibility(){
int color = 0;
if(passwordIsVisible){
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
// eyeImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.eye_white));
color = ContextCompat.getColor(context, R.color.dark_blue_button);
ImageViewCompat.setImageTintList(eyeImageView, ColorStateList.valueOf(color));
}else{
passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
// eyeImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.eye_white));
color = ContextCompat.getColor(context, R.color.dark_gray);
}
ImageViewCompat.setImageTintList(eyeImageView, ColorStateList.valueOf(color));
passwordIsVisible = !passwordIsVisible;
passwordEditText.setSelection(passwordEditText.getText().length());
}
private void showIncompleteCredentialsMessage() {
Snackbar.make(loginButton, context.getText(R.string.incomplete_credentials_title), Snackbar.LENGTH_LONG).show();
}
public View getView(){
return rootView;
}
public Observable<String> observeClicks(){
return clickSubject;
}
public void openMainScreen(){
context.startMainActivity();
}
public void openSearchTicketsScreen() {
context.startSearchTicketsActivity();
}
public void showWrongCredentialsMessage() {
loginButton.setVisibility(View.VISIBLE);
loadingProgressBar.setVisibility(View.GONE);
Snackbar.make(loginButton, context.getText(R.string.wrong_credentials_title), Snackbar.LENGTH_LONG).show();
}
public void setContext(LoginActivity context) {
this.context = context;
}
}
根据我的猜测,该库正在尝试在我的 Activity 上查找这些字段,并且由于它们不存在,因此它会引发此错误。
有没有办法根据我正在处理的架构使用这个库?