cncjs

Форк
0
/
validations.jsx 
72 строки · 1.5 Кб
1
/* eslint react/prop-types: 0 */
2
import React from 'react';
3
import i18n from './i18n';
4

5
const Error = (props) => (
6
  <div {...props} style={{ color: '#A94442' }} />
7
);
8

9
const required = (value, props, components) => {
10
  if (props.type === 'radio') {
11
    const name = props.name;
12

13
    components = components[name] || [];
14
    if (components.length === 0) {
15
      return null;
16
    }
17

18
    // Controls the placement of the error message for radio buttons
19
    if (components[components.length - 1] !== props) {
20
      return null;
21
    }
22

23
    const checked = components.reduce((checked, props) => {
24
      return checked || props.checked;
25
    }, false);
26

27
    if (checked) {
28
      return null;
29
    }
30

31
    return (
32
      <Error>{i18n._('This field is required.')}</Error>
33
    );
34
  }
35

36
  if (props.type === 'checkbox') {
37
    if (props.checked) {
38
      return null;
39
    }
40

41
    return (
42
      <Error>{i18n._('This field is required.')}</Error>
43
    );
44
  }
45

46
  value = ('' + value).trim();
47
  if (!value) {
48
    return (
49
      <Error>{i18n._('This field is required.')}</Error>
50
    );
51
  }
52

53
  return null;
54
};
55

56
const password = (value, props, components) => {
57
  const bothBlurred = components.password[0].blurred && components.confirm[0].blurred;
58
  const bothChanged = components.password[0].changed && components.confirm[0].changed;
59

60
  if (bothBlurred && bothChanged && components.password[0].value !== components.confirm[0].value) {
61
    return (
62
      <Error>{i18n._('Passwords should be equal.')}</Error>
63
    );
64
  }
65

66
  return null;
67
};
68

69
export {
70
  required,
71
  password
72
};
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.