Closed valZho closed 9 years ago
I find myself having to use a custom hook to circumvent FormIt's built-in required
validator allowing only whitespace entries as valid.
Here's an example of a custom hook to achieve this: confirmValid hook:
$value = preg_replace( '/\s+/', '', $value ); // Strip whitespace
$success = strlen( $value ) > 0; // Does something valid exist?
if( !$success ) {
// Add an error to the field
$validator->addError( $key, 'Please enter a valid value.' ); // Optionally substitute with formit.field_required lexicon here
}
return $success;
I agree with you, the built-in required validator should probably be accounting for this already.
I can't think of a use-case on a web-form where a spaces-only entry would count as not empty (note: just because I can't think of it doesn't mean it doesn't exist).
I suggest performing a trim() on the value before testing for empty() to bounce back spaces-only entries: