There are quite a few moving parts to this question, but if you have any insight to any piece of it, it would be appreciated.
I want to build a feedback form that acts as one would expect. When the user clicks the feedback button at the bottom right of the page, it launches a bootstrap modal. The modal has a django crispy form that submits or returns the fields that are invalid when the submit button is pressed.
First, I have my feedback button:
{% load crispy_forms_tags %}
.feedback-button {
position: fixed;
bottom: 0;
right: 30px;
}
<div class='feedback-button'>
<a class="btn btn-info" href="#feedbackModal" data-toggle="modal" title="Leave feedback" target="_blank">
<i class="icon-comment icon-white"></i>
Leave feedback
</a>
</div>
<div class="modal hide" id="feedbackModal" tabindex="-1" role="dialog" aria-labelledby="feedbackModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 id="feedbackModalLabel">Contact Form</h3>
</div>
<div class="modal-body">
{% crispy feedback_form feedback_form.helper %}
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Submit</button>
</div>
</div>
Next, I have my form:
class Feedback(models.Model):
creation_date = models.DateTimeField("Creation Date", default=datetime.now)
topic = models.CharField("Topic", choices = TOPIC_CHOICES, max_length=50)
subject = models.CharField("Subject", max_length=100)
message = models.TextField("Message", blank=True)
sender = models.CharField("Sender", max_length=50, blank=True, null=True)
def __unicode__(self):
return "%s - %s" % (self.subject, self.creation_date)
class Meta:
ordering = ["creation_date"]
verbose_name = "Feedback"
verbose_name_plural = "Feedback"
class Crispy_ContactForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
Field('topic', placeholder='Topic', css_class='input-medium'),
Field('subject', placeholder='Subject', css_class='input-xlarge'),
Field('message', placeholder='Message', rows='5', css_class='input-xlarge'),
Field('sender', placeholder='Sender', css_class='input-xlarge'),
),
)
self.helper.form_id = 'id-Crispy_ContactForm'
self.helper.form_method = 'post'
super(Crispy_ContactForm, self).__init__(*args, **kwargs)
class Meta:
model = Feedback
exclude = ['creation_date']
I tried to omit the legend in the crispy form because if I include it, the modal appears to have two form titles. But omitting the legend in the crispy form layout resulted in the fields appearing out of order.
So I'm left with a few questions:
- Overall, am I going about this the right way?
- If I hook up the modal's submit button to AJAX, how do I go about error checking the form?
- Is there a better way to display the crispy form in the bootstrap modal?