# Walidacja wejścia przez validate() komponentu

> Zaimplementuj validate() w komponencie, czytaj stany invalid, touched i modified i znajduj nieprawidłowe pola przez ERRORS().

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

Komponent edytujący wartość może ją walidować. Zdefiniuj `self.validate(value)` i zwróć `true` (lub pusty napis), gdy wartość jest prawidłowa, albo `false` (lub komunikat), gdy nie jest. Zapisz wartość z powrotem przez `self.modify(value)`: oznacza też komponent jako **modified** i **touched**.

jComponent trzyma te stany w `config` (`invalid`, `touched`, `modified`) i jako klasy CSS (`ui-invalid`, `ui-touched`, `ui-modified`), więc stylowanie nie wymaga kodu. `ERRORS(path, callback)` zwraca nieprawidłowe komponenty pod ścieżką, czego formularz potrzebuje przed wysłaniem, a `RESET(path)` czyści stany touched i modified.

```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>
```
