diff --git a/application/classes/Ushahidi/Repository/Form/Stage.php b/application/classes/Ushahidi/Repository/Form/Stage.php index b29b1c1a1f..b56bd74824 100644 --- a/application/classes/Ushahidi/Repository/Form/Stage.php +++ b/application/classes/Ushahidi/Repository/Form/Stage.php @@ -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' + ])); + } } diff --git a/application/classes/Ushahidi/Validator/Post/Create.php b/application/classes/Ushahidi/Validator/Post/Create.php index 84c5d25fee..faa4fa58ac 100644 --- a/application/classes/Ushahidi/Validator/Post/Create.php +++ b/application/classes/Ushahidi/Validator/Post/Create.php @@ -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']], @@ -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'])) { @@ -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]); } } } diff --git a/application/messages/post.php b/application/messages/post.php index 8a649ebd21..6402ffb78c 100644 --- a/application/messages/post.php +++ b/application/messages/post.php @@ -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', diff --git a/src/Core/Entity/FormStageRepository.php b/src/Core/Entity/FormStageRepository.php index 7f34e08a79..38e6a34e9d 100644 --- a/src/Core/Entity/FormStageRepository.php +++ b/src/Core/Entity/FormStageRepository.php @@ -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); } diff --git a/tests/datasets/ushahidi/Base.yml b/tests/datasets/ushahidi/Base.yml index 1ba32c5778..5e6b88f6f3 100644 --- a/tests/datasets/ushahidi/Base.yml +++ b/tests/datasets/ushahidi/Base.yml @@ -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: -