Constructor
new FileSizeValidatorController()
Examples
Multiple Files
```html
<input type="file"
multiple
data-file-size-validator-target="fileInput"
data-action="change->file-size-validator#validateFiles">
```
Custom Total Limit
```html
<div data-controller="file-size-validator"
data-file-size-validator-max-size-value="26214400"
data-file-size-validator-total-max-size-value="52428800">
```
Methods
buildInvalidFilesMessage(invalidFiles) → {string}
- Description:
Build error message for invalid files
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
invalidFiles |
Array | Array of invalid file objects |
Returns:
Error message with HTML-escaped file names
- Type
- string
buildTotalSizeWarningMessage(totalSize, fileCount) → {string}
- Description:
Build warning message for total size
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
totalSize |
number | Total size in bytes |
fileCount |
number | Number of files |
Returns:
Warning message
- Type
- string
checkFileSizes(files) → {Object}
- Description:
Check file sizes and return validation result
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
files |
Array.<File> | Array of File objects |
Returns:
Validation result
- Type
- Object
clearWarning()
- Description:
Clear warning message
- Source:
connect()
- Description:
Initialize controller
- Source:
disableSubmit()
- Description:
Disable submit button
- Source:
dispatch(eventName, options)
- Description:
Dispatch custom event
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
eventName |
string | Event name (without prefix) |
options |
Object | Event options |
enableSubmit()
- Description:
Enable submit button
- Source:
escapeHtml(str) → {string}
- Description:
Escape HTML special characters to prevent XSS
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
str |
string | String to escape |
Returns:
Escaped string
- Type
- string
formatBytes(bytes, decimals) → {string}
- Description:
Format bytes to human-readable string
- Source:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
bytes |
number | Size in bytes |
|
decimals |
number |
2
|
Number of decimal places |
Returns:
Formatted size string
- Type
- string
formatWarningMessage(message, type) → {string}
- Description:
Format warning message with icon
- Source:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
message |
string | Warning message |
|
type |
string |
warning
|
Message type ('error' or 'warning') |
Returns:
Formatted HTML
- Type
- string
showInvalidFilesWarning(validation)
- Description:
Show warning for invalid files
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
validation |
Object | Validation result |
showTotalSizeWarning(validation)
- Description:
Show warning for total size
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
validation |
Object | Validation result |
validateFiles(event)
- Description:
Validate selected files
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
event |
Event | File input change event |