Skip to content

Commit

Permalink
Always validate required post fields
Browse files Browse the repository at this point in the history
Always validated required post fields, regardless if the 'post' stage
is complete or not
  • Loading branch information
rjmackay committed Aug 18, 2017
1 parent 3dae2c2 commit 84ac13c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
9 changes: 9 additions & 0 deletions application/classes/Ushahidi/Repository/Form/Stage.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,13 @@ public function getRequired($form_id)

return $this->getCollection($results->as_array());
}

// FormStageRepository
public function getPostStage($form_id)
{
return $this->getEntity($this->selectOne([
'form_stages.form_id' => $form_id,
'form_stages.type' => 'post'
]));
}
}
33 changes: 30 additions & 3 deletions application/classes/Ushahidi/Validator/Post/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ protected function getRules()
],
'values' => [
[[$this, 'checkValues'], [':validation', ':value', ':fulldata']],
[[$this, 'checkRequiredAttributes'], [':validation', ':value', ':fulldata']],
[[$this, 'checkRequiredPostAttributes'], [':validation', ':value', ':fulldata']],
[[$this, 'checkRequiredTaskAttributes'], [':validation', ':value', ':fulldata']],
],
'post_date' => [
[[$this, 'validDate'], [':value']],
Expand Down Expand Up @@ -333,7 +334,33 @@ public function checkRequiredStages(Validation $validation, $fullData)
* @param Array $attributes
* @param Array $fullData
*/
public function checkRequiredAttributes(Validation $validation, $attributes, $fullData)
public function checkRequiredPostAttributes(Validation $validation, $attributes, $fullData)
{
// Get the post stage
$stage = $this->stage_repo->getPostStage($fullData['form_id']);

// Load the required attributes
$required_attributes = $this->attribute_repo->getRequired($stage->id);

// Check each attribute has been completed
foreach ($required_attributes as $attr)
{
if (!array_key_exists($attr->key, $attributes))
{
// If a required attribute isn't completed, throw an error
$validation->error('values', 'postAttributeRequired', [$attr->label, $stage->label]);
}
}
}

/**
* Check required attributes are completed before completing stages
*
* @param Validation $validation
* @param Array $attributes
* @param Array $fullData
*/
public function checkRequiredTaskAttributes(Validation $validation, $attributes, $fullData)
{
if (empty($fullData['completed_stages']))
{
Expand All @@ -354,7 +381,7 @@ public function checkRequiredAttributes(Validation $validation, $attributes, $fu
{
$stage = $this->stage_repo->get($stage_id);
// If a required attribute isn't completed, throw an error
$validation->error('values', 'attributeRequired', [$attr->label, $stage->label]);
$validation->error('values', 'taskAttributeRequired', [$attr->label, $stage->label]);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion application/messages/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'tooManyValues' => 'Too many values for :param1 (max: :param2)',
'valueDoesNotExist' => 'value id :param2 for field :param1 does not exist',
'canNotUseExistingValueOnNewPost' => 'Cannot use existing value :param2 for field :param1 on a new post',
'attributeRequired' => 'attribute :param1 is required before stage ":param2" can be completed',
'postAttributeRequired' => 'attribute :param1 is required',
'taskAttributeRequired' => 'attribute :param1 is required before stage ":param2" can be completed',
'emptyIdAndLocale' => 'Must have at least id or locale',
'emptyParentWithLocale' => 'Must have at parent id when passing locale',
'notAnArray' => 'Post values for :param1 must be an array',
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Entity/FormStageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ public function getByForm($form_id);
public function existsInForm($id, $form_id);

/**
* Get required stages for form
*
* @param int $form_id
* @return [Ushahidi\Core\Entity\FormAttribute, ...]
*/
public function getRequired($form_id);

/**
* Get 'post' type stage for form
*
* @param int $form_id
* @return Ushahidi\Core\Entity\FormAttribute
*/
public function getPostStage($form_id);
}
5 changes: 5 additions & 0 deletions tests/datasets/ushahidi/Base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,11 @@ post_varchar:
post_id: 122
form_attribute_id: 24
value: "arabic string"
-
id: 23
post_id: 105
form_attribute_id: 7
value: "Atlantis"
post_comments:
tags:
-
Expand Down

0 comments on commit 84ac13c

Please sign in to comment.