Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/Form/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ public static function create()
return new self;
}

/**
* @return Form
*/
public function checkRules()
{
parent::checkRules();

foreach ($this->getPrimitiveList() as $name => $primitive) {
if ($primitive instanceof PrimitiveForm) {
$error = $this->getError($name);
$validated = $primitive->validate();
if (!$error && !$validated) {
$this->markWrong($name);
} elseif ($error == Form::WRONG && $validated) {
$this->markGood($name);
}
}
}

return $this;
}

public function getErrors()
{
return array_merge($this->errors, $this->violated);
Expand Down
65 changes: 60 additions & 5 deletions core/Form/Primitives/PrimitiveForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ class PrimitiveForm extends BasePrimitive
{
protected $proto = null;

protected $needValidate = false;

private $composite = false;

/**
* @param bool $needValidate
* @return PrimitiveForm
*/
public function setNeedValidate($needValidate)
{
$this->needValidate = ($needValidate == true);
return $this;
}

/**
* @throws WrongArgumentException
* @return PrimitiveForm
Expand Down Expand Up @@ -80,6 +92,15 @@ public function getProto()
return $this->proto;
}

public function validate()
{
$result = true;
if ($this->needValidate && $this->value) {
$result = $this->valiadateForm($this->value);
}
return $result;
}

/**
* @throws WrongArgumentException
* @return PrimitiveForm
Expand All @@ -91,6 +112,18 @@ public function setValue($value)
return parent::setValue($value);
}

public function clean()
{
if (!$this->composite)
return parent::clean();

$this->raw = null;
$this->imported = false;
if ($this->value) {
$this->value->clean()->dropAllErrors();
}
}

/**
* @throws WrongArgumentException
* @return PrimitiveForm
Expand All @@ -116,7 +149,8 @@ public function exportValue()
if (!$this->value)
return null;

return $this->value->export();
$default = $this->composite && $this->imported ? array() : null;
return $this->value->export() ?: $default;
}

public function getInnerErrors()
Expand All @@ -139,10 +173,7 @@ public function unfilteredImport($scope)

private function actualImport($scope, $importFiltering)
{
if (!$this->proto)
throw new WrongStateException(
"no proto defined for PrimitiveForm '{$this->name}'"
);
$this->assertSettuped();

if (!isset($scope[$this->name]))
return null;
Expand All @@ -168,5 +199,29 @@ private function actualImport($scope, $importFiltering)

return true;
}

protected final function valiadateForm(Form $form)
{
if ($this->proto instanceof EntityProto) {
return $this->proto->validate(null, $form);
} else {
return !$form->checkRules()->getErrors();
}
}

protected function assertSettuped()
{
if ($this->composite) {
if (!$this->value)
throw new WrongStateException(
"setValue(Form) first if you choosed composite PrimitiveForm '{$this->name}'"
);
} else {
if (!$this->proto)
throw new WrongStateException(
"no proto defined for PrimitiveForm '{$this->name}'"
);
}
}
}
?>
12 changes: 12 additions & 0 deletions core/Form/Primitives/PrimitiveFormsList.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public function setComposite($composite = true)
);
}

public function validate()
{
$result = true;
if ($this->needValidate && $this->value) {
foreach ($this->value as $form) {
//every form must be validated, do not do 'continue' here if false result
$result = $result && $this->valiadateForm($form);
}
}
return $result;
}

public function getInnerErrors()
{
$result = array();
Expand Down
55 changes: 38 additions & 17 deletions main/EntityProto/EntityProto.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function checkConstraints(
return $this;
}

public function checkPostConstraints(
$object, Form $form, $previousObject = null
)
{
return $this;
}

public function isAbstract()
{
return false;
Expand All @@ -66,14 +73,16 @@ final public function validate(
$object, $form, $previousObject = null
)
{
if (is_array($object)) {
if (($object !== null && is_array($object)) || ($form !== null && is_array($form))) {
return $this->validateList($object, $form, $previousObject);
}

Assert::isInstance($object, $this->className());
if ($object && $this->className()) {
Assert::isInstance($object, $this->className());
}
Assert::isInstance($form, 'Form');

if ($previousObject)
if ($previousObject && $this->className())
Assert::isInstance($previousObject, $this->className());

if ($this->baseProto())
Expand All @@ -88,31 +97,29 @@ final public function validateSelf(
)
{
$this->checkConstraints($object, $form, $previousObject);

$getter = new ObjectGetter($this, $object);
$getter = $object
? $this->getValidateObjectGetter($object)
: null;

$previousGetter = $previousObject
? new ObjectGetter($this, $previousObject)
? $this->getValidateObjectGetter($previousObject)
: null;

foreach ($this->getFormMapping() as $id => $primitive) {
foreach ($this->getFormMapping() as $primitiveName => $primitive) {

if ($primitive instanceof PrimitiveForm) {
$proto = $primitive->getProto();

$childForm = $form->getValue($primitive->getName());

$child = $getter->get($id);
$child = $getter ? $getter->get($primitiveName) : null;

$previousChild = $previousGetter
? $previousGetter->get($id)
? $previousGetter->get($primitiveName)
: null;

$childResult = true;

if (
$child
&& !$proto->validate(
!$proto->validate(
$child, $childForm, $previousChild
)
) {
Expand All @@ -121,6 +128,8 @@ final public function validateSelf(
}
}

$this->checkPostConstraints($object, $form, $previousObject);

$errors = $form->getErrors();

return empty($errors);
Expand All @@ -130,7 +139,8 @@ final public function validateList(
$objectsList, $formsList, $previousObjectsList = null
)
{
Assert::isEqual(count($objectsList), count($formsList));
if ($objectsList !== null)
Assert::isEqual(count($objectsList), count($formsList));

reset($formsList);

Expand All @@ -145,11 +155,14 @@ final public function validateList(
$result = true;

$previousObject = null;
$object = null;

foreach ($objectsList as $object) {
foreach ($formsList as $form) {

$form = current($formsList);
next($formsList);
if ($objectsList) {
$object = current($objectsList);
next($formsList);
}

if ($previousObjectsList) {
$previousObject = current($previousObjectsList);
Expand Down Expand Up @@ -224,5 +237,13 @@ final public function getPrimitive($name)

return $result;
}

/**
* @param any $object
* @return ObjectGetter
*/
protected function getValidateObjectGetter($object) {
return new ObjectGetter($this, $object);
}
}
?>
Loading