Skip to content

fix: trim all whitespace when splitting &validate fields#312

Open
Ibochkarev wants to merge 1 commit into
Sterc:3.xfrom
Ibochkarev:fix/validate-param-newline-trim
Open

fix: trim all whitespace when splitting &validate fields#312
Ibochkarev wants to merge 1 commit into
Sterc:3.xfrom
Ibochkarev:fix/validate-param-newline-trim

Conversation

@Ibochkarev

@Ibochkarev Ibochkarev commented Jul 23, 2026

Copy link
Copy Markdown

A multi-line &validate chunk like:

&validate=`
name:required,
email:email,
phone:required,
message:required
`

silently skips validation on the last field. validateFields() split the chunk on , and cleaned each piece with trim(ltrim($v), ' '), which only strips leading whitespace and trailing spaces. The last piece keeps its trailing newline (from the line break before the closing backtick), so the parsed validator name becomes "required\n" instead of "required". method_exists() can't match that name, so the field falls through to the "validator not specified" branch and is treated as valid.

Reproduced with a plain PHP snippet against the explode()/trim() logic in validateFields(), confirming the trailing \n survives the old trim and is removed by trim($v).

A comment on the issue mentions a workaround (trailing comma after the last field) that used to add "an extra empty field" to validation in an older FormIt version. That side effect doesn't reproduce here: the !empty($key[0]) check right after the split already discards the empty piece a trailing comma produces, with or without this fix. Verified both the reported case and the trailing-comma workaround against the current validateFields() logic.

Fixes #276

trim(ltrim($v), ' ') only strips leading whitespace and trailing
spaces, so a validator definition ending with a newline (e.g. the
last line of a multi-line &validate chunk) keeps that newline as
part of its validator name. method_exists() then fails to match the
validator, and validation for that field is silently skipped.

Use trim($v) to strip whitespace, including newlines, from both ends.

Fixes Sterc#276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect field splitting in &validate parameter

1 participant