1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Required extends Quform_Validator_Abstract
7: {
8: const INVALID = 'requiredInvalid';
9: const REQUIRED = 'required';
10:
11: 12: 13: 14: 15: 16:
17: public function isValid($value)
18: {
19: $this->reset();
20:
21: if (is_string($value)) {
22: if ($value !== '') {
23: return true;
24: }
25: } else if (is_array($value)) {
26: foreach (array_values($value) as $val) {
27: if (is_string($val) && $val !== '') {
28: return true;
29: }
30: }
31: } else {
32: $this->error(self::INVALID);
33: return false;
34: }
35:
36: $this->error(self::REQUIRED);
37: return false;
38: }
39:
40: 41: 42: 43: 44: 45:
46: public static function getMessageTemplates($key = null)
47: {
48: $messageTemplates = array(
49: self::INVALID => __('Invalid type given, string or array expected', 'quform'),
50: self::REQUIRED => __('This field is required', 'quform')
51: );
52:
53: if (is_string($key)) {
54: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
55: }
56:
57: return $messageTemplates;
58: }
59:
60: 61: 62: 63: 64: 65:
66: public static function getDefaultConfig($key = null)
67: {
68: $config = apply_filters('quform_default_config_validator_required', array(
69: 'messages' => array(
70: self::INVALID => '',
71: self::REQUIRED => ''
72: )
73: ));
74:
75: $config['type'] = 'required';
76:
77: if (Quform::isNonEmptyString($key)) {
78: return Quform::get($config, $key);
79: }
80:
81: return $config;
82: }
83: }
84: