#reactjs
#реагирует на
Вопрос:
Я выполняю операции CRUD с api между React и сущностью C#. Я сделал таблицу национальных номеров уникальной в C#, и при добавлении ее из раздела формы она ничего не делает, когда я ввожу тот же национальный номер. Как я могу заставить его выдать сообщение о том, что этот национальный номер используется с тостом или другим способом?
import React, { useState, useEffect } from "react"; import { Button, Grid, TextField, withStyles } from "@material-ui/core"; import useForm from "./useForm"; import { connect } from "react-redux"; import * as actions from "../actions/sOgrenci"; import { useToasts } from "react-toast-notifications"; import Controls from "../components/controls/Controls"; const styles = theme =gt; ({ root: { 'amp; .MuiTextField-root': { margin: theme.spacing(1), minWidth: 230, } }, smMargin: { margin: theme.spacing(1) } }) const initialFieldvalues = { tcKimlikNo: '', ad: '', soyad: '', email: '', dogumTarihi: null, cepTelefon: '' } const SOgrenciForm = ({ classes, ...props }) =gt; { const { addToast } = useToasts() const validate = (fieldValues = values) =gt; { let temp = {...errors} if ('tcKimlikNo' in fieldValues) temp.tcKimlikNo = fieldValues.tcKimlikNo.length == 11 ? "" : "TC kimlik numarası 11 haneli olmalıdır." if ('ad' in fieldValues) temp.ad = fieldValues.ad ? "" : "Öğrenci adı girilmesi zorunludur." if ('soyad' in fieldValues) temp.soyad = fieldValues.soyad ? "" : "Öğrenci soyadı girilmesi zorunludur." if ('dogumTarihi' in fieldValues) temp.dogumTarihi = fieldValues.dogumTarihi ? "" : "Öğrenci doğum tarihi girilmesi zorunludur." if ('email' in fieldValues) temp.email = (/^$|. @. .. /).test(fieldValues.email) ? "" : "Lütfen doğru bir Email adresi giriniz.." setErrors({ ...temp }) if (fieldValues == values) return Object.values(temp).every(x =gt; x == "") } const { values, setValues, errors, setErrors, handleInputChange, resetForm } = useForm(initialFieldvalues, validate, props.setCurrentId) const handleSubmit = e =gt; { e.preventDefault() if (validate()) { const onSuccess = () =gt; { resetForm() addToast("Ekleme başarılı.",{appearance:'success'}) } if(props.currentId==0) props.createOgrenci(values, onSuccess) else props.updateOgrenci(props.currentId, values, onSuccess ) } } useEffect(() =gt; { if (props.currentId != 0) { setValues({ ...props.sOgrenciList.find(x =gt; x.id == props.currentId) }) setErrors({}) } }, [props.currentId]) const [date, setDate] = useState(new Date()); return ( lt;form autoComplete="off" noValidate className={classes.root} onSubmit={handleSubmit}gt; lt;Grid containergt; lt;Grid item xs={12}gt; lt;TextField name="tcKimlikNo" variant="outlined" label="TC Kimlik No" value={values.tcKimlikNo} onChange={handleInputChange} {...(errors.tcKimlikNo amp;amp; { error: true, helperText: errors.tcKimlikNo })} /gt; lt;/Gridgt; lt;Grid item xs={12}gt; lt;TextField name="ad" variant="outlined" label="Ad" value={values.ad} onChange={handleInputChange} {...(errors.ad amp;amp; { error: true, helperText: errors.ad })} /gt; lt;/Gridgt; lt;Grid item xs={12}gt; lt;TextField name="soyad" variant="outlined" label="Soyad" value={values.soyad} onChange={handleInputChange} {...(errors.soyad amp;amp; { error: true, helperText: errors.soyad })} /gt; lt;/Gridgt; lt;Grid item xs={12}gt; lt;TextField name="email" variant="outlined" label="Email" value={values.email} onChange={handleInputChange} {...(errors.email amp;amp; { error: true, helperText: errors.email })} /gt; lt;/Gridgt; {/* lt;Grid item xs={12}gt; lt;TextField name="dogumTarihi" variant="outlined" label="Doğum Tarihi" value={values.dogumTarihi} onChange={handleInputChange} {...(errors.dogumTarihi amp;amp; { error: true, helperText: errors.dogumTarihi })} /gt; lt;/Gridgt; */} lt;Controls.DatePicker name="dogumTarihi" label="Doğum Tarihi" value={values.dogumTarihi} onChange={handleInputChange} /gt; lt;Grid item xs={12}gt; lt;TextField name="cepTelefon" variant="outlined" label="Cep Telefonu" value={values.cepTelefon} onChange={handleInputChange} {...(errors.cepTelefon amp;amp; { error: true, helperText: errors.cepTelefon })} /gt; lt;/Gridgt; lt;divgt; lt;Button variant="contained" color="primary" type="submit" className={classes.smMargin} gt; Kaydet lt;/Buttongt; lt;Button variant="contained" className={classes.smMargin} onClick={resetForm} gt; Reset lt;/Buttongt; lt;/divgt; lt;/Gridgt; lt;/formgt; ); } const mapStateToProps = state =gt; ({ sOgrenciList: state.rOgrenci.list }) const mapActionToProps = { createOgrenci: actions.create, updateOgrenci: actions.update } export default connect(mapStateToProps, mapActionToProps)(withStyles(styles)(SOgrenciForm));