# Validate input with a component's validate()

> Implement validate() in a component, read the invalid, touched and modified states, and find invalid fields with ERRORS().

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: en
Content-SHA256: da083e2d978ec1a4472abb4b2d8a9ae7b2b3d28b560d6e54ea3a9114ed7be5f7
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

A component that edits a value can validate it. Define `self.validate(value)` and return `true` (or an empty string) when the value is valid, or `false` (or a message) when it is not. Write the value back with `self.modify(value)`: it also marks the component as **modified** and **touched**.

jComponent keeps these states in `config` (`invalid`, `touched`, `modified`) and as CSS classes (`ui-invalid`, `ui-touched`, `ui-modified`), so styling needs no code. `ERRORS(path, callback)` returns the invalid components below a path, which is what a form needs before it submits, and `RESET(path)` clears the touched and modified states.

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
	<style>.ui-invalid input { border: 2px solid crimson; }</style>

	<ui-component name="required" path="form.name"></ui-component>
	<ui-component name="required" path="form.email"></ui-component>

	<script>

		COMPONENT('required', function(self) {

			self.make = function() {
				self.html('<input type="text" />');
				self.find('input').on('input', function() {
					self.modify(this.value);
				});
			};

			self.validate = function(value) {
				return !!value && value.length > 2;
			};

			self.setter = function(value) {
				self.find('input').val(value || '');
			};
		});

		SET('form.name', 'Peter');
		SET('form.email', '');

	</script>

</body>
</html>
```
