Saripaar 验证 Android,多种形式
Saripaar validation Android, multiple forms
我有 3 个相同的 activity(和布局)表单,我想使用 saripaar 进行验证。这验证了三种形式,但它们不独立验证。
这是布局:
- Form1:输入电子邮件并提交按钮。
- Form2:输入验证码并提交按钮。
- Form3: 输入密码,密码确认和提交按钮。
我希望当您为 form1 按提交时,仅验证 form1 的规则,对于 form2 和 form3 也是如此。
我有 3 个验证器对象,每个对象的逻辑都是相似的。例如:
validatorEmail = new Validator(this);
editTextRecoveryEmail = (EditText) findViewById(R.id.editTextRecoveryEmail);
buttonRecoveryEmail = (Button) findViewById(R.id.buttonRecoveryEmail);
validatorEmail.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Enviar el email
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryEmail.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
buttonRecoveryEmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorEmail.validate();
}
});
这是我的完整 activity 代码:
/* Inicializamos los controles */
editTextRecoveryEmail = (EditText) findViewById(R.id.editTextRecoveryEmail);
editTextRecoveryCode = (EditText) findViewById(R.id.editTextRecoveryCode);
editTextRecoveryPassword = (EditText) findViewById(R.id.editTextRecoveryPassword);
editTextRecoveryPasswordConfirm = (EditText) findViewById(R.id.editTextRecoveryPasswordConfirm);
buttonRecoverySubmit = (Button) findViewById(R.id.buttonRecoverySubmit);
buttonRecoveryCode = (Button) findViewById(R.id.buttonRecoveryCode);
buttonRecoveryEmail = (Button) findViewById(R.id.buttonRecoveryEmail);
/* Objeto validador */
validatorEmail = new Validator(this);
validatorCode = new Validator(this);
validatorSubmit = new Validator(this);
validatorEmail.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Enviar el email
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryEmail.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
validatorCode.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Validar código
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryCode.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
validatorSubmit.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Validar Submit
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryPassword.getId()
|| failedView.getId() == editTextRecoveryPasswordConfirm.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
/* Comportamiento del botón de Registro */
buttonRecoveryEmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorEmail.validate();
}
});
/* Comportamiento del botón de Registro */
buttonRecoveryCode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorCode.validate();
}
});
/* Comportamiento del botón de Registro */
buttonRecoverySubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorSubmit.validate();
}
});
这是一个有趣的问题。您需要在 3 个不同的控制器中使用 3 个验证器 类 来处理这种情况。
static class EmailController implements ValidationListener {
@Email
private EditText mEmailEditText;
private Button mSubmitButton;
private Context mContext;
private Validator mValidator;
public EmailController(@NonNull EditText emailEditText, @NonNull Button submitButton) {
mEmailEditText = emailEditText;
mSubmitButton = submitButton;
mContext = emailEditText.getContext();
mValidator = new Validator(this);
mValidator.setValidationListener(this);
mSubmitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validator.validate();
}
});
}
@Override
public void onValidationSucceeded() {
// Success...
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
// Failure...
}
}
您还需要 2 个 - 一个用于验证码,另一个用于密码。如果您希望与您的 activity 通信,请传递它或实施适当的侦听器接口。
如果可能,请考虑简化表单。
我有 3 个相同的 activity(和布局)表单,我想使用 saripaar 进行验证。这验证了三种形式,但它们不独立验证。
这是布局:
- Form1:输入电子邮件并提交按钮。
- Form2:输入验证码并提交按钮。
- Form3: 输入密码,密码确认和提交按钮。
我希望当您为 form1 按提交时,仅验证 form1 的规则,对于 form2 和 form3 也是如此。
我有 3 个验证器对象,每个对象的逻辑都是相似的。例如:
validatorEmail = new Validator(this);
editTextRecoveryEmail = (EditText) findViewById(R.id.editTextRecoveryEmail);
buttonRecoveryEmail = (Button) findViewById(R.id.buttonRecoveryEmail);
validatorEmail.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Enviar el email
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryEmail.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
buttonRecoveryEmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorEmail.validate();
}
});
这是我的完整 activity 代码:
/* Inicializamos los controles */
editTextRecoveryEmail = (EditText) findViewById(R.id.editTextRecoveryEmail);
editTextRecoveryCode = (EditText) findViewById(R.id.editTextRecoveryCode);
editTextRecoveryPassword = (EditText) findViewById(R.id.editTextRecoveryPassword);
editTextRecoveryPasswordConfirm = (EditText) findViewById(R.id.editTextRecoveryPasswordConfirm);
buttonRecoverySubmit = (Button) findViewById(R.id.buttonRecoverySubmit);
buttonRecoveryCode = (Button) findViewById(R.id.buttonRecoveryCode);
buttonRecoveryEmail = (Button) findViewById(R.id.buttonRecoveryEmail);
/* Objeto validador */
validatorEmail = new Validator(this);
validatorCode = new Validator(this);
validatorSubmit = new Validator(this);
validatorEmail.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Enviar el email
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryEmail.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
validatorCode.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Validar código
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryCode.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
validatorSubmit.setValidationListener(new ValidationListener() {
@Override
public void onValidationSucceeded() {
// Validar Submit
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
if (failedView.getId() == editTextRecoveryPassword.getId()
|| failedView.getId() == editTextRecoveryPasswordConfirm.getId()) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
}
});
/* Comportamiento del botón de Registro */
buttonRecoveryEmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorEmail.validate();
}
});
/* Comportamiento del botón de Registro */
buttonRecoveryCode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorCode.validate();
}
});
/* Comportamiento del botón de Registro */
buttonRecoverySubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validatorSubmit.validate();
}
});
这是一个有趣的问题。您需要在 3 个不同的控制器中使用 3 个验证器 类 来处理这种情况。
static class EmailController implements ValidationListener {
@Email
private EditText mEmailEditText;
private Button mSubmitButton;
private Context mContext;
private Validator mValidator;
public EmailController(@NonNull EditText emailEditText, @NonNull Button submitButton) {
mEmailEditText = emailEditText;
mSubmitButton = submitButton;
mContext = emailEditText.getContext();
mValidator = new Validator(this);
mValidator.setValidationListener(this);
mSubmitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
validator.validate();
}
});
}
@Override
public void onValidationSucceeded() {
// Success...
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
// Failure...
}
}
您还需要 2 个 - 一个用于验证码,另一个用于密码。如果您希望与您的 activity 通信,请传递它或实施适当的侦听器接口。
如果可能,请考虑简化表单。