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

View File

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

View File

@ -2,14 +2,23 @@ package com.sismics.docs.ui.form;
import android.view.View;
import com.sismics.docs.ui.form.validator.ValidatorType;
public class Validable {
private final ValidatorType[] validatorTypes;
private View view;
private boolean isValidated = false;
public Validable(ValidatorType... validatorTypes) {
this.validatorTypes = validatorTypes;
}
/**
* Getter of view.
*
* @return view
*/
public View getView() {
@ -18,6 +27,7 @@ public class Validable {
/**
* Setter of view.
*
* @param view view
*/
public void setView(View view) {
@ -26,6 +36,7 @@ public class Validable {
/**
* Getter of isValidated.
*
* @return isValidated
*/
public boolean isValidated() {
@ -34,9 +45,19 @@ public class Validable {
/**
* Setter of isValidated.
*
* @param isValidated isValidated
*/
public void setValidated(boolean 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.
*/
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.
@ -34,12 +34,18 @@ public class Validator {
*/
private boolean showErrors;
/**
* Context.
*/
private Context context;
/**
* Constructor.
*
* @param showErrors True to display validation errors
*/
public Validator(boolean showErrors) {
public Validator(Context context, boolean showErrors) {
this.context = context;
this.showErrors = showErrors;
}
@ -58,12 +64,11 @@ public class Validator {
* @param editText Edit text
* @param validatorTypes Validators
*/
public void addValidable(final Context context, final EditText editText, final ValidatorType...validatorTypes) {
final Validable validable = new Validable();
public void addValidable(final EditText editText, final ValidatorType... validatorTypes) {
final Validable validable = new Validable(validatorTypes);
validables.put(editText, validable);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// NOP
@ -76,9 +81,31 @@ public class Validator {
@Override
public void afterTextChanged(Editable s) {
validate(editText, validable);
}
});
}
/**
* Returns true if the element is validated.
*
* @param view View
* @return True if the element is validated
*/
public boolean isValidated(View view) {
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 : validatorTypes) {
if (!validatorType.validate(s.toString())) {
for (ValidatorType validatorType : validable.getValidatorTypes()) {
if (!validatorType.validate(editText.getEditableText().toString())) {
if (showErrors) {
editText.setError(validatorType.getErrorMessage(context));
}
@ -95,17 +122,14 @@ public class Validator {
onValidationChanged.onComplete();
}
}
});
}
/**
* Returns true if the element is validated.
*
* @param view View
* @return True if the element is validated
* Validate everything now.
*/
public boolean isValidated(View view) {
return validables.get(view).isValidated();
public void validate() {
for (Map.Entry<EditText, Validable> entry : validables.entrySet()) {
validate(entry.getKey(), entry.getValue());
}
}
/**