首页 > 解决方案 > 我的 EmployeeForm 类中的“Employee_Name”的自定义验证不起作用

问题描述

我制作了一个表格来创建员工详细信息。在 forms.py 中,我创建了 EmployeeForm 类,还添加了一个自定义验证来验证“Employee_Name”,但自定义验证“EmployeeNameValidate”不起作用。

表格.py

class EmployeeForm (forms.ModelForm):
    Employee_Name = forms.CharField(widget=forms.TextInput(attrs{
                              "placeholder" : "Enter the Employee Name "
                                }
                                 ))
    Employee_ID   = forms.CharField(initial = '17CS')
    Address       = forms.CharField(
            required = False , 
            widget = forms.Textarea
                                  (
            attrs={
                 "placeholder": "Enter your Address here ",
                 "class" : "new-class-name two",
                 "id" :"my-id-for-textarea",
                 "rows":10,
                 "cols":100
                }
                ) 
                                   )
    Salary  = forms.DecimalField(initial = 60000.24)

    class Meta:
        model  = Employee
        fields = [
        'Employee_Name',
        'Employee_ID',
        'Address',
        'Salary'
        ]

    #print("yaaah !!")

    def EmployeeNameValidate(self, *args , **kwargs):
        print("yaaah 22222 !!")
        Employee_Name = self.cleaned_data.get("Employee_Name")
        print(Employee_Name)
        if "abc" not in Employee_Name:
            raise forms.ValidationError ("This is not a valid Employee Name 
                                        ")
        return Employee_Name 

     #print("yaaah 3333333333!!")

视图.py

def emp_create_view(request):
    form = EmployeeForm(request.POST or None )
    if form.is_valid():
        form.save()
        form = EmployeeForm()
    context={
    'form': form
    }

    return render(request,"employee/emp_create.html",context)

emp_create.html

{% extends "base.html" %}
{% block content %}
<h1>
 Enter the details of the Employee :
</h1>
<form method = 'POST'> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
{% endblock %}

标签: pythondjangoformsvalidation

解决方案


您的验证方法必须名称为 clean_Employee_Name,因为您的字段名称为 Employee_Name

尝试这个

class EmployeeForm (forms.ModelForm):
    Employee_Name = forms.CharField(widget=forms.TextInput(attrs{
                              "placeholder" : "Enter the Employee Name "
                                }
                                 ))
    Employee_ID   = forms.CharField(initial = '17CS')
    Address       = forms.CharField(
            required = False , 
            widget = forms.Textarea
                                  (
            attrs={
                 "placeholder": "Enter your Address here ",
                 "class" : "new-class-name two",
                 "id" :"my-id-for-textarea",
                 "rows":10,
                 "cols":100
                }
                ) 
                                   )
    Salary  = forms.DecimalField(initial = 60000.24)

    class Meta:
        model  = Employee
        fields = [
        'Employee_Name',
        'Employee_ID',
        'Address',
        'Salary'
        ]

    #print("yaaah !!")

    def clean_Employee_Name(self, *args , **kwargs):
        print("yaaah 22222 !!")
        Employee_Name = self.cleaned_data.get("Employee_Name")
        print(Employee_Name)
        if "abc" not in Employee_Name:
            raise forms.ValidationError ("This is not a valid Employee Name 
                                        ")
        return Employee_Name 

     #print("yaaah 3333333333!!")

希望有帮助


推荐阅读