Skip to content

Commit 84ac13c

Browse files
committed
Always validate required post fields
Always validated required post fields, regardless if the 'post' stage is complete or not
1 parent 3dae2c2 commit 84ac13c

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

application/classes/Ushahidi/Repository/Form/Stage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,13 @@ public function getRequired($form_id)
194194

195195
return $this->getCollection($results->as_array());
196196
}
197+
198+
// FormStageRepository
199+
public function getPostStage($form_id)
200+
{
201+
return $this->getEntity($this->selectOne([
202+
'form_stages.form_id' => $form_id,
203+
'form_stages.type' => 'post'
204+
]));
205+
}
197206
}

application/classes/Ushahidi/Validator/Post/Create.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ protected function getRules()
119119
],
120120
'values' => [
121121
[[$this, 'checkValues'], [':validation', ':value', ':fulldata']],
122-
[[$this, 'checkRequiredAttributes'], [':validation', ':value', ':fulldata']],
122+
[[$this, 'checkRequiredPostAttributes'], [':validation', ':value', ':fulldata']],
123+
[[$this, 'checkRequiredTaskAttributes'], [':validation', ':value', ':fulldata']],
123124
],
124125
'post_date' => [
125126
[[$this, 'validDate'], [':value']],
@@ -333,7 +334,33 @@ public function checkRequiredStages(Validation $validation, $fullData)
333334
* @param Array $attributes
334335
* @param Array $fullData
335336
*/
336-
public function checkRequiredAttributes(Validation $validation, $attributes, $fullData)
337+
public function checkRequiredPostAttributes(Validation $validation, $attributes, $fullData)
338+
{
339+
// Get the post stage
340+
$stage = $this->stage_repo->getPostStage($fullData['form_id']);
341+
342+
// Load the required attributes
343+
$required_attributes = $this->attribute_repo->getRequired($stage->id);
344+
345+
// Check each attribute has been completed
346+
foreach ($required_attributes as $attr)
347+
{
348+
if (!array_key_exists($attr->key, $attributes))
349+
{
350+
// If a required attribute isn't completed, throw an error
351+
$validation->error('values', 'postAttributeRequired', [$attr->label, $stage->label]);
352+
}
353+
}
354+
}
355+
356+
/**
357+
* Check required attributes are completed before completing stages
358+
*
359+
* @param Validation $validation
360+
* @param Array $attributes
361+
* @param Array $fullData
362+
*/
363+
public function checkRequiredTaskAttributes(Validation $validation, $attributes, $fullData)
337364
{
338365
if (empty($fullData['completed_stages']))
339366
{
@@ -354,7 +381,7 @@ public function checkRequiredAttributes(Validation $validation, $attributes, $fu
354381
{
355382
$stage = $this->stage_repo->get($stage_id);
356383
// If a required attribute isn't completed, throw an error
357-
$validation->error('values', 'attributeRequired', [$attr->label, $stage->label]);
384+
$validation->error('values', 'taskAttributeRequired', [$attr->label, $stage->label]);
358385
}
359386
}
360387
}

application/messages/post.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
'tooManyValues' => 'Too many values for :param1 (max: :param2)',
88
'valueDoesNotExist' => 'value id :param2 for field :param1 does not exist',
99
'canNotUseExistingValueOnNewPost' => 'Cannot use existing value :param2 for field :param1 on a new post',
10-
'attributeRequired' => 'attribute :param1 is required before stage ":param2" can be completed',
10+
'postAttributeRequired' => 'attribute :param1 is required',
11+
'taskAttributeRequired' => 'attribute :param1 is required before stage ":param2" can be completed',
1112
'emptyIdAndLocale' => 'Must have at least id or locale',
1213
'emptyParentWithLocale' => 'Must have at parent id when passing locale',
1314
'notAnArray' => 'Post values for :param1 must be an array',

src/Core/Entity/FormStageRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ public function getByForm($form_id);
3333
public function existsInForm($id, $form_id);
3434

3535
/**
36+
* Get required stages for form
37+
*
3638
* @param int $form_id
3739
* @return [Ushahidi\Core\Entity\FormAttribute, ...]
3840
*/
3941
public function getRequired($form_id);
42+
43+
/**
44+
* Get 'post' type stage for form
45+
*
46+
* @param int $form_id
47+
* @return Ushahidi\Core\Entity\FormAttribute
48+
*/
49+
public function getPostStage($form_id);
4050
}

tests/datasets/ushahidi/Base.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@ post_varchar:
974974
post_id: 122
975975
form_attribute_id: 24
976976
value: "arabic string"
977+
-
978+
id: 23
979+
post_id: 105
980+
form_attribute_id: 7
981+
value: "Atlantis"
977982
post_comments:
978983
tags:
979984
-

0 commit comments

Comments
 (0)