zend-validate-uniquefield
Gesponsorde koppelingen
PHP script bestanden
Gebruik:
Je moet een model maken met minimaal het volgende:
Code (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
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
<?php
class Users extends Zend_Db_Table
{
protected $_name = 'users';
protected $_primary = 'user_id';
}
[/code]
($_primary is natuurlijk niet nodig maar ik gebruik ID niet als mijn primary key)
het form element:
[code]
[code]<?php
$this->addElement('text', 'name', array(
'decorators' => $this->_standardElementDecorator,
'label' => 'Desired Username:',
'Validators' => array(
array('StringLength', false, array(5,20)),
array('UniqueField', false, array('users', 'username')),
),
'required' => true
));
[/code]
specifiek deze regel:
[code]array('UniqueField', false, array('users', 'username'))[/code]
UniqueField = naam van validator.
users = naam van model
username = naam van veld
en de validator:
ps. Ja ik heb hem gewoon in mijn Zend/Validate folder staan :)
[code]
[code]<?php
class Zend_Validate_UniqueField extends Zend_Validate_Abstract
{
/**
* @var String
*/
const NOT_UNIQUE = 'error';
/**
* @var String
*/
protected $_modelName = 'Users';
/**
* @var String
*/
protected $_fieldName = 'username';
/**
* @var array
*/
protected $_messageTemplates = array(
self::NOT_UNIQUE => "The data given is not unique."
);
/**
* Sets validator options
*
* @param string $modelName
* @param string $fieldName
* @return void
*/
public function __construct($modelName = '', $fieldName = '')
{
$this->setModelName($modelName);
$this->setFieldName($fieldName);
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the email address is found to NOT have a match in the fieldname in the model specified
*
* @param Array $value
* @param Array $request
* @return boolean
*/
public function isValid($value, $request = null)
{
// Set the value
$value = (string) $value;
$this->_setValue($value);
// Get our model and do a search for that username
$table = new $this->_modelName;
$result = $table->fetchAll($table->select()->where("$this->_fieldName = '$value'"))->toArray();
// Check to see if we got any results
if (count($result) == 0) {
return true;
}
$this->_error(self::NOT_UNIQUE);
return false;
}
/**
* Set the model name.
*
* @param string $modelName
* @return Zend_Validate_UniqueUsername Provides a fluent interface
*/
public function setModelName($modelName)
{
$this->_modelName = $modelName;
return $this;
}
/**
* Set the field name.
*
* @param string $fieldName
* @return Zend_Validate_UniqueUsername Provides a fluent interface
*/
public function setFieldName($fieldName)
{
$this->_fieldName = $fieldName;
return $this;
}
}
[/code]
Ik hoop dat iemand hier iets aan heeft.
class Users extends Zend_Db_Table
{
protected $_name = 'users';
protected $_primary = 'user_id';
}
[/code]
($_primary is natuurlijk niet nodig maar ik gebruik ID niet als mijn primary key)
het form element:
[code]
[code]<?php
$this->addElement('text', 'name', array(
'decorators' => $this->_standardElementDecorator,
'label' => 'Desired Username:',
'Validators' => array(
array('StringLength', false, array(5,20)),
array('UniqueField', false, array('users', 'username')),
),
'required' => true
));
[/code]
specifiek deze regel:
[code]array('UniqueField', false, array('users', 'username'))[/code]
UniqueField = naam van validator.
users = naam van model
username = naam van veld
en de validator:
ps. Ja ik heb hem gewoon in mijn Zend/Validate folder staan :)
[code]
[code]<?php
class Zend_Validate_UniqueField extends Zend_Validate_Abstract
{
/**
* @var String
*/
const NOT_UNIQUE = 'error';
/**
* @var String
*/
protected $_modelName = 'Users';
/**
* @var String
*/
protected $_fieldName = 'username';
/**
* @var array
*/
protected $_messageTemplates = array(
self::NOT_UNIQUE => "The data given is not unique."
);
/**
* Sets validator options
*
* @param string $modelName
* @param string $fieldName
* @return void
*/
public function __construct($modelName = '', $fieldName = '')
{
$this->setModelName($modelName);
$this->setFieldName($fieldName);
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the email address is found to NOT have a match in the fieldname in the model specified
*
* @param Array $value
* @param Array $request
* @return boolean
*/
public function isValid($value, $request = null)
{
// Set the value
$value = (string) $value;
$this->_setValue($value);
// Get our model and do a search for that username
$table = new $this->_modelName;
$result = $table->fetchAll($table->select()->where("$this->_fieldName = '$value'"))->toArray();
// Check to see if we got any results
if (count($result) == 0) {
return true;
}
$this->_error(self::NOT_UNIQUE);
return false;
}
/**
* Set the model name.
*
* @param string $modelName
* @return Zend_Validate_UniqueUsername Provides a fluent interface
*/
public function setModelName($modelName)
{
$this->_modelName = $modelName;
return $this;
}
/**
* Set the field name.
*
* @param string $fieldName
* @return Zend_Validate_UniqueUsername Provides a fluent interface
*/
public function setFieldName($fieldName)
{
$this->_fieldName = $fieldName;
return $this;
}
}
[/code]
Ik hoop dat iemand hier iets aan heeft.