Tagged: sanitization
- This topic has 3 replies, 2 voices, and was last updated 6 years, 6 months ago by
Steve.
-
AuthorPosts
-
-
August 4, 2015 at 6:50 pm #4139
mcmasterMemberI’m using the front-end forms code (beta 0.9.5v). I can’t seem to get the sanitizing code right. I’ve tried two models. The first is what’s described here: https://piklist.com/user-guide/docs/sanitizing-field-data/
piklist('field', array( 'type' => 'text', 'field' => 'donation', 'scope' => 'post_meta', 'value' => '', 'template' => 'mcw_quantity', 'label' => 'Amount of Donation', 'sanitize' => array( array( 'type' => 'text', 'callback' => 'mcw_sanitize_integer', 'options' => array( 'non_negative' => true, ), ), ), 'required' => false, ));My callback function is:
function mcw_sanitize_integer ( $value, $field, $options ) { mcw_log( "got here!" ); $number = preg_match( '/^[0-9]+$/', $value ); $number = (int)$number; if ( isset( $options['non_negative'] ) && $options['non_negative'] && ( $number < 0 ) ) { return '0'; } else { return "$number"; } }The problem is that the callback function never gets called.
-
August 4, 2015 at 7:01 pm #4140
mcmasterMemberThen I found this page: https://piklist.com/user-guide/docs/piklist_sanitization_rules/
So I added a ‘piklist_sanitization_rules’ filter and created a rule for ‘mcw_integer’, using the same callback function:add_filter( 'piklist_sanitization_rules', 'mcw_add_sanitize_rule', 11 ); function mcw_add_sanitize_rule ( $sanitization_rules ) { $sanitization_rules['mcw_integer'] = array( 'callback' => 'mcw_sanitize_integer' ); return $sanitization_rules; } function mcw_sanitize_integer ( $value, $field, $options ) { $number = preg_match( '/^[0-9]+$/', $value ); $number = (int)$number; if ( isset( $options['non_negative'] ) && $options['non_negative'] && ( $number < 0 ) ) { return '0'; } else { return "$number"; } }I changed the field definition to:
piklist('field', array( 'type' => 'text', 'field' => 'donation', 'scope' => 'post_meta', 'value' => '', 'template' => 'mcw_quantity', 'label' => 'Amount of Donation', 'sanitize' => array( array( 'type' => 'mcw_integer', 'options' => array( 'non_negative' => true, ), ), ), 'required' => false, ));The good news is that it does call the callback function. But it kept giving me an error message: ‘PHP Notice: Undefined index: type in /…/wp-content/plugins/piklist/includes/class-piklist-validate.php on line 304
But of course, now that I’m reporting this, the error message has gone away. So it looks as though the second model is working.
It would be really helpful to have one complete example that shows both ends of the process, and I would like to know what I did wrong with my first attempt.
Thanks! 🙂
-
August 4, 2015 at 8:12 pm #4141
mcmasterMemberp.s. If you looked closely, you probably figured out that the preg_match phrase doesn’t work. I ended up going in a different direction as all I really wanted to do was remove the dollar sign if someone entered it with the amount. So in case it’s helpful, here’s what I’m using:
$number = str_replace( '$', '', $value); $number = (int)$number; -
August 5, 2015 at 12:39 pm #4152
SteveKeymaster@mcmaster– Glad you got it working! Those docs you mentioned are now cross-linked.
The reason your code in the first example didn’t work is because you called a sanitization method that doesn’t exist: “text”, should be “text_field“:
'sanitize' => array( array( 'type' => 'text_field' ) )This inspired a new notice in the next version of Piklist, if you have WP_DEBUG turned on:
Notice: Sanitization type "text" is not valid.
-
-
AuthorPosts
- You must be logged in to reply to this topic.