61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import re
|
|
import sys
|
|
|
|
def remove_required(filepath):
|
|
with open(filepath, 'r') as f:
|
|
content = f.read()
|
|
|
|
# Fields that should remain required
|
|
# name, email, password, salary
|
|
# We will remove `required` from Label, Input, and Select for all other fields.
|
|
|
|
# Let's find all occurrences of `required` and carefully remove them unless they are for our core fields.
|
|
lines = content.split('\n')
|
|
out_lines = []
|
|
|
|
current_field = None
|
|
|
|
for line in lines:
|
|
# Detect field context
|
|
match = re.search(r'(?:htmlFor|id)="([^"]+)"', line)
|
|
if match:
|
|
current_field = match.group(1)
|
|
|
|
# Also detect gender which uses RadioGroup
|
|
if 'id="gender-male"' in line:
|
|
current_field = 'gender'
|
|
|
|
if '<Label required>{t(\'Gender\')}</Label>' in line:
|
|
line = line.replace('<Label required>', '<Label>')
|
|
|
|
# If it's a core field, keep it
|
|
if current_field in ['name', 'email', 'password', 'salary']:
|
|
out_lines.append(line)
|
|
continue
|
|
|
|
# For non-core fields, remove `required`
|
|
if '<Label' in line and 'required' in line:
|
|
line = line.replace(' required>', '>')
|
|
line = line.replace(' required ', ' ')
|
|
|
|
if '<Input' in line and 'required' in line:
|
|
line = line.replace(' required', '')
|
|
|
|
if '<Select' in line and 'required' in line:
|
|
line = line.replace(' required', '')
|
|
|
|
# Some `required` might be on their own line
|
|
if line.strip() == 'required':
|
|
# Skip this line completely
|
|
continue
|
|
|
|
out_lines.append(line)
|
|
|
|
with open(filepath, 'w') as f:
|
|
f.write('\n'.join(out_lines))
|
|
|
|
remove_required('resources/js/pages/hr/employees/create.tsx')
|
|
remove_required('resources/js/pages/hr/employees/edit.tsx')
|
|
|
|
print("Successfully removed required attributes from non-core fields.")
|