Tagged: validation sanitization
- This topic has 1 reply, 2 voices, and was last updated 7 months, 1 week ago by
Steve.
-
AuthorPosts
-
-
June 15, 2020 at 7:31 am #10712
bevislw
ParticipantI am just getting started with Piklist and was hoping someone would be able to point me in the right direction with a particular issue.
Is there anyway to convert text input to uppercase in piklist currently? I have a added it to the ‘description’ as a workaround, although this didn’t appear if the text field was inside a group so put it at the top level. Is that correct?
Also, I tried adding ‘pattern’ => ‘^[A-Z]?[0-9]$’ and to the field inside a group but it’s not validating the input correctly. I am still getting ‘please match the format requested’ message.
So I tried another approach using validation rule.
add_filter(‘piklist_validation_rules’, ‘check_discount_code’, 11);
function check_discount_code($validation_rules)
{
$validation_rules[‘discount_code’] = array(
‘rule’ => “/[A-Z]?[0-9]/”
,’message’ => __(‘Please enter discount code with uppercase letters and numbers’)
);return $validation_rules;
},’validate’ => array(
array(
‘type’ => ‘discount_code’
)
)But I am having no joy. Any advice would be greatly appreciated.
-
June 15, 2020 at 12:41 pm #10713
Steve
Keymaster@bevislw– Welcome to the Piklist Community!
Here are a few options:
1. Many developers choose to save the data normally and then convert it to uppercase on output. For that you can use the PHP functionstrtoupper
: https://www.w3schools.com/php/func_string_strtoupper.asp2. To Validate I would recommend using a callback function with
strtoupper
. Something like this should work:function my_check_uppercase($index, $value, $options, $field, $fields) { return $value === strtoupper($value) ? true : 'contains lowercase letters.'; }
Using the callback is documented here: https://docs.piklist.com/actions-filters/filters/piklist_validation_rules/
3. If you want to convert the data on save you should use a Sanitization rule. Validation rules are true/false, where Sanitization rules can change your data. You can also use the
strtoupper
function to convert your data. Here are docs on Sanitization rules: https://docs.piklist.com/actions-filters/filters/piklist_sanitization_rules/Let me know if any of these worked for you.
-
-
AuthorPosts
- The topic ‘Convert text to uppercase’ is closed to new replies.