我有以下片段,使用Saaripar的注释来验证某些字段:
/**
* Simple fragment used to register a new user
*/
public static class FragmentRegister extends Fragment implements Validator.ValidationListener {
@InjectView(R.id.display_name_label)
TextView displayNameLabel;
@NotEmpty(messageResId = R.string.validation_not_empty)
@InjectView(R.id.display_name)
EditText displayName;
@InjectView(R.id.username_label)
TextView nameLabel;
@NotEmpty(messageResId = R.string.validation_not_empty)
@InjectView(R.id.username)
EditText username;
@InjectView(R.id.password_label)
TextView passwordLabel;
@InjectView(R.id.password)
@Password
@Size(min = 6, messageResId = R.string.validation_6_characters)
EditText password;
@InjectView(R.id.confirm_password_label)
TextView confirmPasswordLabel;
@InjectView(R.id.confirm_password)
@ConfirmPassword
EditText confirmPassword;
@InjectView(R.id.create_user_button)
Button createUserButton;
private Validator validator;
public FragmentRegister() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_register, container, false);
// Set up Saripaar validator
validator = new Validator(this);
validator.setValidationListener(this);
// Injecting views with ButterKnife
ButterKnife.inject(this, rootView);
return rootView;
}
...
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
// Injected view listeners
@OnClick(R.id.create_user_button)
public void createUser() {
validator.validate();
}
// Saripaar callbacks
@Override
public void onValidationSucceeded() {
clearErrors();
String dispName = displayName.getText().toString();
String user = username.getText().toString();
String pass = password.getText().toString();
Controller.instance().register(user, dispName, pass);
getActivity().finish();
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
clearErrors();
View view = null;
for (ValidationError error : errors) {
view = error.getView();
if (view instanceof EditText) {
((EditText) view).setError(error.getFailedRules().get(0).getMessage(getActivity()));
}
}
if(view != null) {
view.requestFocus();
}
}
public void clearErrors() {
displayName.setError(null);
username.setError(null);
password.setError(null);
confirmPassword.setError(null);
}
}
@ConfirmPassword
从不起作用,它仅在@Password
字段显示空错误消息,而对于@ConfirmPassword
则显示“密码不匹配”。对可能发生的事情有任何想法吗? 最佳答案
https://github.com/ragunathjawahar/android-saripaar/issues/52似乎是一个错误,今天将被修复。
关于android - Saripaar的@ConfirmPassword无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27559552/