Skip to content

Commit

Permalink
Merge pull request #2 from locomotivemtl/joel-fix-conditional-logic
Browse files Browse the repository at this point in the history
Add ConditionalLogic related improvements to FormTrait and FormGroupT…
  • Loading branch information
Joel Alphonso authored Mar 27, 2019
2 parents d5d28af + 0058bfa commit 4cbc28a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Charcoal/Ui/Form/FormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,39 @@ protected function finalizeFormGroups($groups)
}
}

if ($group->rawConditionalLogic()) {
if (is_callable([$this, 'obj'])) {
foreach ($group->rawConditionalLogic() as $logic) {
$valid = true;
$value = $this->obj()->get($logic['property']);

switch ($logic['operator']) {
case '!==':
case '!=':
case '!':
case 'not':
if ($value === $logic['value']) {
$valid = false;
}
break;
default:
case '"==="':
case '"=="':
case '"="':
case '"is"':
if ($value !== $logic['value']) {
$valid = false;
}
break;
}

if (!$valid) {
$group->setConditionalLogicUnmet(true);
}
}
}
}

$out[] = $group;
}

Expand Down
39 changes: 39 additions & 0 deletions src/Charcoal/Ui/FormGroup/FormGroupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ trait FormGroupTrait
*/
private $conditionalLogic;

/**
* @var array|null
*/
private $rawConditionalLogic;

/**
* @var boolean
*/
private $conditionalLogicUnmet;

/**
* Comparison function used by {@see uasort()}.
*
Expand Down Expand Up @@ -319,12 +329,22 @@ public function conditionalLogic()
return $this->conditionalLogic;
}

/**
* @return array|null
*/
public function rawConditionalLogic()
{
return $this->rawConditionalLogic;
}

/**
* @param array|null $conditionalLogic ConditionalLogic for FormGroupWidget.
* @return self
*/
public function setConditionalLogic($conditionalLogic)
{
$this->rawConditionalLogic = $conditionalLogic;

if ($conditionalLogic && is_array($conditionalLogic)) {
foreach ($conditionalLogic as &$condition) {
$prop = $this->form()->formProperty($condition['property']);
Expand All @@ -338,4 +358,23 @@ public function setConditionalLogic($conditionalLogic)

return $this;
}

/**
* @return boolean
*/
public function conditionalLogicUnmet()
{
return $this->conditionalLogicUnmet;
}

/**
* @param boolean $conditionalLogicUnmet ConditionalLogicUnmet for FormGroupTrait.
* @return self
*/
public function setConditionalLogicUnmet($conditionalLogicUnmet)
{
$this->conditionalLogicUnmet = $conditionalLogicUnmet;

return $this;
}
}

0 comments on commit 4cbc28a

Please sign in to comment.