Skip to content

Commit e3fde91

Browse files
committed
Simple example of form validation.
(TODO fix the RadioGroup)
1 parent 4c8b63d commit e3fde91

File tree

12 files changed

+476
-30
lines changed

12 files changed

+476
-30
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<category android:name="android.intent.category.LAUNCHER"/>
1818
</intent-filter>
1919
</activity>
20+
21+
<activity android:name=".FormActivity"/>
2022
</application>
2123

2224
</manifest>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ext.sample;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
6+
/**
7+
* Created by evelina on 20/09/14.
8+
*/
9+
public class FormActivity extends Activity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
getFragmentManager().beginTransaction()
15+
.replace(android.R.id.content, new SimpleFormFragment())
16+
.commit();
17+
}
18+
}

app/src/main/java/ext/sample/HomeActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package ext.sample;
22

33
import android.app.Activity;
4+
import android.content.Intent;
45
import android.os.Bundle;
56
import android.view.Menu;
67
import android.view.MenuItem;
78

9+
import butterknife.ButterKnife;
10+
import butterknife.OnClick;
11+
812

913
public class HomeActivity extends Activity {
1014

1115
@Override
1216
protected void onCreate(Bundle savedInstanceState) {
1317
super.onCreate(savedInstanceState);
1418
setContentView(R.layout.activity_home);
19+
ButterKnife.inject(this);
1520
}
1621

1722

@@ -33,4 +38,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
3338
}
3439
return super.onOptionsItemSelected(item);
3540
}
41+
42+
@OnClick(R.id.button)
43+
void openForm() {
44+
startActivity(new Intent(this, FormActivity.class));
45+
}
3646
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package ext.sample;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.util.Log;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.EditText;
10+
import android.widget.RadioGroup;
11+
12+
import org.joda.time.DateTime;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
import butterknife.ButterKnife;
18+
import butterknife.OnClick;
19+
import ext.extensions.forms.Condition;
20+
import ext.extensions.forms.DateInput;
21+
import ext.extensions.forms.DateMultiInput;
22+
import ext.extensions.forms.Form;
23+
import ext.extensions.forms.Forms.FormBuilder;
24+
import ext.extensions.forms.Mapping;
25+
import ext.extensions.forms.StringInput;
26+
import ext.extensions.forms.ValidationException;
27+
import ext.sample.SimpleFormFragment.User.Gender;
28+
29+
30+
/**
31+
* Created by evelina on 20/09/14.
32+
*/
33+
public class SimpleFormFragment extends Fragment {
34+
35+
private Form<User> mForm;
36+
37+
@Override
38+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
39+
View view = inflater.inflate(R.layout.fragment_simple_form, container, false);
40+
ButterKnife.inject(this, view);
41+
return view;
42+
}
43+
44+
@Override
45+
public void onViewCreated(View view, Bundle savedInstanceState) {
46+
super.onViewCreated(view, savedInstanceState);
47+
48+
FormBuilder builder = new FormBuilder();
49+
builder.addViewAndInput((EditText) view.findViewById(R.id.username), new StringInput("username", 6, 32));
50+
builder.addView("birthday_day", (EditText) view.findViewById(R.id.birthday_day));
51+
builder.addView("birthday_month", (EditText) view.findViewById(R.id.birthday_month));
52+
builder.addView("birthday_year", (EditText) view.findViewById(R.id.birthday_year));
53+
builder.addInput(new DateMultiInput("birthday").verifying(new Condition<DateTime>() {
54+
@Override
55+
public boolean verify(DateTime value) {
56+
// at least 18
57+
return value.isBefore(DateTime.now().minusYears(18));
58+
}
59+
}, R.string.birthday_error));
60+
builder.addViewAndInput((RadioGroup) view.findViewById(R.id.gender),
61+
new StringInput("gender").map(new Mapping<String, Gender>() {
62+
@Override
63+
public Gender bind(String value) throws ValidationException {
64+
try {
65+
return Gender.valueOf(value.toUpperCase());
66+
} catch (IllegalArgumentException e) {
67+
throw new ValidationException("gender", R.string.gender_error);
68+
}
69+
}
70+
71+
@Override
72+
public String unbind(Gender value) {
73+
return value.name();
74+
}
75+
}));
76+
77+
mForm = builder.build(new Mapping<Map<String, Object>, User>() {
78+
@Override
79+
public User bind(Map<String, Object> value) throws ValidationException {
80+
return new User((String) value.get("username"), (DateTime) value.get("birthday"), (Gender) value.get("gender"));
81+
}
82+
83+
@Override
84+
public Map<String, Object> unbind(final User value) {
85+
return new HashMap<String, Object>() {{
86+
put("username", value.username);
87+
put("birthday", value.birthday);
88+
put("gender", value.gender);
89+
}};
90+
}
91+
});
92+
93+
94+
}
95+
96+
@OnClick(R.id.submit)
97+
void submit() {
98+
try {
99+
User user = mForm.bind();
100+
Log.i("FORMS", "Validation passed: " + user);
101+
} catch (ValidationException e) {
102+
Log.i("FORMS", "Validation failed: " + e);
103+
}
104+
}
105+
106+
public static class User {
107+
108+
public static enum Gender {
109+
MALE, FEMALE
110+
}
111+
112+
113+
public final String username;
114+
public final DateTime birthday;
115+
public final Gender gender;
116+
117+
public User(String username, DateTime birthday, Gender gender) {
118+
this.username = username;
119+
this.birthday = birthday;
120+
this.gender = gender;
121+
}
122+
123+
@Override
124+
public String toString() {
125+
return "User{" +
126+
"username='" + username + '\'' +
127+
", birthday=" + birthday +
128+
", gender=" + gender +
129+
'}';
130+
}
131+
}
132+
}

app/src/main/res/layout/activity_home.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
android:text="Normal text"/>
2525

2626
<ext.Button
27+
android:id="@+id/button"
2728
android:layout_width="wrap_content"
2829
android:layout_height="wrap_content"
2930
android:layout_marginTop="16dp"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:orientation="vertical"
5+
android:padding="20dp">
6+
7+
<ext.EditText
8+
android:id="@+id/username"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:hint="Username"/>
12+
13+
<RadioGroup
14+
android:id="@+id/gender"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:orientation="horizontal"
18+
android:layout_marginTop="20dp">
19+
20+
<RadioButton
21+
android:id="@+id/male"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:text="Male"/>
25+
26+
<RadioButton
27+
android:id="@+id/female"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="Female"/>
31+
</RadioGroup>
32+
33+
<ext.TextView
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:layout_marginTop="20dp"
37+
android:text="Birthday"/>
38+
39+
<LinearLayout
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:orientation="horizontal"
43+
android:layout_marginTop="20dp">
44+
45+
<ext.EditText
46+
android:id="@+id/birthday_day"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:minEms="2"
50+
android:gravity="center"
51+
android:hint="dd"/>
52+
53+
<ext.EditText
54+
android:id="@+id/birthday_month"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_marginLeft="8dp"
58+
android:minEms="2"
59+
android:gravity="center"
60+
android:hint="mm"/>
61+
62+
<ext.EditText
63+
android:id="@+id/birthday_year"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_marginLeft="8dp"
67+
android:minEms="4"
68+
android:gravity="center"
69+
android:hint="yyyy"/>
70+
71+
</LinearLayout>
72+
73+
<ext.Button
74+
android:id="@+id/submit"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:layout_marginTop="20dp"
78+
android:text="Submit"/>
79+
80+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
<string name="hello_world">Hello world!</string>
66
<string name="action_settings">Settings</string>
77

8+
<string name="birthday_error">You need to be at least 18.</string>
9+
<string name="gender_error">Expecting \'male\' or \'female\'.</string>
10+
811
</resources>

0 commit comments

Comments
 (0)