Add required field validation to RSVP form - Make attendance, guest info, and guest details required
This commit is contained in:
@@ -36,7 +36,7 @@ const RSVPForm: React.FC = () => {
|
|||||||
name: '',
|
name: '',
|
||||||
attending: '',
|
attending: '',
|
||||||
bringing_guests: '',
|
bringing_guests: '',
|
||||||
guest_count: 0,
|
guest_count: 1,
|
||||||
guest_names: '',
|
guest_names: '',
|
||||||
items_bringing: []
|
items_bringing: []
|
||||||
});
|
});
|
||||||
@@ -146,6 +146,25 @@ const RSVPForm: React.FC = () => {
|
|||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
|
// Validate required fields
|
||||||
|
if (!formData.name.trim() || !formData.attending) {
|
||||||
|
setError('Please fill in all required fields');
|
||||||
|
setIsSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formData.attending === 'yes' && !formData.bringing_guests) {
|
||||||
|
setError('Please indicate if you are bringing guests');
|
||||||
|
setIsSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formData.bringing_guests === 'yes' && (formData.guest_count < 1 || !formData.guest_names.trim())) {
|
||||||
|
setError('Please provide the number and names of your guests');
|
||||||
|
setIsSubmitting(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const submissionData = {
|
const submissionData = {
|
||||||
...formData,
|
...formData,
|
||||||
@@ -320,7 +339,7 @@ const RSVPForm: React.FC = () => {
|
|||||||
variant="outlined"
|
variant="outlined"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormControl fullWidth>
|
<FormControl fullWidth required>
|
||||||
<InputLabel>Are you attending?</InputLabel>
|
<InputLabel>Are you attending?</InputLabel>
|
||||||
<Select
|
<Select
|
||||||
name="attending"
|
name="attending"
|
||||||
@@ -329,6 +348,7 @@ const RSVPForm: React.FC = () => {
|
|||||||
label="Are you attending?"
|
label="Are you attending?"
|
||||||
required
|
required
|
||||||
>
|
>
|
||||||
|
<MenuItem value="">Select an option</MenuItem>
|
||||||
<MenuItem value="yes">Yes</MenuItem>
|
<MenuItem value="yes">Yes</MenuItem>
|
||||||
<MenuItem value="no">No</MenuItem>
|
<MenuItem value="no">No</MenuItem>
|
||||||
</Select>
|
</Select>
|
||||||
@@ -336,14 +356,16 @@ const RSVPForm: React.FC = () => {
|
|||||||
|
|
||||||
{formData.attending === 'yes' && (
|
{formData.attending === 'yes' && (
|
||||||
<>
|
<>
|
||||||
<FormControl fullWidth>
|
<FormControl fullWidth required>
|
||||||
<InputLabel>Are you bringing any guests?</InputLabel>
|
<InputLabel>Are you bringing any guests?</InputLabel>
|
||||||
<Select
|
<Select
|
||||||
name="bringing_guests"
|
name="bringing_guests"
|
||||||
value={formData.bringing_guests}
|
value={formData.bringing_guests}
|
||||||
onChange={handleSelectChange}
|
onChange={handleSelectChange}
|
||||||
label="Are you bringing any guests?"
|
label="Are you bringing any guests?"
|
||||||
|
required
|
||||||
>
|
>
|
||||||
|
<MenuItem value="">Select an option</MenuItem>
|
||||||
<MenuItem value="yes">Yes</MenuItem>
|
<MenuItem value="yes">Yes</MenuItem>
|
||||||
<MenuItem value="no">No</MenuItem>
|
<MenuItem value="no">No</MenuItem>
|
||||||
</Select>
|
</Select>
|
||||||
@@ -359,7 +381,10 @@ const RSVPForm: React.FC = () => {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
fullWidth
|
fullWidth
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
|
required
|
||||||
inputProps={{ min: 1 }}
|
inputProps={{ min: 1 }}
|
||||||
|
error={formData.guest_count < 1}
|
||||||
|
helperText={formData.guest_count < 1 ? "Number of guests must be at least 1" : ""}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
@@ -371,7 +396,10 @@ const RSVPForm: React.FC = () => {
|
|||||||
rows={3}
|
rows={3}
|
||||||
fullWidth
|
fullWidth
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
|
required
|
||||||
placeholder="Please list the names of your guests"
|
placeholder="Please list the names of your guests"
|
||||||
|
error={!formData.guest_names.trim()}
|
||||||
|
helperText={!formData.guest_names.trim() ? "Please enter the names of your guests" : ""}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@@ -410,7 +438,11 @@ const RSVPForm: React.FC = () => {
|
|||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
size="large"
|
size="large"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting ||
|
||||||
|
!formData.name.trim() ||
|
||||||
|
!formData.attending ||
|
||||||
|
(formData.attending === 'yes' && !formData.bringing_guests) ||
|
||||||
|
(formData.bringing_guests === 'yes' && (formData.guest_count < 1 || !formData.guest_names.trim()))}
|
||||||
sx={{ mt: 2 }}
|
sx={{ mt: 2 }}
|
||||||
>
|
>
|
||||||
{isSubmitting ? 'Submitting...' : 'Submit RSVP'}
|
{isSubmitting ? 'Submitting...' : 'Submit RSVP'}
|
||||||
|
|||||||
Reference in New Issue
Block a user