Skip to content
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

integer and boolean validation rules with empty values #302

Closed
Mutusen opened this issue Mar 9, 2020 · 2 comments
Closed

integer and boolean validation rules with empty values #302

Mutusen opened this issue Mar 9, 2020 · 2 comments

Comments

@Mutusen
Copy link
Contributor

Mutusen commented Mar 9, 2020

Hi, thank you for this project, I find it very useful and simple. Since version 1.7 update it seems like the integer and boolean validation rules (and presumably other filters) return false when given an empty value. It is inconvenient, because it cannot validate an optional numeric field, and unchecked checkboxes sent through an HTML have no value, so it effectively makes checkboxes with the boolean validation rule mandatory.

However, this happens only with the new format (listing the validation rules as an array). If I list them as a string with | as a separator, it seems to be working as it did before.

You can try it with the following code:

<?php
require('vendor/autoload.php');

if (isset($_POST['mandatory_number'])) {
	echo 'Old format:<br>';
	$is_valid = GUMP::is_valid($_POST, [
		'mandatory_number' => 'integer|required',
		'optional_number' => 'integer',
		'checkbox' => 'boolean',
	]);
	if ($is_valid === true) {
		echo 'ok';
	} else {
		print_r($is_valid);
	}
	echo '<br>New format:<br>';
	$is_valid = GUMP::is_valid($_POST, [
		'mandatory_number' => ['integer', 'required'],
		'optional_number' => ['integer'],
		'checkbox' => ['boolean'],
	]);
	if ($is_valid === true) {
		echo 'ok';
	} else {
		print_r($is_valid);
	}
}
?>
<form action="" method="post">
	<input type="number" name="mandatory_number">
	<input type="number" name="optional_number">
	<input type="checkbox" name="checkbox">
	<input type="submit">
</form>
@filisko filisko closed this as completed in 924e218 Mar 9, 2020
@filisko
Copy link
Collaborator

filisko commented Mar 9, 2020

Thanks 🎉

@filisko
Copy link
Collaborator

filisko commented Mar 9, 2020

filisko added a commit that referenced this issue Jul 15, 2022
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

No branches or pull requests

2 participants