-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
user input validation #634
Comments
Hi @lufre1, can you clarify the moment at which you would have the data validated? The values coming from the widget should always be of the valid type, so, if you declared an Another comment is that validation is a huge ball of wax that appears at first to be simple (certainly for simple types), but ends up being complex. If you need validated data, I would suggest using pydantic or other similar libraries that perform validation and coercion. magicgui has experimental support for pydantic models (i.e. it can create a widget from a pydantic model), but at the moment, the functionality is not public and is a bit hard to find. See https://forum.image.sc/t/using-magicgui-to-create-pydantic-class-instances/92520 and https://forum.image.sc/t/building-a-napari-widget-from-a-pydantic-model/90257 for a bit more info on that |
Hi @tlambert03, public function validateForm(array &$form, FormStateInterface $form_state) {
if (strlen($form_state->getValue('phone_number')) < 3) {
$form_state->setErrorByName('phone_number', $this->t('The phone number is too short. Please enter a full phone number.'));
}
} The intent is to inform the user via tiny messages of missing/incorrect inputs without the need for exception handling. |
If you mean inputs made by the user in the widgets, then yeah, I can see that as a good feature, but probably only for the string fields (line edits). All the other widgets should already be validated. Does that fit with your expectations? Qt, for example, does allow custom validators to be attached to line edits, and we could look into making that easier to hook into |
Yes exactly,
For use cases like the one shown above, where the required fields are changed dynamically, it would be nice to have a hook like the one you mentioned. |
ok thanks. I think we've got a mutual vision now :)
i do think this might be a widget-specific consideration. for example, number widgets should already have all the info necessary such that it's impossible to get an invalid number. But, just for the sake of generality, i agree it could make sense to let all widgets take a user-provided validation function. This is a relatively significant feature, so it will take some time to think about, but I agree on the need. thanks for the issue! |
A feature that checks the inputs given with a customizable logic. This would help e.g. to prevent code being executed that depends on several inputs but some inputs are missing.
code example:
Is there something similar that I haven't found yet? The current solution would be to use exception handling.
The text was updated successfully, but these errors were encountered: