note 117387 added to filter.filters.validate

From: Date: Tue, 02 Jun 2015 08:54:08 +0000
Subject: note 117387 added to filter.filters.validate
Groups: php.notes 
Request: Send a blank email to php-notes+get-202721@lists.php.net to get a copy of this message
Contrary to what documentation implies, the FILTER_NULL_ON_FAILURE seem to affect any validation filter, not just FILTER_VALIDATE_BOOLEAN. I've been using that since PHP 5.2, and as of PHP 5.6.8 it still works. I have no clue if it's a blug or if it is as intended, in which case the documentation needs to be fixed. When the flag is used on a validation filter other than FILTER_VALIDATE_BOOLEAN, as expected the filter will return NULL instead of FALSE upon failure. This is quite useful when filtering a POST form with filter_input_array(), where you don't want to check what field is invalid and what field is missing. Just check if NULL is among the returned elements and you're done. <?php $definition = array( 'login' => array( 'filter' => FILTER_VALIDATE_STRING, 'flags' => FILTER_NULL_ON_FAILURE ), 'pwd' => FILTER_UNSAFE_RAW ); $form_data = filter_input_array(INPUT_POST, $definition); if(in_array(null, $form_data, true)) { // invalid form } else { // valid form, let's proceed } ?> Of course, if you want more precise error messages that approach won't work. But it's still good to know, i believe. ---- Server IP: 72.52.91.14 Probable Submitter: 85.68.43.36 ---- Manual Page -- http://php.net/manual/en/filter.filters.validate.php Edit -- https://master.php.net/note/edit/117387 Del: integrated -- https://master.php.net/note/delete/117387/integrated Del: useless -- https://master.php.net/note/delete/117387/useless Del: bad code -- https://master.php.net/note/delete/117387/bad+code Del: spam -- https://master.php.net/note/delete/117387/spam Del: non-english -- https://master.php.net/note/delete/117387/non-english Del: in docs -- https://master.php.net/note/delete/117387/in+docs Del: other reasons-- https://master.php.net/note/delete/117387 Reject -- https://master.php.net/note/reject/117387 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202721) next »