Android: Document form validation

This commit is contained in:
jendib 2014-12-11 22:29:04 +01:00
parent 17e5c65d04
commit dd59172a19
4 changed files with 90 additions and 30 deletions

View File

@ -19,6 +19,8 @@ import com.sismics.docs.event.DocumentAddEvent;
import com.sismics.docs.event.DocumentEditEvent; import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.listener.JsonHttpResponseHandler;
import com.sismics.docs.resource.DocumentResource; import com.sismics.docs.resource.DocumentResource;
import com.sismics.docs.ui.form.Validator;
import com.sismics.docs.ui.form.validator.Required;
import com.sismics.docs.ui.view.DatePickerView; import com.sismics.docs.ui.view.DatePickerView;
import com.sismics.docs.ui.view.TagsCompleteTextView; import com.sismics.docs.ui.view.TagsCompleteTextView;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
@ -45,7 +47,12 @@ public class DocumentEditActivity extends ActionBarActivity {
/** /**
* Document edited. * Document edited.
*/ */
JSONObject document; private JSONObject document;
/**
* Form validator.
*/
private Validator validator;
// View cache // View cache
private EditText titleEditText; private EditText titleEditText;
@ -105,7 +112,10 @@ public class DocumentEditActivity extends ActionBarActivity {
tagsEditText.allowDuplicates(false); tagsEditText.allowDuplicates(false);
tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList)); tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList));
// TODO Form validation // Validation
validator = new Validator(this, true);
validator.addValidable(titleEditText, new Required());
// Fill the activity // Fill the activity
if (document == null) { if (document == null) {
datePickerView.setDate(new Date()); datePickerView.setDate(new Date());
@ -133,6 +143,11 @@ public class DocumentEditActivity extends ActionBarActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.save: case R.id.save:
validator.validate();
if (!validator.isValidated()) {
return true;
}
// Metadata // Metadata
final String title = titleEditText.getText().toString(); final String title = titleEditText.getText().toString();
final String description = descriptionEditText.getText().toString(); final String description = descriptionEditText.getText().toString();

View File

@ -62,10 +62,10 @@ public class LoginActivity extends ActionBarActivity {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
// Form validation // Form validation
final Validator validator = new Validator(false); final Validator validator = new Validator(this, false);
validator.addValidable(this, txtServer, new Required()); validator.addValidable(txtServer, new Required());
validator.addValidable(this, txtUsername, new Required()); validator.addValidable(txtUsername, new Required());
validator.addValidable(this, txtPassword, new Required()); validator.addValidable(txtPassword, new Required());
validator.setOnValidationChanged(new CallbackListener() { validator.setOnValidationChanged(new CallbackListener() {
@Override @Override
public void onComplete() { public void onComplete() {

View File

@ -2,14 +2,23 @@ package com.sismics.docs.ui.form;
import android.view.View; import android.view.View;
import com.sismics.docs.ui.form.validator.ValidatorType;
public class Validable { public class Validable {
private final ValidatorType[] validatorTypes;
private View view; private View view;
private boolean isValidated = false; private boolean isValidated = false;
public Validable(ValidatorType... validatorTypes) {
this.validatorTypes = validatorTypes;
}
/** /**
* Getter of view. * Getter of view.
*
* @return view * @return view
*/ */
public View getView() { public View getView() {
@ -18,6 +27,7 @@ public class Validable {
/** /**
* Setter of view. * Setter of view.
*
* @param view view * @param view view
*/ */
public void setView(View view) { public void setView(View view) {
@ -26,6 +36,7 @@ public class Validable {
/** /**
* Getter of isValidated. * Getter of isValidated.
*
* @return isValidated * @return isValidated
*/ */
public boolean isValidated() { public boolean isValidated() {
@ -34,9 +45,19 @@ public class Validable {
/** /**
* Setter of isValidated. * Setter of isValidated.
*
* @param isValidated isValidated * @param isValidated isValidated
*/ */
public void setValidated(boolean isValidated) { public void setValidated(boolean isValidated) {
this.isValidated = isValidated; this.isValidated = isValidated;
} }
/**
* Getter of validatorTypes.
*
* @return validatorTypes
*/
public ValidatorType[] getValidatorTypes() {
return validatorTypes;
}
} }

View File

@ -22,7 +22,7 @@ public class Validator {
/** /**
* List of validable elements. * List of validable elements.
*/ */
private Map<View, Validable> validables = new HashMap<View, Validable>(); private Map<EditText, Validable> validables = new HashMap<EditText, Validable>();
/** /**
* Callback when the validation of one element has changed. * Callback when the validation of one element has changed.
@ -33,13 +33,19 @@ public class Validator {
* True if the validator show validation errors. * True if the validator show validation errors.
*/ */
private boolean showErrors; private boolean showErrors;
/**
* Context.
*/
private Context context;
/** /**
* Constructor. * Constructor.
* *
* @param showErrors True to display validation errors * @param showErrors True to display validation errors
*/ */
public Validator(boolean showErrors) { public Validator(Context context, boolean showErrors) {
this.context = context;
this.showErrors = showErrors; this.showErrors = showErrors;
} }
@ -58,12 +64,11 @@ public class Validator {
* @param editText Edit text * @param editText Edit text
* @param validatorTypes Validators * @param validatorTypes Validators
*/ */
public void addValidable(final Context context, final EditText editText, final ValidatorType...validatorTypes) { public void addValidable(final EditText editText, final ValidatorType... validatorTypes) {
final Validable validable = new Validable(); final Validable validable = new Validable(validatorTypes);
validables.put(editText, validable); validables.put(editText, validable);
editText.addTextChangedListener(new TextWatcher() { editText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
// NOP // NOP
@ -76,24 +81,7 @@ public class Validator {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
validable.setValidated(true); validate(editText, validable);
for (ValidatorType validatorType : validatorTypes) {
if (!validatorType.validate(s.toString())) {
if (showErrors) {
editText.setError(validatorType.getErrorMessage(context));
}
validable.setValidated(false);
break;
}
}
if (validable.isValidated()) {
editText.setError(null);
}
if (onValidationChanged != null) {
onValidationChanged.onComplete();
}
} }
}); });
} }
@ -107,7 +95,43 @@ public class Validator {
public boolean isValidated(View view) { public boolean isValidated(View view) {
return validables.get(view).isValidated(); return validables.get(view).isValidated();
} }
/**
* Validate a specific EditText.
*
* @param editText EditText
* @param validable Validable
*/
private void validate(EditText editText, Validable validable) {
validable.setValidated(true);
for (ValidatorType validatorType : validable.getValidatorTypes()) {
if (!validatorType.validate(editText.getEditableText().toString())) {
if (showErrors) {
editText.setError(validatorType.getErrorMessage(context));
}
validable.setValidated(false);
break;
}
}
if (validable.isValidated()) {
editText.setError(null);
}
if (onValidationChanged != null) {
onValidationChanged.onComplete();
}
}
/**
* Validate everything now.
*/
public void validate() {
for (Map.Entry<EditText, Validable> entry : validables.entrySet()) {
validate(entry.getKey(), entry.getValue());
}
}
/** /**
* Returns true if all elements are validated. * Returns true if all elements are validated.
* *