Included form validations to publish form

This commit is contained in:
jostheta 2024-07-19 08:40:38 +05:30
parent a259008a9a
commit 2ed6574915
5 changed files with 87 additions and 10 deletions

View File

@ -146,6 +146,35 @@ class Forms extends CI_Controller
}
public function publish_form($form_id) {
// Load form and questions data
$form = $this->Form_model->get_form_by_id($form_id);
$questions = $this->Form_model->get_questions_by_form_id($form_id);
// Validation checks
if (empty($form->title)) {
$this->session->set_flashdata('error', 'Form title cannot be empty.');
redirect('forms/preview/' . $form_id);
return;
}
foreach ($questions as $question) {
if (empty($question->question_text)) {
$this->session->set_flashdata('error', 'All questions must have text.');
redirect('forms/preview/' . $form_id);
return;
}
// Check if question type is multiple-choice or checkbox
if (in_array($question->question_type, ['multiple-choice', 'checkbox'])) {
$options = $this->Form_model->get_options_by_question_id($question->question_id);
if (empty($options)) {
$this->session->set_flashdata('error', 'Questions of type multiple-choice or checkbox must have at least one option.');
redirect('forms/preview/' . $form_id);
return;
}
}
}
// Generate a unique link
$response_link = base_url("forms/respond/" . $form_id);
@ -159,6 +188,10 @@ class Forms extends CI_Controller
redirect('forms/list_user_published_forms');
}
public function respond($form_id) {
// Check if user is logged in
if (!$this->session->userdata('user_id')) {

View File

@ -14,7 +14,11 @@
<?php foreach ($forms as $form) : ?>
<?php if ($form->is_published == 0) : ?>
<tr>
<td><a href="<?= base_url() ?>forms/view_form/<?= $form->form_id ?>"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td>
<td>
<a href="<?= base_url() ?>forms/preview/<?=$form->form_id?>">
<?= htmlspecialchars($form->title ? $form->title : $form->form_id, ENT_QUOTES, 'UTF-8') ?>
</a>
</td>
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
<td><a href="<?= base_url() ?>forms/delete/<?= $form->form_id ?>" onclick="return confirm('Are you sure you want to delete this form?');">Delete</a></td>
</tr>

View File

@ -7,6 +7,12 @@
<div class="form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></div>
</div>
<?php if ($this->session->flashdata('error')): ?>
<div class="error-message" style="color: red;">
<?= $this->session->flashdata('error') ?>
</div>
<?php endif; ?>
<div id="questions-container">
<?php if (!empty($questions)) : ?>
<?php foreach ($questions as $index => $question) : ?>
@ -34,6 +40,8 @@
</div>
<br>
<?php endforeach; ?>
<?php else: ?>
<p>No options found for this question.</p>
<?php endif; ?>
</div>
<?php endif; ?>

View File

@ -295,6 +295,7 @@ tr:nth-child(even) {
padding: 10px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px;
border-radius: 5px;
overflow-y: auto;
}
.submit-button{
@ -316,6 +317,11 @@ tr:nth-child(even) {
background-color: white;
}
.question-box.active {
border-left: 6px solid #1a73e8;
}

View File

@ -16,6 +16,9 @@ $(document).ready(function() {
// Append the cloned question to the form container
$('#question-template').parent().append(newQuestion);
// Scroll to the newly added question and set it as active
setActiveQuestion(newQuestion);
});
// Add new option to a question
@ -93,6 +96,26 @@ $(document).ready(function() {
questionBox.attr('data-question-type', selectedType);
}).trigger('change');
// Function to set the active question and scroll the sidebar
function setActiveQuestion(questionBox) {
// Remove active class from all question boxes
$('.question-box').removeClass('active');
// Add active class to the clicked question box
questionBox.addClass('active');
// Scroll sidebar to the active question
var offset = questionBox.offset().top - $('.sidebar').offset().top;
$('.sidebar').animate({
scrollTop: offset + $('.sidebar').scrollTop()
}, 500);
}
// Add click event listener to all question boxes to set active question
$(document).on('click', '.question-box', function() {
setActiveQuestion($(this));
});
// Submit form
$('#submit-form').click(function() {
var formData = {
@ -136,6 +159,9 @@ $(document).ready(function() {
}
});
});
});
$(document).ready(function() {
$('#update-form').click(function() {
@ -221,4 +247,4 @@ $(document).ready(function() {
});
});