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

JFormValidator - Optional field with pattern attribute marked invalid #19751

Closed
aaron-harding opened this issue Feb 21, 2018 · 5 comments
Closed

Comments

@aaron-harding
Copy link
Contributor

aaron-harding commented Feb 21, 2018

Steps to reproduce the issue

Add the following field to any module manifest

<field
	name="test"
	type="text"
	description="testing field validation"
	filter="string"
	label="Test"
	pattern="[A-Za-z\s0-9]+" />

Click on the test field and then select another without filling anything in. This will happen on any form which uses JFormValidator / JHtml::_('behavior.formvalidator');

Expected result

The field to not be marked as invalid

Actual result

Gets highlighted red and marked as invalid.

System information (as much as possible)

Joomla 3.8.5

Additional comments

Optional fields should only validate a pattern when a value is present. validate.js already checks for value.length but defaults to false if length is 0: https://github.com/joomla/joomla-cms/blob/staging/media/system/js/validate-uncompressed.js#L89-L96. Removing the else statement solves this but i'm not sure if this had been added intentionally.

This same issue is also present with J4.

@alikon
Copy link
Contributor

alikon commented Feb 21, 2018

make sense
something like this snippet for https://github.com/joomla/joomla-cms/blob/staging/media/system/js/validate-uncompressed.js#L89-L96

if ($el.val().length) {
	isValid = new RegExp('^'+$el.attr('pattern')+'$').test($el.val());
	handleResponse(isValid, $el);
	return isValid;
		
}
if ($el.attr('required') || $el.hasClass('required')) {
	handleResponse(false, $el);
	return false;
}
handleResponse(true, $el);
return true;

@dgrammatiko
Copy link
Contributor

J4 has the code you’re looking for, someone needs to backport it here

@aaron-harding
Copy link
Contributor Author

Looks like J4 will have the same issue: https://github.com/joomla/joomla-cms/blob/4.0-dev/media/system/js/fields/validate.js#L165-L172. It defaults to false in the else statement, can this default to true instead?

@joomla-cms-bot
Copy link

Set to "closed" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/19751

@ghost
Copy link

ghost commented Feb 24, 2018

closed as having Pull Request #19771

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants