Skip to content

Commit

Permalink
added validation of custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
David Zaremba committed Aug 27, 2020
1 parent c7c626a commit 583d3fa
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions plugins/workflow/fields/fields.php
Expand Up @@ -141,20 +141,16 @@ public function onWorkflowBeforeTransition(WorkflowTransitionEvent $event)
$pks = $event->getArgument('pks');

//get Values from Form
$reqired = $transition->options->get('required');
$blankreqired = $transition->options->get('notrequired');
$contains = $transition->options->get('contains');
$containsNot = $transition->options->get('containsNot');
$values = $transition->options->toArray();


if (!$this->isSupported($context)
/*if (!$this->isSupported($context)
||!is_numeric($reqired)
||!is_numeric($blankreqired)
||!is_numeric($contains)
||!is_numeric($containsNot))
{
return true;
}
}*/

$component = $this->app->bootComponent($extensionName);

Expand All @@ -170,13 +166,14 @@ public function onWorkflowBeforeTransition(WorkflowTransitionEvent $event)

$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), $options);



foreach ($pks as $pk)
{



$articleItem = $model->getItem($pk);
$this->app->triggerEvent('onContentPrepare', [$context,$articleItem]);
if(!$this->validate($articleItem->jcfields, $values['subform'])){
$event->setStopTransition();
return false;
}
}

if (!$this->isSupported($context))
Expand All @@ -186,6 +183,48 @@ public function onWorkflowBeforeTransition(WorkflowTransitionEvent $event)
return true;
}

/**
* Validates the Custom Fields in an Article
*
* @param $fieldvalues values of Custom Fields
* @param $formvalues values of transition settings
* @since 4.1.0
*/

private function validate($fieldvalues, $formvalues ){
foreach ($formvalues as $formvalue){
$id = $formvalue['customfield'];

if($formvalue['required']){
if(!$fieldvalues[$id]->rawvalue){
Factory::getApplication()->enqueueMessage(Text::_('Required Field '.$fieldvalues[$id]->title.' is empty'));
return false;
}
}
if($formvalue['notrequired']){
if($fieldvalues[$id]->rawvalue){
Factory::getApplication()->enqueueMessage(Text::_('The Field '.$fieldvalues[$id]->title.' , which has to be blank, is filled'));
return false;
}
}
if($formvalue['contains']){
if(strpos($fieldvalues[$id]->rawvalue,$formvalue['contains'])==false){
Factory::getApplication()->enqueueMessage(Text::_('The Field '.$fieldvalues[$id]->title.' , does not contain '.$formvalue['contains']));
return false;
}
}
if($formvalue['containsNot']){
if(strpos($fieldvalues[$id]->rawvalue,$formvalue['containsNot'])!=false){
Factory::getApplication()->enqueueMessage(Text::_('The Field '.$fieldvalues[$id]->title.' contains '.$formvalue['containsNot'].' but should not contain this value'));
return false;
}
}
}

return true;

}

/**
* Change State of an item. Used to disable state change
*
Expand Down

0 comments on commit 583d3fa

Please sign in to comment.