Welcome to Android Forms documentation!¶
This library makes it easy to deal with views, especially if dealing with a large amount of views.
It provide a consistent and fluent way of setting, validating, retrieving and saving values from views.
With this library you not have to change or alter you layout in anyway, but you will be able to work with multiple forms on multiple fragments via FormCollection.
The library also comes an inbuilt Data populator to use when developing. This comes in handy when you want to prepopulate you form with sample data.
For validation the Validation is used.
install¶
using Maven.
<dependency>
<groupId>com.eddmash</groupId>
<artifactId>android-form</artifactId>
<version>1.0.7</version>
<type>pom</type>
</dependency>
Using gradle
compile 'com.eddmash:android-form:1.0.7'
Usage¶
To use this library is very easy
- Create
Form
class
private class BasicForm extends Form {
public BasicForm() {
super();
}
public BasicForm(ValidatorInterface validator) {
super(validator);
}
@Override
public void save() throws FormException {
// implement the saving logic, you have access to
// getValues() returns a map of where key is the name of the field and the values
}
}
- Create form object
BasicForm form = new BasicForm();
- Add
Field
toFormInterface
// get the views from the layout
Spinner genderSpinner = (Spinner) view.findViewById(R.id.gender);
EditText fName = (EditText) view.findViewById(R.id.firstname);
EditText phone_number = (EditText) view.findViewById(R.id.phone_number);
// add views to the form
form.addField("gender", genderSpinner);
form.addField("firstname", fName);
form.addField("phone_number", fName);
// add validation check
form.addCheck(new NotEmptyCheck(gender,"Gender cannot be blank"));
form.addCheck(new NotEmptyCheck(fName,"Firstname cannot be blank"));
- Run
validation
and get thevalues
.
if(form.isValid()){
form.getValues()// returns a map of where key is the name of the field and the values
}else{
LinearLayout errorSpace = (LinearLayout) findViewById(R.id.error_base);
errorSpace.removeAllViews();// clear space first
ErrorRenderer errorRenderer = new ErrorRenderer(this, form.getValidator());
errorRenderer.render(errorSpace);
}
if(form.isValid()){
try{
form.save() // save
} catch (FormException e) {
e.printStackTrace();
}
}else{
LinearLayout errorSpace = (LinearLayout) findViewById(R.id.error_base);
errorSpace.removeAllViews();// clear space first
ErrorRenderer errorRenderer = new ErrorRenderer(this, form.getValidator());
errorRenderer.render(errorSpace);
}