django字段是必需的验证器

时间:2016-10-21 14:52:30

标签: django validation django-forms

我在一个页面上有一些表单,我也尝试验证某些字段。

因此,如果我在测试字段输入了错误的输入,我显然会收到消息'无效输入',但对于每个其他字段,消息'此字段是必需的'。

我该如何解决?覆盖清洁功能?但是如何?

var app = angular.module('hotelsApp.controllers', [])

// My factory
app.factory("PromoService", function(){
    var promos = [
   {
      "Hotel":{
         "id":"52",
         "typeEtablissement":"HOTEL",
         "nomHotel":"AUBERGE DES ILES",
         "email":"auberge@ghaima.ci",
         "siteWeb":null,
         "situationGeo":"Assinie",
         "nombreChambre":"12",
         "commune_id":"21",
         "standing_id":"4",
         "prixChambreMin":"5000",
         "prixChambreMax":"15000",
         "tarif":"5 000 \/ 15 000",
         "description":"L'auberge des \u00eeles est un lieu paisible et reposant situ\u00e9 \u00e0 Assinie. Un cadre tout aussi propice \u00e0 l'\u00e9vasion et au loisir, vous accueille pour des s\u00e9jours \u00e0 court moyen et long terme.",
         "typePaiement":"",
         "autreinfo":"",
         "logo":"\/img\/hotels\/52\/logo.jpg",
      },
      "Commune":{
         "id":"21",
         "libelle":"Assinie"
      },
      "Standing":{
         "id":"4",
         "nbEtoiles":"2",
         "libelle":"2 Etoiles"
      },
      "Service":[
         {
            "id":"6",
            "libelle":"Bar",
            "Servicehotel":{
               "id":"1360",
               "hotel_id":"52",
               "service_id":"6"
            }
         }
      ]
   }
];

return {
    GetPromos: function(){
      return promos;
    },
    GetPromo: function(promoId){
      for(var j = 0; j < promos.length; j++) {
        if (promos[j].id === parseInt(promoId)) {
          return promos[j];
        }
      }
      return null;
    }
}
});


// My controllers
app.controller('PromosCtrl', function($scope, PromoService) {
  $scope.promos = PromoService.GetPromos();
});

app.controller('PromoCtrl', function($scope, $stateParams, PromoService) {
  promoId = $stateParams.promoId;
  $scope.promos[Hotel] = PromoService.GetPromo(promoId);
});

我还区分了不同的形式,如:

class Example1Form(forms.ModelForm):

    test = forms.CharField(max_length=30, validators=[RegexValidator(r'^[a-zA-Z0-9_-]+$'), MaxLengthValidator(30)])

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

首先,您需要一种方法来判断提交的表单。您可以为提交按钮命名,例如:

<form>
    {{ form_example1 }}
    <input name="form_example_1" type="submit" value="Submit form 1">
</form>

<form>
    {{ form_example2 }}
    <input name="form_example_2" type="submit" value="Submit form 2">
</form>

然后,在您的视图中,如果提交了该表单,则只应将表单绑定到POST数据。例如:

if "form_example1" in request.POST:
    form_example1 = Example1Form(request.POST)
else:
    form_example1 = Example1Form()

if "form_example2" in request.POST:
    form_example2 = Example2Form(request.POST)
else:
    form_example2 = Example2Form()