I have 3 forms in a same activity (and layout) that I want validate using saripaar. This validates the three forms, but they dont validate independently.
This the layout:
- Form1: Input for email and submit button.
- Form2: Input for a validation code and submit button.
- Form3: Input for a password, passwordconfirm and submit button.
I want that when you press submit for form1, validate only the rules form the form1, and same to form2 and form3.
I have 3 validators objects and the logic of each of them is similar. Ex:
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();
}
});
This my full activity code:
/* 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();
}
});