laravel/prompts

Real Time Validation

devajmeireles opened this issue ยท 1 comments

Laravel Prompts Version

Latest

Laravel Version

prompt:text:default:required:validated

PHP Version

8.1.21

Operating System & Version

macOS 13.4.1

Terminal Application

iTerm2 3.4.19

Description

From what I could see, prompts have "real-time validation", but it only works after the first attempt to send some value. I don't know if it's bad behavior or if it was expected to happen. In any case, I am reporting to the care of the team. Demo: https://share.cleanshot.com/vddFkrXQLBZSxDjqh3s7

Steps To Reproduce

  1. Try to validate something
  2. The first validation will only work when you submit the value
  3. After that, all validation works like a "real time validation", when typing.

Hi @devajmeireles, thanks for the report. This is the intended behaviour, but perhaps there is room for improvement.

For example, if an email address is being validated, we don't want to display an "Invalid email" error when they've only typed the first letter. However, in your example, I can understand that displaying an error might be a better experience as soon as they go over the character limit.

An alternative approach could be immediately displaying the error if the value was previously valid. I still think this would be a problem with email validation, as typing "jess@example.com" would be valid, but if I continued entering ".au", it would become invalid again when I typed the "." and I'd see an error before I had finished entering what I intended.

For length validation, it could be interesting to add a max argument to the text inputs, similar to <input max="3" /> on the web. This could prevent further characters from being entered rather than displaying an error. It does get a little tricky with multi-byte encoding, where characters like emojis can count as multiple characters, depending on what you need to do with it, such as storing it in a database column with a limited length.

var_dump(strlen('๐Ÿ˜€')); // int(4)
var_dump(mb_strlen('๐Ÿ˜€')); // int(1)

With the validate closure, developers can use whichever makes sense for them.

Given the caveats, I don't think there are any changes I'd like to make at this time, but I'm always open to suggestions.