Skip to content

Commit

Permalink
feat(form): automatically create a section when creating a form
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 12, 2022
1 parent dc7ad7b commit e5577ed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions front/form.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
if (isset($_POST['add'])) {
// Add a new Form
Session::checkRight('entity', UPDATE);
$_POST['_create_empty_section'] = true;
$newID = $form->add($_POST);
Html::redirect(FORMCREATOR_ROOTDOC . '/front/form.form.php?id=' . $newID);

Expand Down
7 changes: 7 additions & 0 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,13 @@ public function prepareInputForAdd($input) {
* @return void
*/
public function post_addItem() {
if (isset($this->input['_create_empty_section'])) {
$section = new PluginFormcreatorSection();
$section->add([
self::getForeignKeyField() => $this->getID(),
'name' => PluginFormcreatorSection::getTypeName(1),
]);
}
$this->updateValidators();
if ($this->input['show_rule'] != PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
$this->updateConditions($this->input);
Expand Down
18 changes: 18 additions & 0 deletions tests/3-unit/PluginFormcreatorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,4 +1277,22 @@ public function testCheckImportVersion($version, $expected) {
$output = \PluginFormcreatorForm::checkImportVersion($version);
$this->boolean($output)->isEqualTo($expected);
}

public function testAdd() {
$instance = $this->newTestedInstance();
$instance->add([
'_create_empty_section' => true,
'name' => 'form with auto created section'
]);

$this->boolean($instance->isNewItem())->isFalse();
$section = new \PluginFormcreatorSection();
$rows = $section->find([
'plugin_formcreator_forms_id' => $instance->getID(),
]);

$this->array($rows)->hasSize(1);
$row = array_shift($rows);
$this->string($row['name'])->isEqualTo(\PluginFormcreatorSection::getTypeName(1));
}
}

0 comments on commit e5577ed

Please sign in to comment.