woocommerce_form_field(): maxlength working, but minlength and pattern won't validate
Issue Description:
On the WooCommerce Checkout with woocommerce_checkout_fields filter I can customize checkout fields. I can add the "required" parameter for example, and also the "maxlength" one, which works perfectly as you can't type in more chars than what you set e.g.:
add_filter( 'woocommerce_checkout_fields', 'bbloomer_checkout_fields_custom_attributes', 9999 );
function bbloomer_checkout_fields_custom_attributes( $fields ) {
$fields['billing']['billing_company']['maxlength'] = 15;
return $fields;
}
This is great and it works. But if I add, similarly, other attributes such as minlength or pattern, nothing happens on the checkout, there is no validation, and user can submit the checkout without any error notification. I've tried to test some snippets here, and of course they were all unsuccessful, apart from this one that adds custom validation errors:
add_action( 'woocommerce_checkout_process', 'bbloomer_checkout_fields_custom_validation' );
function bbloomer_checkout_fields_custom_validation() {
if ( isset( $_POST['billing_company'] ) && ! empty( $_POST['billing_company'] ) ) {
if ( strlen( $_POST['billing_company'] ) < 15 ) {
wc_add_notice( 'Company name requires at least 15 characters', 'error' );
}
}
}
Now, if maxlength is working, I don't see why minlength or pattern should not work i.e. should not validate and stop the checkout from submitting.
Could you get this looked after please?
Hi! it seems to work jsut fine your workaround, although i cant make it transaltable through string translation in WPML. As soon as you wrap it in __() or _e() it simply ignores the condition. Any solution sugested?
EDIT: Solved it, I was doing it wrong. In my case i used this for the minimum billing phone number characters, and now it can be translated using: wc_add_notice(__( 'Phone number cannot be empty','minnum'), 'error' );
Thanks for the enhancement request, looks like minlength or pattern is not supported at the moment.
I am marking this as a low priority for now, in the context of other high priority issues we have open, but we would definitely appreciate pull requests for this enhancement meanwhile.
$fields['billing']['billing_company']['maxlength'] = 15;
This works only for adding HTML5 attribute to the input, it doesn't add server side validation that still needs to be done manually.
I don't think this is really needed. You can use 'custom_attributes' to add a pattern, e.g. $fields['billing']['billing_company']['custom_attributes'] = array('pattern' => '[A-Z]') (Obviously you should be checking if the custom_attributes index already exists and is an array before just replacing it as above).
Having said that, if you could specify a pattern and server-side validation was also done using that pattern automatically, that would be nice.
I do believe supporting minlength is really useful for easy checkout modifications
i would like to take this one during Contributor Day also, but can you please confirm what should I do now? adding minlength and pattern to form field, right?
Yes, when we modify the fields on the filter, those variables does nothing: minlength and pattern