fix: trim all whitespace when splitting &validate fields#312
Open
Ibochkarev wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A multi-line
&validatechunk like:silently skips validation on the last field.
validateFields()split the chunk on,and cleaned each piece withtrim(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 invalidateFields(), confirming the trailing\nsurvives the old trim and is removed bytrim($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 currentvalidateFields()logic.Fixes #276