{"id":393267,"date":"2024-06-29T10:54:37","date_gmt":"2024-06-29T10:54:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=393267"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=393267","title":{"rendered":"<span>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0432 Android<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041e \u0447\u0451\u043c \u044d\u0442\u043e?<\/h3>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f &#8212; \u043e\u0431\u0437\u043e\u0440 \u043c\u043e\u0435\u0439 <a href=\"https:\/\/github.com\/Egor-Blagochinnov\/Validation\" rel=\"noopener noreferrer nofollow\"><strong>Android-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438<\/strong><\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0443\u044e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u0430 \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/86d\/f8f\/23e\/86df8f23e53a304db63c02b9f601d7c7.png\" width=\"674\" height=\"306\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/86d\/f8f\/23e\/86df8f23e53a304db63c02b9f601d7c7.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0430\u0441\u0442\u043e \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u044d\u043a\u0440\u0430\u043d\u044b \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u041d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0443\u043c\u043e\u043c \u0438 \u0441\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e &#8212; \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0442\u0430\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438: \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 RegExp \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b &#8212; \u043f\u0440\u0438\u0446\u0435\u043f\u0438\u0442\u044c \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u043d\u0430 <code>EditText<\/code> <\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val pattern = \"[a-z]+\".toRegex()  editText.addTextChangedListener { \u00a0 \u00a0   val text = it?.toString() ?: return@addTextChangedListener \u00a0   if (text.matches(pattern)) { \u00a0 \u00a0 \u00a0 \u00a0       hideError(editText) \u00a0       } else { \u00a0 \u00a0 \u00a0 \u00a0       showError(editText, \"Only a-z symbols allowed\") \u00a0       } }<\/code><\/pre>\n<p>\u041d\u043e \u0430 \u0435\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u043b\u0435 \u043f\u043e \u0434\u0432\u0443\u043c \u0440\u0430\u0437\u043d\u044b\u043c RegExp \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438?<\/p>\n<p>\u041d\u0443 \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c:<\/p>\n<pre><code>editText.addTextChangedListener {     val text = it?.toString() ?: return@addTextChangedListener     if (text.length &lt;= 10) {         hideError(editText)     } else {         showError(editText, \"Only 10 symbols allowed\")     } }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0441\u043b\u0438 \u0432 \u043f\u043e\u043b\u0435 \u0432\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u0440\u043e\u043a\u0443 &#171;12345&#187; \u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u043f\u043e\u043b\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0446\u0438\u0444\u0440\u044b, \u0430 \u0432\u043e\u0442 \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u0441\u043a\u0440\u043e\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e \u0435\u0433\u043e \u043c\u043d\u0435\u043d\u0438\u044e &#8212; \u043f\u043e\u043b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435, \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 10 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>\u0418 \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0414\u0430\u043b\u044c\u0448\u0435 &#8212; \u0432\u0435\u0441\u0435\u043b\u0435\u0435:<\/p>\n<ul>\n<li>\n<p>\u041a\u0430\u043a \u043f\u043e\u0432\u0435\u0441\u0438\u0442\u044c \u043d\u0430 \u043f\u043e\u043b\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u0430\u0432\u0438\u043b \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0435?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0430?<\/p>\n<\/li>\n<li>\n<p>\u0413\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u0443\u0447\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435?<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435&#8230;<\/p>\n<\/li>\n<\/ul>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u043e ViewModel \u0438\u043b\u0438 Presenter, \u043d\u043e \u043d\u0435 \u0432 UI \u0441\u043b\u043e\u0435. \u0417\u0430\u0434\u0430\u0447\u0430 UI &#8212; \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 view-\u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043a \u043b\u044e\u0431\u043e\u0439 view<\/p>\n<p>\u0412\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0447\u0442\u043e\u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u0436\u0434\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0432\u044b\u0434\u0430\u0432\u0430\u043b\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0432\u0441\u0451 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/413\/b4f\/3f1\/413b4f3f1e53ee1d9889a5e6299ec271.jpg\" width=\"572\" height=\"666\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/413\/b4f\/3f1\/413b4f3f1e53ee1d9889a5e6299ec271.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0451 \u044d\u0442\u043e \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0432<a href=\"https:\/\/github.com\/Egor-Blagochinnov\/Validation\" rel=\"noopener noreferrer nofollow\"> \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435<\/a>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0451 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<h3>Condition &#8212; \u043e\u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0435\u0433\u043e<\/h3>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u0441\u0435\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 (<code>Condition<\/code>) &#8212; \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u043e\u0434\u043d\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>validate<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1d5\/52f\/117\/1d552f117a3b5f12605afee16ce05f91.png\" alt=\"interface Condition\" title=\"interface Condition\" width=\"2166\" height=\"732\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1d5\/52f\/117\/1d552f117a3b5f12605afee16ce05f91.png\"\/><figcaption>interface Condition<\/figcaption><\/figure>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h4>\n<p>\u041d\u0430 \u0438\u0437\u0438! \u0423 <code>Condition<\/code> \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>validate(data)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u0435\u0440\u043d\u0451\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <code>ValidationResult<\/code>. \u0412\u043d\u0443\u0442\u0440\u0438 <code>ValidationResult<\/code> \u0431\u0443\u0434\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 <code>isValid<\/code> \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0435\u0441\u043b\u0438 <code>isValid == false<\/code><\/p>\n<h4>\u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 <\/h4>\n<p><code>Condition<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u0442\u044c. \u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0418\u041b\u0418, \u0430 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0418<\/p>\n<p>\u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0435 (\u0418\u041b\u0418)<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p><strong>\u0418\u041b\u0418<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0423\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 (\u0418)<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p><strong>\u0418<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0441\u043a\u043b\u044b\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u043b\u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u044e\u0442\u0441\u044f <code>ValidationResult<\/code><\/p>\n<h3>Validator &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/h3>\n<p>\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u0439?<\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h4>\n<p><code>Validator<\/code> \u043f\u043e-\u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>Condition<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u043a\u0430\u0447\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>Validator<\/code>  \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0436\u0443\u0441\u0442\u0432\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0439 <code>Set&lt;Condition><\/code>. \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 <code>Set&lt;ValidationResult><\/code>. \u0417\u0430\u0442\u0435\u043c, \u044d\u0442\u043e\u0442 \u043d\u0430\u0431\u043e\u0440 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 (<code>Operator<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0412\u043e\u0442, \u0412\u0441\u0451.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/526\/489\/04b\/52648904b0e49de497b81dd8cf1a1eca.png\" alt=\"Validator\" title=\"Validator\" width=\"2114\" height=\"514\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/526\/489\/04b\/52648904b0e49de497b81dd8cf1a1eca.png\"\/><figcaption>Validator<\/figcaption><\/figure>\n<p>\u0423 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u043a\u043e\u043b\u044b:<\/p>\n<h4>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<\/h4>\n<p><code>Operator<\/code> &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e <code>Condition&lt;Collection&lt;ValidationResult>><\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u0442\u0443\u043f\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430 \u0431\u0443\u043b\u0435\u0432\u043e\u0439 \u0430\u043b\u0433\u0435\u0431\u0440\u044b. \u041f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440-\u043a\u043e\u043d\u044a\u044e\u043d\u043a\u0446\u0438\u044f.<\/p>\n<p>\u041d\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c <code>ValidationResult(true)<\/code> \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u043e \u043f\u043e\u0440\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"kotlin\">class ThresholdOperator(val validThreshold: Int) : Validator.Operator {     override fun validate(value: Collection&lt;ValidationResult>?): ValidationResult {         val validCount = value?.count { it.isValid } ?: 0          return if (validCount >= validThreshold) {             ValidationResult.valid()         } else {             ValidationResult.invalid(\"Less than $validThreshold valid conditions\")         }     } }<\/code><\/pre>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<pre><code class=\"kotlin\">validator.setOperator(ThresholdOperator(validator.getConditionsSet().size \/ 2))<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440! <code>Validator<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c view.<\/p>\n<pre><code class=\"kotlin\">validator.addOperatorChangedListener {     \/\/ on operator changed }  \/\/\u0418\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044f validator.removeOperatorChangedListener(operatorListener)<\/code><\/pre>\n<h4>\u041d\u0430\u0431\u043e\u0440 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/h4>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">validator.addCondition(Condition { string ->     ValidationResult.obtain(string?.contains(\"target\") == true, \"String must contains target\") })<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">validator.removeCondition(condition)<\/code><\/pre>\n<p>\u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0439 &#8212; \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044f <code>OnConditionsChangedListener<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<pre><code class=\"kotlin\">validator.addConditionsChangedListener { newConditions ->     \/\/on new conditions }<\/code><\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>changeConditionsSet<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c <code>OnConditionsChangedListener<\/code> \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 &#8212; \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439 \u043d\u0430\u0431\u043e\u0440\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439.<\/p>\n<pre><code class=\"kotlin\">validator.changeConditionsSet {     this.add(Conditions.RequiredField())     this.remove(condition2)     this.add(Conditions.NotNull()) }<\/code><\/pre>\n<h3>LiveDataValidator &#8212; \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/h3>\n<p>\u0411\u044b\u043b\u043e \u0431\u044b \u0443\u0434\u043e\u0431\u043d\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438. \u0422\u0430\u043a \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c! \u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u043e\u0434\u043d\u043e \u043c\u043e\u043b\u043e\u0434\u0435\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>LiveData<\/code>. \u0422\u0430\u043a \u043f\u0443\u0441\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043f\u043e\u0434\u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0451 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/95a\/c33\/ced\/95ac33ced74b129767075d891651c25b.png\" alt=\"LiveDataValidator\" title=\"LiveDataValidator\" width=\"2242\" height=\"450\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/95a\/c33\/ced\/95ac33ced74b129767075d891651c25b.png\"\/><figcaption>LiveDataValidator<\/figcaption><\/figure>\n<p><code>LiveDataValidator<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>Vlidator<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438:<\/p>\n<h4>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state)<\/h4>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044d\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 <code>LiveData&lt;ValidationResult><\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c. <code>LiveDataValidator<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u0432 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043f\u043e\u043a\u0430 \u043e\u043d \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a (<code>Validator.observe<\/code>; <code>Validator.observeForever<\/code>)<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.state<\/code><\/pre>\n<h4>\u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f LiveDataValidator<\/h4>\n<p>LiveDataValidator \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0442\u044c \u043a\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u043d\u0435\u0433\u043e<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.state.observe(viewLifecycleOwner) { validationResult ->     \/\/apply validation result }  \/\/\u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a, \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0435\u0442 liveDataValidator.observe(viewLifecycleOwner) { validationResult ->     \/\/apply validation result }<\/code><\/pre>\n<h4>\u0420\u0435\u0430\u043a\u0446\u0438\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 LiveData<\/h4>\n<p><code>LiveDataValidator<\/code> \u0443\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 LiveData \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>watchOn<\/code><\/p>\n<pre><code class=\"kotlin\">liveDataValidator.watchOn(textMaxLength) { newTextMaxLength ->     liveDataValidator.validate() }<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 <code>liveDataValidator<\/code> \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430 \u043f\u043e\u043b\u0435\u043c <code>textMaxLength<\/code> \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435  <code>textMaxLength<\/code> \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f <code>liveDataValidator<\/code> \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>triggerOn<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437 \u043a\u043e\u0433\u0434\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.triggerOn(textMaxLength)<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440<\/p>\n<p>\u0415\u0441\u0442\u044c 2 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044f: \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043f\u0438\u043a\u0438 \u0442\u043e\u0447\u0435\u043d\u044b, \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c <s>\u0445** \u0434<\/s><strong><s>*<\/s><\/strong><s>\u044b<\/s> \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043f\u0438\u043a\u0438 \u0417\u0430\u0434\u0430\u0447\u0430, \u0447\u0442\u043e\u0431\u044b \u0432\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b\u043e \u0432 \u0441\u0435\u0431\u0435 \u0442\u0435\u043a\u0441\u0442 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/p>\n<pre><code class=\"kotlin\">val first: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() \/\/\u041f\u0435\u0440\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 (\u0441 \u043f\u0438\u043a\u0430\u043c\u0438) val second: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() \/\/\u0412\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 (\u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0438\u043a\u0430\u043c\u0438)  val secondValidator: LiveDataValidator&lt;String?> = LiveDataValidator(second).apply {     addCondition(Conditions.RequiredField())     addCondition(Conditions.RegEx(\"[a-z]+\".toRegex(), \"only a-z symbols allowed\"))     addCondition(Conditions.TextMaxLength(10))      addCondition {       \/\/ \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441\u044e\u0434\u0430: \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0435 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 first!         if (it?.contains(first.value ?: \"\") == true) {             ValidationResult.invalid(\"textField2 should not contains textField1\")         } else {             ValidationResult.valid()         }     }      triggerOn(first) \/\/\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 first \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434 validate() }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, <code>secondValidator<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u043b\u0435 <code>second<\/code>, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u0437\u0443\u0435\u0442 <code>first<\/code> \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 <code>first<\/code> \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f? \u0422\u043e\u0433\u0434\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0432\u0438\u0441\u0435\u0442\u044c \u0432 \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f <code>second<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 <code>first<\/code> \u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>triggerOn(LiveData&lt;*>)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 <code>first<\/code><\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e <code>triggerOn<\/code> \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>watchOn<\/code> \u0438 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<pre><code class=\"kotlin\">watchOn(textField1) {     validate() }<\/code><\/pre>\n<h3>MuxLiveDataValidator &#8212; \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b<\/h3>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0443\u0447\u0430 \u043f\u043e\u043b\u0435\u0439 \u0441 LiveDataValidator&#8217;\u0430\u043c\u0438 \u043d\u0430\u0434\u043e \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u043f\u0440\u0435\u0434\u0438\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0421\u0430\u043c\u044b\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u043e\u043b\u044f \u043d\u0430 \u0444\u043e\u0440\u043c\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e &#8212; \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043a\u043d\u043e\u043f\u043a\u0443 &#171;\u0414\u0430\u043b\u0435\u0435&#187;.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c <code>MuxLiveDataValidator<\/code>.  \u041e\u043d \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e <code>LiveDataValidator'\u043e\u0432<\/code> \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f &#8212; <code>MuxLiveDataValidator<\/code> \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (<code>ValidationResult<\/code>) \u0432\u0441\u0435\u0445 LiveDataValidator&#8217;\u043e\u0432 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0438\u0445 \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 (<code>Operator<\/code>). Operator \u0432\u044b\u0434\u0430\u0451\u0442 \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p>\u041a\u043e\u0440\u043e\u0447\u0435, <code>MuxLiveDataValidator<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0438\u043f\u0430 \u043a\u0430\u043a \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u043e\u0440. \u041e\u0442\u0441\u044e\u0434\u0430 \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6a7\/220\/717\/6a722071718cc2ed286eeaf8b7b070bb.png\" alt=\"MuxLiveDataValidator\" title=\"MuxLiveDataValidator\" width=\"1688\" height=\"520\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6a7\/220\/717\/6a722071718cc2ed286eeaf8b7b070bb.png\"\/><figcaption>MuxLiveDataValidator<\/figcaption><\/figure>\n<h4>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state)<\/h4>\n<p>\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <code>LiveDataValidator<\/code> \u0443 <code>MuxLiveDataValidator<\/code>  \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/p>\n<pre><code class=\"kotlin\">muxValidator.state<\/code><\/pre>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044d\u0442\u043e <code>LiveData&lt;ValidationResult><\/code> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<h4>\u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f MuxLiveDataValidator<\/h4>\n<p>\u0422\u0443\u0442 \u043a\u0430\u043a \u0443 <code>LiveDataValidator<\/code> &#8212; \u0434\u043e\u0441\u0442\u0443\u043f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0435<\/p>\n<pre><code class=\"kotlin\">viewModel.muxValidator.observe(viewLifecycleOwner) { validationResult ->     \/\/ apply validatioin result }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u043d\u0430 <code>MuxLiveDataValidator<\/code>, \u0442\u043e \u0432\u0441\u0435 \u0435\u0433\u043e <code>LiveDataValidator<\/code> \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u043d\u0430 \u043d\u0438\u0445 (\u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u0430\u043d\u0430\u043b\u043e\u0433 \u0441\u0435\u043c\u0435\u0439\u043d\u043e\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0443 <code>MediatorLiveData<\/code> ). \u0422\u043e \u0435\u0441\u0442\u044c \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043b\u0438\u0441\u044c \u043d\u0430 <code>MuxLiveDataValidator<\/code>, \u0442\u043e \u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0442\u0435 <code>LiveDataValidator<\/code>, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u043d \u0441\u043b\u0435\u0434\u0438\u0442.<\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430<\/h4>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>LiveDataValidator<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 <code>MuxLiveDataValidator<\/code><\/p>\n<pre><code class=\"kotlin\">val muxValidator = MuxLiveDataValidator(     textField1Validator,     textField2Validator )<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">muxValidator.addValidator(textField3Validator)  \/\/\u041c\u043e\u0436\u0435\u0448\u044c \u0434\u043e\u043a\u0438\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e muxValidator.addValidators(   listOf(     textField4Validator,      textField5Validator   ) )<\/code><\/pre>\n<h4>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430<\/h4>\n<p>\u041d\u0443 \u0442\u0443\u0442 \u0442\u0438\u043f\u0430 \u0432\u0430\u0449\u0435 \u0432\u0441\u0451 \u0438\u0437\u044f\u043d<\/p>\n<pre><code class=\"kotlin\">muxValidator.removeValidator(textField3Validator)<\/code><\/pre>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/h4>\n<p>\u041f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>MuxLiveDataValidator<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440-\u043a\u043e\u043d\u044a\u044e\u043d\u043a\u0446\u0438\u044e. \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u044b\u0434\u0430\u0447\u0438 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e <code>ValidationResult<\/code> \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<\/p>\n<pre><code class=\"kotlin\">muxValidator.setOperator(Validator.Operator.Disjunction())<\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u043c\u0435\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0447\u0442\u043e\u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 view.<\/p>\n<pre><code class=\"kotlin\">muxValidator.addOperatorChangedListener {     \/\/ on operator changed }  \/\/\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a  muxValidator.removeOperatorChangedListener(listener)<\/code><\/pre>\n<h3>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432 \u043a view<\/h3>\n<h4>ConditionViewBinder<\/h4>\n<p><code>ConditionViewBinder<\/code> \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u0435\u043b\u044c <code>view<\/code> \u0438 <code>Condition<\/code><\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a:<\/p>\n<p>\u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 <code>ConditionViewBinder.validate()<\/code> \u0434\u043e\u0441\u0442\u0430\u0435\u0442 \u0438\u0437 view \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>getValidationData()<\/code>. \u042d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0443\u043b\u0435\u0442\u0430\u044e\u0442 \u0432 <code>Condition<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442 \u0438\u0445 \u0438 \u0432\u0435\u0440\u043d\u0435\u0442 <code>ValidationResult<\/code>. \u0417\u0430\u0442\u0435\u043c \u044d\u0442\u043e\u0442 <code>ValidationResult<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443 <code>onValidationResult()<\/code> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f view.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4d4\/2db\/5c0\/4d42db5c06745e977cec5e519c30d247.png\" alt=\"ConditionViewBinder\" title=\"ConditionViewBinder\" width=\"1472\" height=\"656\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4d4\/2db\/5c0\/4d42db5c06745e977cec5e519c30d247.png\"\/><figcaption>ConditionViewBinder<\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440<\/p>\n<pre><code class=\"kotlin\">val editText1 = requireView().findViewById&lt;EditText>(R.id.edit_text1) val condition = Conditions.TextMaxLength&lt;String?>(10)  val conditionBinder = object : ConditionViewBinder&lt;TextView, String?>(WeakReference(editText1), condition) {     override fun getValidationData(view: TextView?): String? {         return view?.text?.toString()     }      override fun onValidationResult(view: TextView?, result: ValidationResult?) {         if (result?.isValid == true) {             view?.error = null         } else {             view?.error = result?.errorMessage         }     } }  conditionBinder.check()<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043a \u043b\u044e\u0431\u043e\u0439 view<\/p>\n<h4>ValidatorViewBinder<\/h4>\n<p>\u041f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>Validator<\/code>: \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430.<\/p>\n<h4>LiveDataValidatorViewBinder<\/h4>\n<p><code>LiveDataValidator<\/code> &#8212; \u043e\u0441\u043e\u0431\u044b\u0439 \u043f\u0430\u0446\u0438\u0435\u043d\u0442. \u0414\u043b\u044f \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0439 binder, \u043a\u043e\u0442\u043e\u0440\u044b\u0439:<\/p>\n<ul>\n<li>\n<p>\u0441\u0430\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f\/\u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 <code>LiveDataValidator<\/code> ( \u0447\u0442\u043e\u0431\u044b \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e)<\/p>\n<\/li>\n<li>\n<p><code>getValidationData()<\/code>  \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043d\u0435 \u0438\u0437 view, \u0430 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 (\u0438\u0437 \u0435\u0433\u043e <code>source<\/code>)<\/p>\n<\/li>\n<\/ul>\n<p>\u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/p>\n<p><code>LiveDataValidatorViewBinder<\/code> \u043d\u0443\u0436\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0422\u0443\u0442 2 \u0441\u043f\u043e\u0441\u043e\u0431\u0430:<\/p>\n<ul>\n<li>\n<p>\u0427\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440. \u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>LifeycleOwner<\/code><\/p>\n<pre><code class=\"kotlin\">object : LiveDataValidatorViewBinder&lt;TextView, String?>(   viewLifecycleOwner,    WeakReference(binding.editText1),    viewModel.textField1Validator) {         override fun onValidationResult(view: TextView?, result: ValidationResult?) {          }          override fun onConditionsChanged(conditions: Set&lt;Condition&lt;String?>>) {          }          override fun onOperatorChanged() {          } }<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>attach<\/code><\/p>\n<pre><code class=\"kotlin\">liveDataValidatorViewBinder.attach(viewLifecycleOwner)<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>\u0413\u043e\u0442\u043e\u0432\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h4>\n<p><strong>TextConditionViewBinder<\/strong><\/p>\n<p>\u0421\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 <code>Condition<\/code> \u0441 <code>TextView<\/code>. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u043b\u0435 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u043d\u0451\u043c<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val editText1 = requireView().findViewById&lt;EditText>(R.id.edit_text1) val condition = Conditions.TextMaxLength&lt;CharSequence?>(10)  editText.validateBy(condition)<\/code><\/pre>\n<p><strong>TextViewLiveDataValidatorBinder<\/strong><\/p>\n<p>\u0422\u0443\u0442 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>TextConditionViewBinder<\/code>, \u043d\u043e \u0442\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 <code>LiveDataValidator<\/code>.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val editText = requireView().findViewById&lt;EditText>(R.id.edit_text1) val liveDataValidator = LiveDataValidator(viewModel.textField1, Conditions.RequiredField())  editText.validateBy(viewLifecycleOwner, liveDataValidator)<\/code><\/pre>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b<\/h3>\n<h4>\u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/h4>\n<ol start=\"0\">\n<li>\n<p>\u0412\u043e ViewModel \u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 <code>Condition<\/code><\/p>\n<pre><code class=\"kotlin\">val textFieldCondition = Conditions.RegEx&lt;CharSequence?>(     \"[a-z]+\".toRegex(),      \"only a-z symbols allowed\" )<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0412\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 (\u0438\u043b\u0438 \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438) \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043a \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c\u0443 \u043f\u043e\u043b\u044e<\/p>\n<pre><code class=\"kotlin\">val editText = requireView().findViewById&lt;EditText>(R.id.edit_text1) editText.validateBy(viewModel.textFieldCondition)<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e<\/p>\n<\/li>\n<\/ol>\n<h4>\u0421\u043b\u043e\u0436\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/h4>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 3 \u043f\u043e\u043b\u044f: \u043f\u043e\u043b\u0435 \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0446\u0438\u0444\u0440, \u043f\u043e\u043b\u0435 \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0431\u0443\u043a\u0432 \u0438 \u043f\u043e\u043b\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u043f\u043e\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0446\u0438\u0444\u0440. \u041e \u043a\u0430\u043a! \u0410 \u0435\u0449\u0451 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0439 \u0444\u043e\u0440\u043c\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435!<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0442\u0443\u0442: <a href=\"https:\/\/github.com\/Egor-Blagochinnov\/ValidationSample\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/Egor-Blagochinnov\/ValidationSample<\/a><\/p>\n<ol start=\"0\">\n<li>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u0441\u0430\u043c\u0438 \u043f\u043e\u043b\u044f \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b \u043a \u043d\u0438\u043c \u0432\u043e ViewModel]<\/p>\n<pre><code class=\"kotlin\">\/\/\u041f\u043e\u043b\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u043f\u043e\u043b\u044f \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0446\u0438\u0444\u0440 val textMaxLength: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>()  \/\/\u0426\u0438\u0444\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 val textField1: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() \/\/\u041f\u0440\u043e \u044d\u0442\u043e\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 - \u0447\u0443\u0442\u044c \u043d\u0438\u0436\u0435 val textField1Validator = ExampleValidators.DigitsFieldValidator(textField1).apply {     watchOn(textMaxLength) {         val maxLength = kotlin.runCatching { it?.toInt() }.getOrNull()         this.setMaxLength(maxLength)     } }  \/\/\u0411\u0443\u043a\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 val textField2: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() val textField2Validator: LiveDataValidator&lt;String?> = LiveDataValidator(textField2).apply {     addCondition(Conditions.RequiredField())     addCondition(Conditions.RegEx(\"[a-z]+\".toRegex(), \"only a-z symbols allowed\"))     addCondition(Conditions.TextMaxLength(10)) }  \/\/\u041e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 val muxValidator = MuxLiveDataValidator(     textField1Validator,     textField2Validator )<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0427\u0442\u043e\u0431\u044b \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u043d\u044f\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 &#8212; \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u043b\u044f \u0441\u043c\u0435\u043d\u044b \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043d\u0443\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u044d\u0442\u0438 \u0441\u0430\u043c\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f, \u0430 \u044d\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435<\/p>\n<pre><code class=\"kotlin\">class ExampleValidators {     class DigitsFieldValidator&lt;S : CharSequence?>(         source: LiveData&lt;S>,         initialCondition: Condition&lt;S?>? = null,         operator: Operator = Operator.Conjunction()     ) : LiveDataValidator&lt;S>(         source,         initialCondition,         operator     ) {         val onlyDigitsCondition = Conditions.RegEx&lt;S>(\"[0-9]+\".toRegex(), \"only digits allowed\")         private var maxLengthCondition = Conditions.TextMaxLength&lt;S?>(5) \/\/\u043f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u0443\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 5          init {             addCondition(onlyDigitsCondition)             addCondition(maxLengthCondition)         }          fun setMaxLength(maxLength: Int?) {             if (maxLength == null || maxLength &lt; 0) {                 removeCondition(maxLengthCondition)                 return             }              val newCondition = Conditions.TextMaxLength&lt;S?>(maxLength)              changeConditionsSet {                 remove(maxLengthCondition)                 maxLengthCondition = newCondition                 add(maxLengthCondition)             }         }     } }<\/code><\/pre>\n<\/li>\n<li>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0434\u0451\u043c \u0432\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0441\u0451 \u044d\u0442\u043e \u0434\u0435\u043b\u043e<\/p>\n<pre><code class=\"kotlin\">override fun onViewCreated(view: View, savedInstanceState: Bundle?) {         super.onViewCreated(view, savedInstanceState)          viewModel = ViewModelProvider(this).get(ExampleViewModel1::class.java)         binding.viewModel = viewModel            \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043a \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u043c\u0443 \u043f\u043e\u043b\u044e         binding.editText1.validateBy(             viewLifecycleOwner,              viewModel.textField1Validator         )    \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043a \u0431\u0443\u043a\u0432\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e         binding.editText2.validateBy(             viewLifecycleOwner,             viewModel.textField2Validator         )    \/\/\u0421\u043b\u0435\u0434\u0438\u043c \u0437\u0430 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u043c (mux) \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u043c         viewModel.muxValidator.observe(viewLifecycleOwner) {             setGeneralValidationResult(it)         }     }      \/\/\u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u0431\u0449\u0435\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u043e\u043b\u0435 \"state\"     private fun setGeneralValidationResult(validationResult: ValidationResult) {         if (validationResult.isValid) {             binding.state.text = \"Correct!\"             binding.state.setTextColor(ContextCompat.getColor(requireContext(), R.color.state_success))         } else {             binding.state.text = validationResult.errorMessage ?: \"Error message is null\"             binding.state.setTextColor(ContextCompat.getColor(requireContext(), R.color.design_default_color_error))         }     }<\/code><\/pre>\n<p>\u0413\u043e\u0442\u043e\u0432\u043e!<\/p>\n<\/li>\n<\/ol>\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/822\/f5e\/ad1\/822f5ead1f2ee1cb91e7e0826c42a014.png\" width=\"694\" height=\"1392\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/822\/f5e\/ad1\/822f5ead1f2ee1cb91e7e0826c42a014.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043f\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e<\/h4>\n<ol start=\"0\">\n<li>\n<p>\u0412\u0441\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432\u043e ViewModel (\u043d\u0443 \u0438\u043b\u0438 \u0432 Presenter) \u041d\u0435 \u043d\u0430\u0434\u043e \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b, \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0430 view \u0443\u0440\u043e\u0432\u0435\u043d\u044c. <\/p>\n<\/li>\n<li>\n<p>\u041f\u043e-\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>LiveDataValidator<\/code>. \u041e\u043d \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u043a\u0430\u0447\u0435\u043d\u043d\u044b\u0439. \u0418 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0440\u0430\u0434\u0438 \u043d\u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0431\u044b\u043b\u0430<\/p>\n<\/li>\n<li>\n<p>\u0410\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0435\u0435 \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0439.  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u043f\u043e\u043b\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0430\u0449\u0438\u0435 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0447\u0442\u043e!<\/p>\n<\/li>\n<li>\n<p>\u0414\u0435\u043b\u0430\u0439\u0442\u0435 \u0441\u0432\u043e\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0432\u043e\u0438 <code>ConditionViewBinder<\/code>\u044b, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u043c\u0438 view \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0432\u043e\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/579164\/\"> https:\/\/habr.com\/ru\/articles\/579164\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041e \u0447\u0451\u043c \u044d\u0442\u043e?<\/h3>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f &#8212; \u043e\u0431\u0437\u043e\u0440 \u043c\u043e\u0435\u0439 <a href=\"https:\/\/github.com\/Egor-Blagochinnov\/Validation\" rel=\"noopener noreferrer nofollow\"><strong>Android-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438<\/strong><\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0443\u044e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u0430 \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0430\u0441\u0442\u043e \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u044d\u043a\u0440\u0430\u043d\u044b \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u041d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0443\u043c\u043e\u043c \u0438 \u0441\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e &#8212; \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0442\u0430\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438: \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 RegExp \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b &#8212; \u043f\u0440\u0438\u0446\u0435\u043f\u0438\u0442\u044c \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u043d\u0430 <code>EditText<\/code> <\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">val pattern = \"[a-z]+\".toRegex()  editText.addTextChangedListener { \u00a0 \u00a0   val text = it?.toString() ?: return@addTextChangedListener \u00a0   if (text.matches(pattern)) { \u00a0 \u00a0 \u00a0 \u00a0       hideError(editText) \u00a0       } else { \u00a0 \u00a0 \u00a0 \u00a0       showError(editText, \"Only a-z symbols allowed\") \u00a0       } }<\/code><\/pre>\n<p>\u041d\u043e \u0430 \u0435\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u043b\u0435 \u043f\u043e \u0434\u0432\u0443\u043c \u0440\u0430\u0437\u043d\u044b\u043c RegExp \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438?<\/p>\n<p>\u041d\u0443 \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c:<\/p>\n<pre><code>editText.addTextChangedListener {     val text = it?.toString() ?: return@addTextChangedListener     if (text.length &lt;= 10) {         hideError(editText)     } else {         showError(editText, \"Only 10 symbols allowed\")     } }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0441\u043b\u0438 \u0432 \u043f\u043e\u043b\u0435 \u0432\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u0440\u043e\u043a\u0443 &#171;12345&#187; \u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u043f\u043e\u043b\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0446\u0438\u0444\u0440\u044b, \u0430 \u0432\u043e\u0442 \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u0441\u043a\u0440\u043e\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e \u0435\u0433\u043e \u043c\u043d\u0435\u043d\u0438\u044e &#8212; \u043f\u043e\u043b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435, \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 10 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>\u0418 \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0414\u0430\u043b\u044c\u0448\u0435 &#8212; \u0432\u0435\u0441\u0435\u043b\u0435\u0435:<\/p>\n<ul>\n<li>\n<p>\u041a\u0430\u043a \u043f\u043e\u0432\u0435\u0441\u0438\u0442\u044c \u043d\u0430 \u043f\u043e\u043b\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u0430\u0432\u0438\u043b \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0435?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0430?<\/p>\n<\/li>\n<li>\n<p>\u0413\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u0443\u0447\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u043e\u0432?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435?<\/p>\n<\/li>\n<li>\n<p>\u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435&#8230;<\/p>\n<\/li>\n<\/ul>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u043e ViewModel \u0438\u043b\u0438 Presenter, \u043d\u043e \u043d\u0435 \u0432 UI \u0441\u043b\u043e\u0435. \u0417\u0430\u0434\u0430\u0447\u0430 UI &#8212; \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 view-\u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u043a \u043b\u044e\u0431\u043e\u0439 view<\/p>\n<p>\u0412\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0447\u0442\u043e\u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u0436\u0434\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0432\u044b\u0434\u0430\u0432\u0430\u043b\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>\u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0432\u0441\u0451 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0451 \u044d\u0442\u043e \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0432<a href=\"https:\/\/github.com\/Egor-Blagochinnov\/Validation\" rel=\"noopener noreferrer nofollow\"> \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435<\/a>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0451 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<h3>Condition &#8212; \u043e\u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0435\u0433\u043e<\/h3>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u0441\u0435\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 (<code>Condition<\/code>) &#8212; \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u043e\u0434\u043d\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>validate<\/code>.<\/p>\n<figure class=\"full-width\"><figcaption>interface Condition<\/figcaption><\/figure>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h4>\n<p>\u041d\u0430 \u0438\u0437\u0438! \u0423 <code>Condition<\/code> \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>validate(data)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u0435\u0440\u043d\u0451\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <code>ValidationResult<\/code>. \u0412\u043d\u0443\u0442\u0440\u0438 <code>ValidationResult<\/code> \u0431\u0443\u0434\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 <code>isValid<\/code> \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0435\u0441\u043b\u0438 <code>isValid == false<\/code><\/p>\n<h4>\u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 <\/h4>\n<p><code>Condition<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u0442\u044c. \u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0418\u041b\u0418, \u0430 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0418<\/p>\n<p>\u0421\u043b\u043e\u0436\u0435\u043d\u0438\u0435 (\u0418\u041b\u0418)<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p><strong>\u0418\u041b\u0418<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0423\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 (\u0418)<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p><strong>\u0418<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/th>\n<th>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(true)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(true)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><strong>Conditon(false)<\/strong><\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<td>\n<p>Conditon(false)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0441\u043a\u043b\u044b\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u043b\u0438 \u0443\u043c\u043d\u043e\u0436\u0430\u044e\u0442\u0441\u044f <code>ValidationResult<\/code><\/p>\n<h3>Validator &#8212; \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/h3>\n<p>\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u0439?<\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h4>\n<p><code>Validator<\/code> \u043f\u043e-\u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>Condition<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u043a\u0430\u0447\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>Validator<\/code>  \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0436\u0443\u0441\u0442\u0432\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0439 <code>Set&lt;Condition><\/code>. \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 <code>Set&lt;ValidationResult><\/code>. \u0417\u0430\u0442\u0435\u043c, \u044d\u0442\u043e\u0442 \u043d\u0430\u0431\u043e\u0440 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 (<code>Operator<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0412\u043e\u0442, \u0412\u0441\u0451.<\/p>\n<figure class=\"full-width\"><figcaption>Validator<\/figcaption><\/figure>\n<p>\u0423 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u043a\u043e\u043b\u044b:<\/p>\n<h4>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<\/h4>\n<p><code>Operator<\/code> &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e <code>Condition&lt;Collection&lt;ValidationResult>><\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u0442\u0443\u043f\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430 \u0431\u0443\u043b\u0435\u0432\u043e\u0439 \u0430\u043b\u0433\u0435\u0431\u0440\u044b. \u041f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440-\u043a\u043e\u043d\u044a\u044e\u043d\u043a\u0446\u0438\u044f.<\/p>\n<p>\u041d\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c <code>ValidationResult(true)<\/code> \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u043e \u043f\u043e\u0440\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"kotlin\">class ThresholdOperator(val validThreshold: Int) : Validator.Operator {     override fun validate(value: Collection&lt;ValidationResult>?): ValidationResult {         val validCount = value?.count { it.isValid } ?: 0          return if (validCount >= validThreshold) {             ValidationResult.valid()         } else {             ValidationResult.invalid(\"Less than $validThreshold valid conditions\")         }     } }<\/code><\/pre>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<pre><code class=\"kotlin\">validator.setOperator(ThresholdOperator(validator.getConditionsSet().size \/ 2))<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440! <code>Validator<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<p>\u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c view.<\/p>\n<pre><code class=\"kotlin\">validator.addOperatorChangedListener {     \/\/ on operator changed }  \/\/\u0418\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044f validator.removeOperatorChangedListener(operatorListener)<\/code><\/pre>\n<h4>\u041d\u0430\u0431\u043e\u0440 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/h4>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">validator.addCondition(Condition { string ->     ValidationResult.obtain(string?.contains(\"target\") == true, \"String must contains target\") })<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f<\/p>\n<pre><code class=\"kotlin\">validator.removeCondition(condition)<\/code><\/pre>\n<p>\u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0439 &#8212; \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044f <code>OnConditionsChangedListener<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<pre><code class=\"kotlin\">validator.addConditionsChangedListener { newConditions ->     \/\/on new conditions }<\/code><\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0439<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>changeConditionsSet<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c <code>OnConditionsChangedListener<\/code> \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 &#8212; \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439 \u043d\u0430\u0431\u043e\u0440\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439.<\/p>\n<pre><code class=\"kotlin\">validator.changeConditionsSet {     this.add(Conditions.RequiredField())     this.remove(condition2)     this.add(Conditions.NotNull()) }<\/code><\/pre>\n<h3>LiveDataValidator &#8212; \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/h3>\n<p>\u0411\u044b\u043b\u043e \u0431\u044b \u0443\u0434\u043e\u0431\u043d\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438. \u0422\u0430\u043a \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c! \u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u043e\u0434\u043d\u043e \u043c\u043e\u043b\u043e\u0434\u0435\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>LiveData<\/code>. \u0422\u0430\u043a \u043f\u0443\u0441\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043f\u043e\u0434\u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0451 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. <\/p>\n<figure class=\"full-width\"><figcaption>LiveDataValidator<\/figcaption><\/figure>\n<p><code>LiveDataValidator<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>Vlidator<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438:<\/p>\n<h4>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state)<\/h4>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044d\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 <code>LiveData&lt;ValidationResult><\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c. <code>LiveDataValidator<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u0432 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043f\u043e\u043a\u0430 \u043e\u043d \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a (<code>Validator.observe<\/code>; <code>Validator.observeForever<\/code>)<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.state<\/code><\/pre>\n<h4>\u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f LiveDataValidator<\/h4>\n<p>LiveDataValidator \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0445\u043e\u0442\u044c \u043a\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u043d\u0435\u0433\u043e<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.state.observe(viewLifecycleOwner) { validationResult ->     \/\/apply validation result }  \/\/\u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a, \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0435\u0442 liveDataValidator.observe(viewLifecycleOwner) { validationResult ->     \/\/apply validation result }<\/code><\/pre>\n<h4>\u0420\u0435\u0430\u043a\u0446\u0438\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 LiveData<\/h4>\n<p><code>LiveDataValidator<\/code> \u0443\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 LiveData \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>watchOn<\/code><\/p>\n<pre><code class=\"kotlin\">liveDataValidator.watchOn(textMaxLength) { newTextMaxLength ->     liveDataValidator.validate() }<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 <code>liveDataValidator<\/code> \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430 \u043f\u043e\u043b\u0435\u043c <code>textMaxLength<\/code> \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435  <code>textMaxLength<\/code> \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f <code>liveDataValidator<\/code> \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>triggerOn<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437 \u043a\u043e\u0433\u0434\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a<\/p>\n<pre><code class=\"kotlin\">liveDataValidator.triggerOn(textMaxLength)<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440<\/p>\n<p>\u0415\u0441\u0442\u044c 2 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044f: \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043f\u0438\u043a\u0438 \u0442\u043e\u0447\u0435\u043d\u044b, \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c <s>\u0445** \u0434<\/s><strong><s>*<\/s><\/strong><s>\u044b<\/s> \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043f\u0438\u043a\u0438 \u0417\u0430\u0434\u0430\u0447\u0430, \u0447\u0442\u043e\u0431\u044b \u0432\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b\u043e \u0432 \u0441\u0435\u0431\u0435 \u0442\u0435\u043a\u0441\u0442 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/p>\n<pre><code class=\"kotlin\">val first: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() \/\/\u041f\u0435\u0440\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 (\u0441 \u043f\u0438\u043a\u0430\u043c\u0438) val second: MutableLiveData&lt;String?> = MutableLiveData&lt;String?>() \/\/\u0412\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 (\u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0438\u043a\u0430\u043c\u0438)  val secondValidator: LiveDataValidator&lt;String?> = LiveDataValidator(second).apply {     addCondition(Conditions.RequiredField())     addCondition(Conditions.RegEx(\"[a-z]+\".toRegex(), \"only a-z symbols allowed\"))     addCondition(Conditions.TextMaxLength(10))      addCondition {       \/\/ \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441\u044e\u0434\u0430: \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0435 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 first!         if (it?.contains(first.value ?: \"\") == true) {             ValidationResult.invalid(\"textField2 should not contains textField1\")         } else {             ValidationResult.valid()         }     }      triggerOn(first) \/\/\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 first \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434 validate() }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, <code>secondValidator<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u043b\u0435 <code>second<\/code>, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u0437\u0443\u0435\u0442 <code>first<\/code> \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 <code>first<\/code> \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f? \u0422\u043e\u0433\u0434\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0432\u0438\u0441\u0435\u0442\u044c \u0432 \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f <code>second<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 <code>first<\/code> \u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>triggerOn(LiveData&lt;*>)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 <code>first<\/code><\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e <code>triggerOn<\/code> \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>watchOn<\/code> \u0438 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<pre><code class=\"kotlin\">watchOn(textField1) {     validate() }<\/code><\/pre>\n<h3>MuxLiveDataValidator &#8212; \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b<\/h3>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0443\u0447\u0430 \u043f\u043e\u043b\u0435\u0439 \u0441 LiveDataValidator&#8217;\u0430\u043c\u0438 \u043d\u0430\u0434\u043e \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u043f\u0440\u0435\u0434\u0438\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0421\u0430\u043c\u044b\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u043e\u043b\u044f \u043d\u0430 \u0444\u043e\u0440\u043c\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e &#8212; \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043a\u043d\u043e\u043f\u043a\u0443 &#171;\u0414\u0430\u043b\u0435\u0435&#187;.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c <code>MuxLiveDataValidator<\/code>.  \u041e\u043d \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e <code>LiveDataValidator'\u043e\u0432<\/code> \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f &#8212; <code>MuxLiveDataValidator<\/code> \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (<code>ValidationResult<\/code>) \u0432\u0441\u0435\u0445 LiveDataValidator&#8217;\u043e\u0432 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0438\u0445 \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 (<code>Operator<\/code>). Operator \u0432\u044b\u0434\u0430\u0451\u0442 \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p>\u041a\u043e\u0440\u043e\u0447\u0435, <code>MuxLiveDataValidator<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0438\u043f\u0430 \u043a\u0430\u043a \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u043e\u0440. \u041e\u0442\u0441\u044e\u0434\u0430 \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><figcaption>MuxLiveDataValidator<\/figcaption><\/figure>\n<h4>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state)<\/h4>\n<p>\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <code>LiveDataValidator<\/code> \u0443 <code>MuxLiveDataValidator<\/code>  \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/p>\n<pre><code class=\"kotlin\">muxValidator.state<\/code><\/pre>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044d\u0442\u043e <code>LiveData&lt;ValidationResult><\/code> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<h4>\u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f MuxLiveDataValidator<\/h4>\n<p>\u0422\u0443\u0442 \u043a\u0430\u043a \u0443 <code>LiveDataValidator<\/code> &#8212; \u0434\u043e\u0441\u0442\u0443\u043f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0435<\/p>\n<pre><code class=\"kotlin\">viewModel.muxValidator.observe(viewLifecycleOwner) { validationResult ->     \/\/ apply validatioin result }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-393267","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/393267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=393267"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/393267\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=393267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=393267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=393267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}