Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Form Store

Events & Formsmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Build a small form controller. Implement `FormStore`: - `new FormStore(initialValues)`. - `setField(name, value)` updates a field. - `validate(requiredFields)` sets and returns an errors object mapping each empty required field to `'Required'`. - `reset(values)` replaces the values and clears errors. - `getValues()` returns the current values.

Examples

Input: init {name:'',email:''}; setField('name','Ada'); validate(['name','email'])
Output: values {name:'Ada',email:''}, errors {email:'Required'}
Only the empty email is flagged required.

Sample tests

Input: set a field, then validate
Output: [{"name":"Ada","email":""},{"email":"Required"}]
Input: all empty
Output: {"a":"Required"}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Treating 0 or false as missing when some fields may legitimately hold them.

Learning resources

  • React: Reacting to Input with State
Approach & explanation (try first)

A small controller holds values and errors, the logic behind a controlled form with validation.

Loading...
⌘/Ctrl + Enter

Run your code to see results.