# Form Enhancements - Quick Reference Guide

## For Developers

### Adding Enhancements to New Forms

#### 1. Include CSS and JS in Template
```html
{% load static %}
<link rel="stylesheet" href="{% static 'css/form-enhancements.css' %}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

<!-- At the end of template -->
<script src="{% static 'js/form-enhancements.js' %}"></script>
```

#### 2. Add Error Messages to Form Fields
```python
# In forms.py
field_name = forms.CharField(
    required=True,
    error_messages={
        'required': 'This field is required',
        'max_length': 'Value is too long',
        'invalid': 'Please enter a valid value'
    }
)
```

#### 3. Display Errors in Template
```html
<div class="{% if form.field_name.errors %}field-error{% endif %}">
    <label>Field Label *</label>
    {{ form.field_name }}
    {% if form.field_name.errors %}
    <div class="error-message">
        <i class="fas fa-exclamation-circle"></i>
        <span>{{ form.field_name.errors.0 }}</span>
    </div>
    {% endif %}
</div>
```

#### 4. Use Reference Data Dropdowns
```python
# In forms.py
from candidates.models import IndustryType

industry = forms.ModelChoiceField(
    queryset=IndustryType.objects.all(),
    required=False,
    label="Industry",
    widget=forms.Select(attrs={'class': 'form-select'})
)
```

### Custom Validation Methods

```python
def clean_phone_number(self):
    phone = self.cleaned_data.get('phone_number')
    if phone:
        phone = phone.replace(' ', '').replace('-', '')
        if not phone.isdigit():
            raise forms.ValidationError('Phone must contain only digits')
        if len(phone) != 10:
            raise forms.ValidationError('Phone must be 10 digits')
    return phone

def clean_email(self):
    email = self.cleaned_data.get('email')
    if User.objects.filter(email=email).exists():
        raise forms.ValidationError('Email already exists')
    return email
```

## For Admins

### Managing Reference Data

#### Access Reference Data
1. Login to admin panel: `/custom-admin/`
2. Navigate to "Reference Data" section
3. Available options:
   - Qualification Types
   - Degree Courses
   - Industry Types
   - Abroad Job Categories
   - Preferred Job Roles
   - Languages
   - Currencies

#### Adding New Reference Data
1. Click on the reference data type (e.g., "Industry Types")
2. Click "Create New"
3. Enter the name
4. Save
5. Data immediately available in registration forms

#### Editing Reference Data
1. Click on the reference data type
2. Click "Edit" next to the item
3. Update the name
4. Save
5. Changes reflect immediately

#### Deleting Reference Data
⚠️ **Warning**: Deleting reference data may affect existing records
1. Click on the reference data type
2. Click "Delete" next to the item
3. Confirm deletion

### Common Reference Data Examples

**Qualification Types:**
- 10th Standard
- 12th Standard
- Diploma
- Bachelor's Degree
- Master's Degree
- PhD

**Industry Types:**
- Information Technology
- Healthcare
- Education
- Manufacturing
- Retail
- Hospitality
- Construction
- Finance

**Abroad Job Categories:**
- Construction Worker
- Nurse
- Engineer
- Chef
- Driver
- Security Guard
- Domestic Helper

## For Users

### Using the Forms

#### Password Visibility
- Click the eye icon (👁️) next to password field to show password
- Click again to hide password
- Works on all password fields

#### Form Validation
- Required fields marked with red asterisk (*)
- Error messages appear below fields with red background
- Error messages explain exactly what's wrong
- Fix errors and resubmit

#### Dropdown Fields
- Educational Qualification: Select from predefined list
- Industry Type: Select your industry
- Job Categories: Select relevant categories
- All dropdowns populated from admin-managed data

### Common Error Messages

**"This username is already taken"**
- Choose a different username
- Username must be unique

**"An account with this email already exists"**
- Use a different email
- Or login with existing account

**"Phone number must be exactly 10 digits"**
- Enter 10-digit mobile number
- Remove spaces and dashes
- Example: 9876543210

**"You must be at least 18 years old"**
- Registration requires minimum age of 18
- Check date of birth entered

**"Passwords do not match"**
- Ensure both password fields are identical
- Use password visibility toggle to verify

## Troubleshooting

### Password Toggle Not Working
1. Check if Font Awesome is loaded
2. Verify form-enhancements.js is included
3. Check browser console for errors

### Validation Messages Not Showing
1. Verify form-enhancements.css is included
2. Check template has error display code
3. Ensure form has error_messages defined

### Dropdown Not Showing Data
1. Check if reference data exists in admin
2. Verify queryset in form field
3. Check if model is imported correctly

### Styling Issues
1. Clear browser cache
2. Check if CSS file is loaded
3. Verify no CSS conflicts

## Support

For issues or questions:
1. Check FORM_ENHANCEMENTS_SUMMARY.md for detailed documentation
2. Review form field definitions in accounts/forms.py
3. Check template implementation in accounts/templates/
4. Contact development team for assistance
