# Form Validation Fix - January 27, 2025


**Last Updated:** 2025-11-20

## Critical Bug Fixed

**Date**: 2025-11-20  
**Issue**: All centralized forms failing with "Internal server error"  
**Root Cause**: Incorrect parameter order in `validateLength()` function call

## Bug Details

### Location

`v2/helpers/request-validator.php` line 445

### Function Signature

```php
function validateLength($value, $fieldName, $result, $minLength = null, $maxLength = null)
```

### Incorrect Call (Before Fix)

```php
validateLength($value, $fieldName, $minLength, $maxLength, $result)
```

### Correct Call (After Fix)

```php
validateLength($value, $fieldName, $result, $minLength, $maxLength)
```

### Impact

- `$result` (ValidationResult object) was passed as 5th parameter instead of 3rd
- `$minLength` was passed as `$result`, causing fatal error when `addError()` was called
- All forms using centralized validator failed with "Internal server error"

## Affected Form Endpoints

All endpoints using `validateRequest()` from `request-validator.php`:

1. `/v2/api/shiftops-hubspot.php` - ShiftOps report email unlock form
2. `/v2/api/addon-request.php` - Add-on/Enterprise pricing inquiry form
3. `/v2/api/submit-template.php` - Template download form
4. `/v2/api/collect-lead.php` - Tools page lead collection
5. `/v2/api/lead-capture.php` - Lead capture popup (Step 1 & Step 2)
6. `/v2/api/webinar-registration.php` - Webinar registration
7. `/v2/api/payroll-webinar-registration.php` - Payroll webinar registration
8. `/v2/api/shiftops-nps.php` - ShiftOps NPS form
9. `/v2/api/generate_excel.php` - Excel generation
10. `/v2/api/export-workdays.php` - Workdays export

## Fix Applied

Fixed parameter order in `validateLength()` call to match function signature.

## Verification

- ✅ All validation functions verified to use correct parameter order
- ✅ All form endpoints verified to use validator correctly
- ✅ Error handling verified in frontend (shows German error messages)
- ✅ No linting errors introduced

## Prevention

### Best Practices for Future Development

1. **Always verify function signatures match call sites**

   - Use IDE autocomplete to verify parameter order
   - Check function definition before calling

2. **Test validation with sample data**

   - Test with valid data
   - Test with invalid data (validation errors)
   - Test with missing required fields

3. **Use consistent parameter order**

   - All validation functions follow pattern: `($value, $fieldName, $result, ...)`
   - Keep `$result` as 3rd parameter for consistency

4. **Error Handling**
   - Backend returns structured error responses
   - Frontend handles errors and shows user-friendly German messages
   - Log validation failures for debugging

## Testing Checklist

When adding new form endpoints or modifying validation:

- [ ] Verify function signatures match call sites
- [ ] Test with valid data
- [ ] Test with invalid data (validation errors)
- [ ] Test with missing required fields
- [ ] Verify error messages are user-friendly
- [ ] Check browser console for JavaScript errors
- [ ] Verify HubSpot submissions succeed (if applicable)
- [ ] Check logs for proper error logging

## Related Files

- `v2/helpers/request-validator.php` - Centralized validation helper
- `v2/api/*.php` - All form endpoints using validator
- `v2/js/*.js` - Frontend form handling
