Skip to content

Commit 2ac537b

Browse files
ryanchristorjmackay
authored andcommitted
Fix validation of form name and description when empty
- Added "not_empty" validation rule to name - Always check validation for name - Added migration allowing null for description - Add a test for setting form.name to empty
1 parent 2fe2a73 commit 2ac537b

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ class Ushahidi_Validator_Form_Create extends Ushahidi_Validator_Form_Update
1818
protected function getRules()
1919
{
2020
return array_merge_recursive(parent::getRules(), [
21-
'name' => [['not_empty'],
22-
[[$this, 'checkPostTypeLimit'], [':validation']],
23-
]]);
21+
'name' => [
22+
[[$this, 'checkPostTypeLimit'], [':validation']],
23+
]
24+
]);
2425
}
2526
}

application/classes/Ushahidi/Validator/Form/Update.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@ public function __construct(FormRepository $repo)
3131

3232
protected function getRules()
3333
{
34+
// Always check validation for name
35+
$name = $this->validation_engine->getFullData('name');
36+
if ($name) {
37+
$data = $this->validation_engine->getData();
38+
$data['name'] = $name;
39+
$this->validation_engine->setData($data);
40+
}
41+
// End
42+
3443
return [
3544
'name' => [
45+
['not_empty'],
3646
['min_length', [':value', 2]],
3747
['regex', [':value', Validator::REGEX_STANDARD_TEXT]], // alpha, number, punctuation, space
3848
],
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Phinx\Migration\AbstractMigration;
4+
5+
class AllowNullInDescriptionForms extends AbstractMigration
6+
{
7+
/**
8+
* Migrate Up.
9+
*/
10+
public function up()
11+
{
12+
$this->table('forms')
13+
->changeColumn('description', 'text', ['null' => true])
14+
->save();
15+
}
16+
17+
/**
18+
* Migrate Down.
19+
*/
20+
public function down()
21+
{
22+
$this->table('forms')
23+
->changeColumn('description', 'text', ['null' => false])
24+
->save();
25+
}
26+
}

tests/integration/forms.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ Feature: Testing the Forms API
5252
And the "everyone_can_create" property is false
5353
Then the guzzle status code should be 200
5454

55+
Scenario: Updating a Form to clear name should fail
56+
Given that I want to update a "Form"
57+
And that the request "data" is:
58+
"""
59+
{
60+
"name":"",
61+
"type":"report",
62+
"description":"This is a test form updated by BDD testing",
63+
"disabled":true,
64+
"require_approval":false,
65+
"everyone_can_create":false
66+
}
67+
"""
68+
And that its "id" is "1"
69+
When I request "/forms"
70+
Then the response is JSON
71+
Then the guzzle status code should be 422
72+
5573
Scenario: Update a non-existent Form
5674
Given that I want to update a "Form"
5775
And that the request "data" is:

0 commit comments

Comments
 (0)