Skip to content

Commit

Permalink
Field errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
silviogutierrez committed Jul 7, 2018
1 parent 8bf2617 commit 42819cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
20 changes: 16 additions & 4 deletions components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import React from 'react';
import {Widget, WidgetType} from './Widget';

interface Field {
name: string;
label: string;
widget: WidgetType;
}

type FormError = string[];

export type FormType = {
errors: {
[name: string]: FormError;
[name: string]: FormError|null;
};
fields: Field[];
}
Expand All @@ -23,12 +25,22 @@ interface Props {
export const Form = (props: Props) => {
return <form method="POST" action="">
<input type="hidden" name="csrfmiddlewaretoken" value={props.csrf_token} />
{Object.keys(props.form.errors).length > 0 &&
{/*Object.keys(props.form.errors).length > 0 &&
<pre>{JSON.stringify(props.form.errors, null, 2)}</pre>
}
*/}
{props.form.fields.map(field =>
<div key={field.widget.name}>
<Widget widget={field.widget} />
<label>
{field.label}
<Widget widget={field.widget} />
</label>
{props.form.errors[field.name] != null &&
<ul>
{props.form.errors[field.name]!.map((error, index) =>
<li key={index}>{error}</li>
)}
</ul>
}
</div>
)}
<button type="submit">Submit</button>
Expand Down
Binary file modified server/db.sqlite3
Binary file not shown.
8 changes: 7 additions & 1 deletion server/server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ class Meta:

serialized_form = {
'errors': form.errors,
'fields': [simplejson.loads(str(field)) for field in form],
'fields': [
{
**simplejson.loads(str(field)),
'name': field.name,
'label': field.label,
} for field in form
],
}

response = JSXResponse(
Expand Down

0 comments on commit 42819cf

Please sign in to comment.