Skip to content

Commit 8b4510e

Browse files
authored
Fix Model/Property Validation (#10)
Fix Model/Property Validation
2 parents 1845a60 + ca77046 commit 8b4510e

File tree

8 files changed

+58
-21
lines changed

8 files changed

+58
-21
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"prefer-stable": true,
2020
"extra": {
2121
"branch-alias": {
22-
"dev-master": "0.8.x-dev"
22+
"dev-master": "0.9.x-dev"
2323
}
2424
},
2525
"require": {
@@ -30,7 +30,7 @@
3030
"psr/log": "^1.0",
3131
"psr/cache": "^1.0",
3232
"locomotivemtl/charcoal-config": "~0.9",
33-
"locomotivemtl/charcoal-core": "~0.4",
33+
"locomotivemtl/charcoal-core": "~0.5",
3434
"locomotivemtl/charcoal-factory": "~0.4",
3535
"locomotivemtl/charcoal-image": "~0.4",
3636
"locomotivemtl/charcoal-translator": "~0.3"

src/Charcoal/Property/AbstractProperty.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,10 @@ public function l10nIdent($lang = null)
291291
/**
292292
* Set the property's value.
293293
*
294+
* @deprecated
295+
*
294296
* @param mixed $val The property (raw) value.
295297
* @return self
296-
* @deprecated
297298
*/
298299
final public function setVal($val)
299300
{
@@ -302,11 +303,26 @@ final public function setVal($val)
302303
return $this;
303304
}
304305

306+
/**
307+
* Clear the property's value.
308+
*
309+
* @deprecated
310+
*
311+
* @return self
312+
*/
313+
final public function clearVal()
314+
{
315+
$this->val = null;
316+
317+
return $this;
318+
}
319+
305320
/**
306321
* Retrieve the property's value.
307322
*
308-
* @return mixed
309323
* @deprecated
324+
*
325+
* @return mixed
310326
*/
311327
final public function val()
312328
{
@@ -840,7 +856,7 @@ public function validationMethods()
840856
return [
841857
'required',
842858
'unique',
843-
'allowNull'
859+
'allowNull',
844860
];
845861
}
846862

@@ -885,8 +901,6 @@ public function validateAllowNull()
885901
return true;
886902
}
887903

888-
889-
890904
/**
891905
* @param mixed $val The value, at time of saving.
892906
* @return mixed

src/Charcoal/Property/EmailProperty.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public function getMaxLength()
3636
public function validationMethods()
3737
{
3838
$parentMethods = parent::validationMethods();
39+
3940
return array_merge($parentMethods, [
40-
'email'
41+
'email',
4142
]);
4243
}
4344

src/Charcoal/Property/FileProperty.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ public function validationMethods()
369369
{
370370
$parentMethods = parent::validationMethods();
371371

372-
return array_merge($parentMethods, [ 'accepted_mimetypes', 'max_filesize' ]);
372+
return array_merge($parentMethods, [
373+
'acceptedMimetypes',
374+
'maxFilesize',
375+
]);
373376
}
374377

375378
/**

src/Charcoal/Property/IdProperty.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ public function save($val)
173173
return $val;
174174
}
175175

176+
/**
177+
* @return boolean
178+
*/
179+
public function validateRequired()
180+
{
181+
$mode = $this->getMode();
182+
183+
if ($mode !== self::MODE_AUTO_INCREMENT) {
184+
return parent::validateRequired();
185+
}
186+
187+
return true;
188+
}
189+
176190
/**
177191
* Auto-generate a value upon first save.
178192
*

src/Charcoal/Property/PropertyValidator.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
// From 'charcoal-core'
66
use Charcoal\Validator\AbstractValidator;
7+
use Charcoal\Validator\ValidatableInterface;
78

89
/**
9-
*
10+
* Property Validator
1011
*/
1112
class PropertyValidator extends AbstractValidator
1213
{
@@ -15,17 +16,21 @@ class PropertyValidator extends AbstractValidator
1516
*/
1617
public function validate()
1718
{
19+
$result = true;
20+
1821
// The model, in this case, should be a PropertyInterface
19-
$model = $this->model;
22+
$property = $this->model;
23+
24+
$methods = $property->validationMethods();
25+
foreach ($methods as $method) {
26+
$method = $this->camelize($method);
2027

21-
$ret = true;
22-
$validationMethods = $model->validationMethods();
23-
foreach ($validationMethods as $m) {
24-
$fn = [$model, 'validate_'.$m];
25-
if (is_callable($fn)) {
26-
$ret = $ret && call_user_func($fn);
28+
$func = [ $property, 'validate'.ucfirst($method) ];
29+
if (is_callable($func)) {
30+
$result = $result && call_user_func($func);
2731
}
2832
}
29-
return $ret;
33+
34+
return $result;
3035
}
3136
}

src/Charcoal/Property/StringProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public function validationMethods()
325325
'maxLength',
326326
'minLength',
327327
'regexp',
328-
'allowEmpty'
328+
'allowEmpty',
329329
]);
330330
}
331331

tests/Charcoal/Property/FilePropertyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public function testVaidationMethods()
135135
{
136136
$obj = $this->obj;
137137
$ret = $obj->validationMethods();
138-
$this->assertContains('accepted_mimetypes', $ret);
139-
$this->assertContains('max_filesize', $ret);
138+
$this->assertContains('acceptedMimetypes', $ret);
139+
$this->assertContains('maxFilesize', $ret);
140140
}
141141

142142
/**

0 commit comments

Comments
 (0)