/
Validate.php
1396 lines (1180 loc) · 39.1 KB
/
Validate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* DataTables PHP libraries.
*
* PHP libraries for DataTables and DataTables Editor, utilising PHP 5.3+.
*
* @author SpryMedia
* @copyright 2012-2014 SpryMedia ( http://sprymedia.co.uk )
* @license http://editor.datatables.net/license DataTables Editor
* @link http://editor.datatables.net
*/
namespace DataTables\Editor;
if (!defined('DATATABLES')) exit();
use DataTables\Editor\ValidateOptions;
/**
* Validation methods for DataTables Editor fields.
*
* These methods will typically be applied through the {@link Field::validator}
* method and thus the arguments to be passed will be automatically resolved
* by Editor.
*
* The validation methods in this class all take three parameters:
*
* 1. Data to be validated
* 2. Full data from the form (this can be used with a custom validation method
* for dependent validation).
* 3. Validation configuration options.
*
* When using the `Validate` class functions with the {@link Field::validator}
* method, the second parameter passed into {@link Field::validator} is given
* to the validation functions here as the third parameter. The first and
* second parameters are automatically resolved by the {@link Field} class.
*
* The validation configuration options is an array of options that can be used
* to customise the validation - for example defining a date format for date
* validation. Each validation method has the option of defining its own
* validation options, but all validation methods provide four common options:
*
* * `{boolean} optional` - Require the field to be submitted (`false`) or not
* (`true` - default). When set to `true` the field does not need to be
* included in the list of parameters sent by the client - if set to `false`
* then it must be included. This option can be be particularly used in Editor
* as Editor will not set a value for fields which have not been submitted -
* giving the ability to submit just a partial list of options.
* * `{boolean} empty` - Allow a field to be empty, i.e. a zero length string -
* `''` (`true` - default) or require it to be non-zero length (`false`).
* * `{boolean} required` - Short-cut for `optional=false` and `empty=false`.
* Note that if this option is set the `optional` and `empty` parameters are
* automatically set and cannot be overridden by passing in different values.
* * `{string} message` - Error message shown should validation fail. This
* provides complete control over the message shown to the end user, including
* internationalisation (i.e. to provide a translation that is not in the
* English language).
*
* @example
* <code>
* // Ensure that a non-empty value is given for a field
* Field::inst( 'engine' )->validator( Validate::required() )
* </code>
*
* @example
* <code>
* // Don't require a field to be submitted, but if it is submitted, it
* // must be non-empty
* Field::inst( 'reg_date' )->validator( Validate::notEmpty() )
* </code>
*
* @example
* <code>
* // Date validation
* Field::inst( 'reg_date' )->validator( Validate::dateFormat( 'D, d M y' ) )
* </code>
*
* @example
* <code>
* // Date validation with a custom error message
* Field::inst( 'reg_date' )->validator( Validate::dateFormat( 'D, d M y',
* ValidateOptions::inst()
* ->message( 'Invalid date' )
* ) )
* </code>
*
* @example
* <code>
* // Require a non-empty e-mail address
* Field::inst( 'reg_date' )->validator( Validate::email( ValidateOptions::inst()
* ->empty( false )
* ) )
* </code>
*
* @example
* <code>
* // Custom validation - closure
* Field::inst( 'engine' )->validator( function($val, $data, $opts) {
* if ( ! preg_match( '/^1/', $val ) ) {
* return "Value <b>must</b> start with a 1";
* }
* return true;
* } )
* </code>
*/
class Validate {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Internal functions
*/
/**
* "Magic" method to automatically apply the required check to any of the
* static methods simply by adding '_required' or '_empty' to the end of the
* method's name, depending on if you need the field to be submitted or not.
*
* This is retained for backwards compatibility, but the validation options
* are now the recommended way of indicating that a field is required.
*
* @internal
* @param string $name Function name
* @param array $arguments Function arguments
* @return mixed|string
*/
public static function __callStatic( $name, $arguments ) {
if ( preg_match( '/_required$/', $name ) ) {
if ( $arguments[0] === null || $arguments[0] === '' ) {
return 'This field is required';
}
return call_user_func_array(
__NAMESPACE__.'\Validate::'.str_replace( '_required', '', $name ),
$arguments
);
}
}
/**
* Extend the options from the user function and the validation function
* with core defaults.
*
* @internal
*/
public static function _extend( $userOpts, $prop, $fnOpts ) {
$cfg = array(
'message' => 'Input not valid',
'required' => false,
'empty' => true,
'optional' => true
);
if ( ! is_array( $userOpts ) ) {
if ( $prop ) {
$cfg[ $prop ] = $userOpts;
}
// Not an array, but the non-array case has been handled above, so
// just an empty array
$userOpts = array();
}
// Merge into a single array - first array gets priority if there is a
// key clash, so user first, then function commands and finally the
// global options
$cfg = $userOpts + $fnOpts + $cfg;
return $cfg;
}
/**
* Perform common validation using the configuration parameters
*
* @internal
*/
public static function _common( $val, $opts ) {
$optional = $opts->optional();
$empty = $opts->allowEmpty();
// Error state tests
if ( ! $optional && $val === null ) {
// Value must be given
return false;
}
else if ( $empty === false && $val === '' ) {
// Value must not be empty
return false;
}
// Validation passed states
if ( $optional && $val === null ) {
return true;
}
else if ( $empty === true && $val === '' ) {
return true;
}
// Have the specific validation function perform its tests
return null;
}
/**
* Convert the old style validation parameters into ValidateOptions
*
* @internal
*/
public static function _commonLegacy( $cfg ) {
$opts = new ValidateOptions();
if ( is_array( $cfg ) ) {
// `required` is a legacy shortcut for optional=false, empty=false
if ( isset( $cfg['required'] ) ) {
$opts->optional( false );
$opts->allowEmpty( false );
}
if ( isset( $cfg['empty'] ) ) {
$opts->allowEmpty( $cfg['empty'] );
}
if ( isset( $cfg['message'] ) ) {
$opts->message( $cfg['message'] );
}
if ( isset( $cfg['optional'] ) ) {
$opts->optional( $cfg['optional'] );
}
}
return $opts;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Basic validation
*/
/**
* No validation - all inputs are valid.
* @return callable Validation function
*/
public static function none() {
return function ( $val, $data, $field, $host ) {
return true;
};
}
/**
* Basic validation - this is used to perform the validation provided by the
* validation options only. If the validation options pass (e.g. `required`,
* `empty` and `optional`) then the validation will pass regardless of the
* actual value.
*
* Note that there are two helper short-cut methods that provide the same
* function as this method, but are slightly shorter:
*
* ```
* // Required:
* Validate::required()
*
* // is the same as
* Validate::basic( $val, $data, array(
* "required" => true
* );
* ```
*
* ```
* // Optional, but not empty if given:
* Validate::notEmpty()
*
* // is the same as
* Validate::basic( $val, $data, array(
* "empty" => false
* );
* ```
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
static function basic( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
return $common === false ?
$opts->message() :
true;
};
}
/**
* Required field - there must be a value and it must be a non-empty value
*
* This is a helper short-cut method which is the same as:
*
* ```
* Validate::basic( $val, $data, array(
* "required" => true
* );
* ```
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
static function required( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
$opts->allowEmpty( false );
$opts->optional( false );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
return $common === false ?
$opts->message() :
true;
};
}
/**
* Optional field, but if given there must be a non-empty value
*
* This is a helper short-cut method which is the same as:
*
* ```
* Validate::basic( $val, $data, array(
* "empty" => false
* );
* ```
*
* @param ValidateOptions $cfg Validation options
* @return callable Validation function
*/
static function notEmpty( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
$opts->allowEmpty( false );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
return $common === false ?
$opts->message() :
true;
};
}
/**
* Validate an input as a boolean value.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function boolean( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
if ( filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null ) {
return $opts->message();
}
return true;
};
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Number validation methods
*/
/**
* Check that any input is numeric.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. Additional options:
* * `decimal`: is available to indicate what character should be used
* as the decimal
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function numeric ( $decimal=".", $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $decimal ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
if ( $decimal !== '.' ) {
$val = str_replace( $decimal, '.', $val );
}
return ! is_numeric( $val ) ?
$opts->message() :
true;
};
}
/**
* Check for a numeric input and that it is greater than a given value.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. Additional options:
* * `min`: indicate the minimum value. If only the default validation
* options are required, this parameter can be given as an integer
* value, which will be used as the minimum value.
* * `decimal`: is available to indicate what character should be used
* as the decimal
* separator (default '.').
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function minNum ( $min, $decimal=".", $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $min, $decimal ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$fn = Validate::numeric( $decimal, $opts );
$numeric = $fn( $val, $data, $field, $host );
if ( $numeric !== true ) {
return $numeric;
}
if ( $decimal !== '.' ) {
$val = str_replace( $decimal, '.', $val );
}
return $val < $min ?
$opts->message() :
true;
};
}
/**
* Check for a numeric input and that it is less than a given value.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options.
* * `max`: indicate the maximum value. If only the default validation
* options are required, this parameter can be given as an integer
* value, which will be used as the maximum value.
* * `decimal`: is available to indicate what character should be used
* as the decimal
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function maxNum ( $max, $decimal=".", $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $max, $decimal ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$fn = Validate::numeric( $decimal, $opts );
$numeric = $fn( $val, $data, $field, $host );
if ( $numeric !== true ) {
return $numeric;
}
if ( $decimal !== '.' ) {
$val = str_replace( $decimal, '.', $val );
}
return $val > $max ?
$opts->message() :
true;
};
}
/**
* Check for a numeric input and that it is both greater and smaller than
* given numbers.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. Additional options:
* * `min`: indicate the minimum value.
* * `max`: indicate the maximum value.
* * `decimal`: is available to indicate what character should be used
* as the decimal
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function minMaxNum ( $min, $max, $decimal='.', $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $min, $max, $decimal ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$fn = Validate::numeric( $decimal, $opts );
$numeric = $fn( $val, $data, $field, $host );
if ( $numeric !== true ) {
return $numeric;
}
$fn = Validate::minNum( $min, $decimal, $opts );
$numeric = $fn( $val, $data, $field, $host );
if ( $numeric !== true ) {
return $numeric;
}
$fn = Validate::maxNum( $max, $decimal, $opts );
$numeric = $fn( $val, $data, $field, $host );
if ( $numeric !== true ) {
return $numeric;
}
return true;
};
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* String validation methods
*/
/**
* Validate an input as an e-mail address.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function email( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return filter_var($val, FILTER_VALIDATE_EMAIL) !== false ?
true :
$opts->message();
};
}
/**
* Validate a string has a minimum length.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional option of
* `min` is available for this method to indicate the minimum string
* length. If only the default validation options are required, this
* parameter can be given as an integer value, which will be used as the
* minimum string length.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function minLen( $min, $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $min, $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$strlen = is_callable('mb_strlen') ?
'mb_strlen' :
'strlen';
return $strlen( $val ) < $min ?
$opts->message() :
true;
};
}
/**
* Validate a string does not exceed a maximum length.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional option of
* `max` is available for this method to indicate the maximum string
* length. If only the default validation options are required, this
* parameter can be given as an integer value, which will be used as the
* maximum string length.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function maxLen( $max, $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $max, $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$strlen = is_callable('mb_strlen') ?
'mb_strlen' :
'strlen';
return $strlen( $val ) > $max ?
$opts->message() :
true;
};
}
/**
* Require a string with a certain minimum or maximum number of characters.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of
* `min` and `max` are available for this method to indicate the minimum
* and maximum string lengths, respectively.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function minMaxLen( $min, $max, $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $min, $max ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$fn = Validate::minLen( $min, $opts );
$res = $fn( $val, $data, $field, $host );
if ( $res !== true ) {
return $res;
}
$fn = Validate::maxLen( $max, $opts );
$res = $fn( $val, $data, $field, $host );
if ( $res !== true ) {
return $res;
}
return true;
};
}
/**
* Validate as an IP address.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function ip( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return filter_var($val, FILTER_VALIDATE_IP) !== false ?
true :
$opts->message();
};
}
/**
* Validate as an URL address.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array $opts Validation options. No additional options are
* available or required for this validation method.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function url( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return filter_var($val, FILTER_VALIDATE_URL) !== false ?
true :
$opts->message();
};
}
/**
* Check if string could contain an XSS attack string
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of
* `db` - database connection object, `table` - database table to use and
* `column` - the column to check this value against as value, are also
* available. These options are not required and if not given are
* automatically derived from the Editor and Field instances.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function xss ( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return $field->xssSafety( $val ) != $val ?
$opts->message() :
true;
};
}
/**
* Confirm that the value submitted is in a list of allowable values
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of
* `db` - database connection object, `table` - database table to use and
* `column` - the column to check this value against as value, are also
* available. These options are not required and if not given are
* automatically derived from the Editor and Field instances.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function values( $values, $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $values, $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return in_array($val, $values) ?
true :
$opts->message();
};
}
/**
* Check if there are any tags in the submitted value
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of
* `db` - database connection object, `table` - database table to use and
* `column` - the column to check this value against as value, are also
* available. These options are not required and if not given are
* automatically derived from the Editor and Field instances.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function noTags ( $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
return strip_tags( $val ) != $val ?
$opts->message() :
true;
};
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Date validation methods
*/
/**
* Check that a valid date input is given
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param array|string $opts If given as a string, then $opts is the date
* format to check the validity of. If given as an array, then the
* date format is in the 'format' parameter, and the return error
* message in the 'message' parameter.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function dateFormat( $format, $cfg=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $format, $opts ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$formatCreate = substr($format, 0, 1) !== '!' ?
'!'.$format :
$format;
$date = \DateTime::createFromFormat( $formatCreate, $val );
return $date && $date->format( $format ) == $val ?
true :
$opts->message();
};
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Database validation methods
*/
/**
* Check that the given value is unique in the database
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of
* `db` - database connection object, `table` - database table to use and
* `column` - the column to check this value against as value, are also
* available. These options are not required and if not given are
* automatically derived from the Editor and Field instances.
* @param array $host Host information
* @return string|true true if the value is valid, a string with an error
* message otherwise.
*/
public static function unique( $cfg=null, $column=null, $table=null, $db=null ) {
$opts = ValidateOptions::select( $cfg );
return function ( $val, $data, $field, $host ) use ( $opts, $column, $table, $db ) {
$common = Validate::_common( $val, $opts );
if ( $common !== null ) {
return $common === false ?
$opts->message() :
$common;
}
$editor = $host['editor'];
if ( ! $db ) {
$db = $host['db'];
}
if ( ! $table ) {
$table = $editor->table(); // Returns an array, but `select` will take an array
}
if ( ! $column ) {
$column = $field->dbField();
}
$query = $db
->query( 'select', $table )
->get( $column )
->where( $column, $val );
// If doing an edit, then we need to also discount the current row,
// since it is of course already validly unique
if ( $host['action'] === 'edit' ) {
$cond = $editor->pkeyToArray( $host['id'], true );
$query->where( $cond, null, '!=' );
}
$res = $query->exec();
return $res->count() === 0 ?
true :
$opts->message();
};
}
/**
* Check that the given value is a value that is available in a database -
* i.e. a join primary key. This will attempt to automatically use the table
* name and value column from the field's `options` method (under the
* assumption that it will typically be used with a joined field), but the
* table and field can also be specified via the options.
*
* @param string $val The value to check for validity
* @param string[] $data The full data set submitted
* @param int|array $opts Validation options. The additional options of