[Django]-Django empty form field validation is not working with clean method

6👍

des never will equal None, the default value will at least be an empty string

if not des: should be enough to satify your if statement when des is empty

If all you really want to check is if its empty or not you can do that in the model by setting blank to False

description = models.CharField(max_length = 100, blank=False, verbose_name =' Descripcion')

If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.


Yeah, but isn’t validation error supossed to show the alert inside the form before user can proceed to save ??

Yes it is but you override the form before it is ever shown to the user, this can easily be solved by defining the forms at the top of your method then overriding them further down

   def post(self, request, *args, **kwargs):
        form_group = GroupsForm()
        form_subgroup= SubGroupsForm()
        form_cost_item = CostItemsForm()
        if request.user.has_perm('cost_control_app.add_costitems'):
            form_cost_item = CostItemsForm(request.POST)
            if form_cost_item.is_valid():
                form_save = form_cost_item.save(commit = False)
                form_save.save(force_insert = True) 
                messages.success(request, "Record created")
            else:
                messages.error(request, "Could not create record, please check your form")
        else:
            messages.error(request, "Permission denied")
        return render(request, self.template_name,{
                                    "form_subgroup":form_subgroup,
                                    "form_cost_item":form_cost_item,
                                    "form_group":form_group,
                                })  
👤Sayse

0👍

If you are trying to show the form errors in the template you should pass the errors to the template. In your case the view should be something like the following.

def post(self, request, *args, **kwargs):
    if request.user.has_perm('cost_control_app.add_costitems'):
        form_insert = CostItemsForm(request.POST)
        if form_insert.is_valid():
            form_save = form_insert.save(commit = False)
            form_save.save(force_insert = True) 
            messages.success(request, "Record created")
        else:
            messages.error(request, "Could not create record, please check your form")
            form_group = GroupsForm()
            form_subgroup= SubGroupsForm()
            return render(request, self.template_name,{
                            "form_subgroup":form_subgroup,
                            "form_cost_item":form_insert,
                            "form_group":form_group,
                        })   

    else:
        messages.error(request, "Permission denied")
    form_group = GroupsForm()
    form_subgroup= SubGroupsForm()
    form_cost_item = CostItemsForm()
    return render(request, self.template_name,{
                                "form_subgroup":form_subgroup,
                                "form_cost_item":form_cost_item,
                                "form_group":form_group,
                            })  

I can see that the fields in the model are required fields by default. So anyway it will raise validation error. All you need to do is to pass the error form to template if form is invalid as above.

Leave a comment