Skip to content

Commit

Permalink
Fix Model/Property Validation (#10)
Browse files Browse the repository at this point in the history
Fix Model/Property Validation
  • Loading branch information
mcaskill authored Nov 4, 2019
2 parents 1845a60 + ca77046 commit 8b4510e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "0.8.x-dev"
"dev-master": "0.9.x-dev"
}
},
"require": {
Expand All @@ -30,7 +30,7 @@
"psr/log": "^1.0",
"psr/cache": "^1.0",
"locomotivemtl/charcoal-config": "~0.9",
"locomotivemtl/charcoal-core": "~0.4",
"locomotivemtl/charcoal-core": "~0.5",
"locomotivemtl/charcoal-factory": "~0.4",
"locomotivemtl/charcoal-image": "~0.4",
"locomotivemtl/charcoal-translator": "~0.3"
Expand Down
24 changes: 19 additions & 5 deletions src/Charcoal/Property/AbstractProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,10 @@ public function l10nIdent($lang = null)
/**
* Set the property's value.
*
* @deprecated
*
* @param mixed $val The property (raw) value.
* @return self
* @deprecated
*/
final public function setVal($val)
{
Expand All @@ -302,11 +303,26 @@ final public function setVal($val)
return $this;
}

/**
* Clear the property's value.
*
* @deprecated
*
* @return self
*/
final public function clearVal()
{
$this->val = null;

return $this;
}

/**
* Retrieve the property's value.
*
* @return mixed
* @deprecated
*
* @return mixed
*/
final public function val()
{
Expand Down Expand Up @@ -840,7 +856,7 @@ public function validationMethods()
return [
'required',
'unique',
'allowNull'
'allowNull',
];
}

Expand Down Expand Up @@ -885,8 +901,6 @@ public function validateAllowNull()
return true;
}



/**
* @param mixed $val The value, at time of saving.
* @return mixed
Expand Down
3 changes: 2 additions & 1 deletion src/Charcoal/Property/EmailProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public function getMaxLength()
public function validationMethods()
{
$parentMethods = parent::validationMethods();

return array_merge($parentMethods, [
'email'
'email',
]);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Charcoal/Property/FileProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ public function validationMethods()
{
$parentMethods = parent::validationMethods();

return array_merge($parentMethods, [ 'accepted_mimetypes', 'max_filesize' ]);
return array_merge($parentMethods, [
'acceptedMimetypes',
'maxFilesize',
]);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Charcoal/Property/IdProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ public function save($val)
return $val;
}

/**
* @return boolean
*/
public function validateRequired()
{
$mode = $this->getMode();

if ($mode !== self::MODE_AUTO_INCREMENT) {
return parent::validateRequired();
}

return true;
}

/**
* Auto-generate a value upon first save.
*
Expand Down
23 changes: 14 additions & 9 deletions src/Charcoal/Property/PropertyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

// From 'charcoal-core'
use Charcoal\Validator\AbstractValidator;
use Charcoal\Validator\ValidatableInterface;

/**
*
* Property Validator
*/
class PropertyValidator extends AbstractValidator
{
Expand All @@ -15,17 +16,21 @@ class PropertyValidator extends AbstractValidator
*/
public function validate()
{
$result = true;

// The model, in this case, should be a PropertyInterface
$model = $this->model;
$property = $this->model;

$methods = $property->validationMethods();
foreach ($methods as $method) {
$method = $this->camelize($method);

$ret = true;
$validationMethods = $model->validationMethods();
foreach ($validationMethods as $m) {
$fn = [$model, 'validate_'.$m];
if (is_callable($fn)) {
$ret = $ret && call_user_func($fn);
$func = [ $property, 'validate'.ucfirst($method) ];
if (is_callable($func)) {
$result = $result && call_user_func($func);
}
}
return $ret;

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Charcoal/Property/StringProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public function validationMethods()
'maxLength',
'minLength',
'regexp',
'allowEmpty'
'allowEmpty',
]);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Charcoal/Property/FilePropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public function testVaidationMethods()
{
$obj = $this->obj;
$ret = $obj->validationMethods();
$this->assertContains('accepted_mimetypes', $ret);
$this->assertContains('max_filesize', $ret);
$this->assertContains('acceptedMimetypes', $ret);
$this->assertContains('maxFilesize', $ret);
}

/**
Expand Down

0 comments on commit 8b4510e

Please sign in to comment.