The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation Class Improvements
#1
Somehow my topic disappeared while I was editing so I'm reposting it.

https://pastebin.com/KmhvbPFh

46,51 - Replaced empty() with strlen()==0 because if the input was "0" (number zero) then the validation wouldn't work.



78-87 - Optionally you can pass array to the unique for a more complicated query (provided you have this patch). For example:
Code:
"unique"=>["tablename", ["and", ["SomeData","=",1], ["OtherData","=",2]]]
Also it will fail validation if query returned an error (which the original did not do).


110-147 - Added rules <, >, <=, >=, !=, == for comparing numbers. They will automatically check if value is a number so there's no need to stack it with is_numeric. For example:
Code:
">="=>25
(equal to 25 or greater ). You can also compare it against other fields. For example:
Code:
">"=>"Price"
(greater than the number in the "Price" input)


149-152 - Added rule is_int for excluding floats.
Code:
"is_int"=>true


209-210 - In function addError() added condition to prevent error message duplication.

===

EDIT:

54-55, 62-63, 70-73 - One issue with using array keys as rules is that you can't stack them. I've added an option to pass an array to rules: min, max, matches, <, >, <=, >=, !=, ==. For example:
Code:
">"=>[25,"Price"]

154-158 - Added rule is_timezone for verifying time zone name.
Code:
"is_timezone"=>true

100, 149 - Added
Code:
is_num
(synonymous with
Code:
is_numeric
). Added
Code:
is_integer
(synonymous with
Code:
is_int
)

160-189 - Added rule in for matching string lowercase.
Code:
"in"=>["a","b","c"]

30, 38-41 - If value is an array then
Code:
sanitize
will return an error. Because I use
Code:
Input::get
I don't want to sanitize again so I added optional argument to skip it

43-44, 51, 57, 65 - My earlier change to
Code:
strlen
screwed me over because values couldn't be arrays. Added condition to detect value type.

46-49 - Rule
Code:
required
now needs to be set to
Code:
true
in order to work

160-189 - Value for the rule
Code:
in
can now be array. Optionally containing sub-arrays where
Code:
[["display","value"]]

22, 2333 - In my view
Code:
_passed
is an unnecessary state so I've removed it. Better to just check if
Code:
_errors
isn't empty which I can do directly now because it's public.

191-198 - added rule is_datetime following this method. Example:
Code:
"is_datetime"=>"Y-m-d"
  Reply
#2
Thought I'd share my own validation additions: https://pastebin.com/VSpRmJm6

Here's what I added:

Date/Time checks-
valid_date- checks if a date is in a valid format
valid_month- checks if a month date is in a valid format
valid_time- checks if a time is in a valid format
valid_datetime- checks if a full datetime is in a valid format
valid_futuredate- checks to see if a date is in the future

Image checks-
valid_image- checks if an image is of a valid type (there is no foolproof way to ensure it's actually an image, but is will weed out most bad images)
unique_image- checks if an image name already exists
image_width- checks if an image is wide enough
image_height- checks if an image is high enough

Misc. checks-
valid_tracking- validates a tracking number using the Argo class package (https://github.com/dsposito/argo)
in_array- checks if a value is a valid match from a given array
zxcvbn- rates a password strength based on the Zxcvbn class package (https://github.com/bjeavons/zxcvbn-php). I use this in conjunction with the Javascript version for before/after form submission checks
  Reply
#3
@karsen I've looked into your code.

required_if - in my view you can just make required work properly (line 47 in my paste) instead of adding whole new rule.

valid_datetime - it's a shame that you haven't tested it because the formats are different and it's returning error every time. Inspired I've implemented my version of this.

>there is no foolproof way to ensure it’s actually an image, but is will weed out most bad images

PHP manual advises against using getimagesize for validation.

I apply finfo, imagecreatefromjpeg (not in validation class but in a separate function because I handle images after validation) and htaccess.

From what I understand @firestorm uses finfo, getimagesize and optionally imagecreate.

in_array - similar to in but you utilize array keys and I use sub-arrays. By the way, you can create unordered list in a single line:

Code:
$allowed = (empty($rule_value) ? "" : "<ul><li>") . implode("</li><li>", $rule_value) . (empty($rule_value) ? "" : "</li></ul>");


  Reply
#4
Would you mind doing a pastebin for your image validation code?

I like that you condensed the in_array to one line, but it's easier for me to read as several.

I've only recently gotten back into coding after around 10 years of little more than dabbling and had to play catch-up on all the PHP version and industry changes. The validations were one of the first things I modified and a lot of them were practice.
  Reply
#5
>Would you mind doing a pastebin for your image validation code?

It's in the class Generated_Form in the upload_image() method.
  Reply
#6
Awesome, thank you!
  Reply
#7
I'm pushing these into 4.3 and also Karsen's valid_date case. Thanks so much guys! You're making this too easy!
  Reply
#8
Introducing a new rule: 'in'.

In
Code:
Validate.php
, function 'check':
<pre>
Code:
    case 'in':
        if (!in_array($value, $rule_value)) {
            $this->addError(["{$display} has to be one of these: ".implode($rule_value, ','),$item]);
        }
        break;
</pre>





In user file, validation array:
<pre>
Code:
'gender' => array(
    'display' => 'Gender',
    'required' => true,
    'in' => array('m','f')
)
</pre>
  Reply
#9
Oh, now I see that this function was proposed before yet. Excuse me.
  Reply
#10
I'm going to take a look at this for 4.3.5
Thanks!
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)