0

Registration Form Consists multiple ContactForm and a FeeForm , i try to validate FeeForm,ContactForms existed in Registration Form,Validations are failed and it didn't display any Validation Errors ,For empty Fields of FeeForm, Contact Forms

forms.py

class ContactForm(forms.Form):
    address = forms.CharField(widget = forms.Textarea(attrs = {'rows' : '5'}),required = False)
    phone = forms.CharField(required = True)
    email = forms.EmailField(required = False)

class FeeForm(forms.Form):
    name_of_bank = forms.CharField(required = True)
    dd_number = forms.CharField(required = True)
    date= forms.DateField(required = True)
    amount = forms.CharField(required=True)
    def clean_amount(self):
        amount = self.cleaned_data['amount']
        if amount == '':
            self._errors["amount"] = ["Please Enter Amount"]
        return amount 

class RegistrationForm(forms.Form):
    hall_ticket_number = forms.CharField(required = False)
    name = forms.CharField(required=True)
    religion = forms.CharField(required = False)
    present_contact = ContactForm()
    gender  = forms.ChoiceField( choices = ( ('M', 'Male'), ('F', 'Female')),required = True)
    organization_contact = ContactForm()
    fee_details = FeeForm()

    def clean_hall_ticket_number(self):
        hall = self.cleaned_data['hall_ticket_number']
        if hall == '':
            self._errors["hall_ticket_number"] = ["Hall Ticket Number is NOt Present"]

        return hall 

    def clean_fee_detaills(self):
        amount = self.cleaned_data['amount']
        if amount == "":
            self._errors["hall_ticket_number"] = ["Hall Ticket Number is NOt Present"]
            self._errors["amount"] = ["Hall Ticket Number is NOt Present"]

        return amount 



       '''def clean(self):
        clean_data=super(RegistrationForm, self).clean()
        amount = self.cleaned_data['amount']
        if amount == "":
            self._errors["amount"] = ["Please Enter Amount"]
        return amount 
'''

Views.py

initial_values = {
    'name' : student.name,
    'hall_ticket_number' : student.hall_ticket_no,
    'gender' : student.sex,
    'religion' : student.religion,

    'present_contact' : {
        'address' : student.present_contact.address,
        'phone' : student.present_contact.phone_no,
        'email' : student.present_contact.email,
    },

    'organization_contact' : {
        'address' : student.original_contact.address,
        'phone' : student.original_contact.phone_no,
        'email' : student.original_contact.email,
    },

}
registration_status = False
if request.method == 'POST':
    form  = RegistrationForm(request.POST)
    #Checking Form is Valid or not
    if form.is_valid():
        text = register(request,student_id) # Processing of Register Page in registartion_contrib
        return redirect('/registration/{student_id}/register'.format(**locals()))
else:
    registrations = Registration.objects.filter(student = student)
    f_initial_values = None
    # Checking Registration Status 
    if len(registrations) > 0:
        registration = registrations[0]
        if registration.status == 'Done':
            registration_status = True
    # Feedetails of Student 
            feedetails = FeeDetail.objects.filter(student = student)
    if len(feedetails) > 0:
        feedetail = feedetails[0] 
    # Initializing the FeeDetails for Form 
        f_initial_values = {}
        f_initial_values['name_of_bank'] = feedetail.name_of_bank 
        f_initial_values['dd_number'] = feedetail.dd_number
        f_initial_values['amount'] = feedetail.amount 
        f_initial_values['date'] =feedetail.fee_date.strftime("%Y-%m-%d")   
    # Specifing the initial values for Forms    
    form = RegistrationForm(initial = initial_values, )
    if f_initial_values != None:
        form.fee_details = FeeForm(initial = f_initial_values)

     # Initializing Contact Forms PRESENT, Organization, Supervisor and Co Supervisor
    form.present_contact = ContactForm(initial = initial_values['present_contact'], prefix="present_contact")
form.organization_contact = ContactForm(initial = initial_values['organization_contact'], prefix="organization_contact")
form.supervisor_contact = ContactForm( initial = {
            "address" : student.guide.contact.address,
            "phone" : student.guide.contact.phone_no,
            "email" : student.guide.contact.email,
        }, prefix = "supervisor_contact" )
form.co_supervisor_contact = ContactForm( initial = {
            "address" : student.co_guide.contact.address,
            "phone" : student.co_guide.contact.phone_no,
            "email" : student.co_guide.contact.email,
        }, prefix = "co_supervisor_contact" )


return render(request, 'registration/register.html', {'student' : student, 'form' : form, 'registration_status' : registration_status,'registration':registration })

template.html

         Forms fields are specified as follows
            {{ form.hall_ticket_number|materializecss}}
            {{ form.name|materializecss}}
            {{ form.religion|materializecss}}
            {{ form.gender|materializecss}}

            {{ form.orgnization_contact.address}}
            {{ form.orgnization_contact.phone}}
            {{ form.orgnization_contact.email}}

            {{ form.present_contact.address}}
            {{ form.present_contact.phone}}
            {{ form.present_contact.email}}



          <div class="col s12 m12 l12">
                {{ form.fee_details.name_of_bank|materializecss  }}
                    </div>          
          <div class="col s12 m12 l12">
                    {{ form.fee_details.dd_number.errors }}

                {{ form.fee_details.dd_number|materializecss }}
                </div> 
          <div class="col s12 m12 l12">
                    {{ form.fee_details.date.errors }}

                {{ form.fee_details.date|materializecss}}
                </div>                  
          <div class="col s12 m12 l12">
                    {{ form.errors }}


              <br>{{ form.fee_details.amount|materializecss  }}
                </div>                  
            </div>

1 Answer 1

1

Every individual form needs its own form.is_valid() to be called to get its error fields in the template. In your post method there is only one form.is_valid() called, which is for RegistrationForm only.Better send/receive three form independently. In the template, use them under one form. Things could've been done this way:

views.py

if request.method == 'POST':
    reg_form  = RegistrationForm(request.POST)
    contact_form  = ContactForm(request.POST)
    free_form = FeeForm(request.POST)

    reg_valid = reg_form.is_valid()
    contact_valid = contact_form.is_valid()
    free_valid = free_form .is_valid()

    if reg_valid and contact_valid and free_valid:
         #Everything is fine, do the registration and go to success page
    else:
         #error happened, so go to form page with error fields
         return render(request, 'registration/register.html', {'student' : student, 'reg_form' : reg_form,'contact_form' : contact_form, 'free_form' : free_form,'registration_status' : registration_status,'registration':registration }) 

else:
    reg_form  = RegistrationForm(initials=...)
    contact_form  = ContactForm(initials=...)
    free_form = FeeForm(initials=...) 

    return render(request, 'registration/register.html', {'student' : student, 'reg_form' : reg_form,'contact_form' : contact_form, 'free_form' : free_form,'registration_status' : registration_status,'registration':registration })   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.