Как удалить поле ограничения из сообщения об ошибке - PullRequest
0 голосов
/ 30 апреля 2019

Я использую validate.js для проверки формы в моих собственных приложениях.Все отлично работает, как я следовал из этих постов: https://medium.com/@pavsidhu/validating-forms-in-react-native-7adc625c49cf и Ошибка при использовании validate.js в React Native - Неизвестный минимум валидатора

коды работают и выдают правильное сообщение об ошибке, но 1 проблема: он дает имя ограниченного поля с сообщением об ошибке в виде строки.

Profile picture Please upload image!

это выдает ошибку с ограничивающим полем ("Изображение профиля") и сообщением об ошибке(«Пожалуйста, загрузите изображение!») В строку.

мои коды:

import validate from 'validate.js';

// Constrain example
const constraints = {
emailAddress: {
    presence: {
        message: "Cannot be blank."
    },
    format: {
        pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
        message: 'Invalid email id',
    },
    email: true
},
password: {
    presence: {
        message: "Cannot be blank."
    },
    length: {
        minimum: 6,
        message: 'Your password must be at least 6 characters'
    }
},
profilePicture: {
    presence: {
        message: "Please upload image!"
    }
},
}

// Function
const formValidator = (field, value) => {
// Creates an object based on the field name and field value
// e.g. let object = {email: 'email@example.com'}
let object = {}
object[field] = value

let constraint = constraints[field]

// Validate against the constraint and hold the error messages
const result = validate(object, { [field]: constraint });

// If there is an error message, return it!
if (result) {
    // Return only the field error message if there are multiple
    //console.log(result);
    return result[field][0]
}

return null
}

Использование на странице формы:

let errorprofilePicture = Validator("profilePicture", profilePicture);

this.setState({
  errorProfilePicture: errorprofilePicture
}

как мне заставить его плюнутьтолько сообщение об ошибке?что-то вроде Please upload image!.

спасибо!

...