0

Python http://formencode.readthedocs.org/en/latest/modules/national.html#module-formencode.nationalで FormEncode を使用しています。

ブラジルのカスタム郵便番号バリデーターを追加しようとしています。ドキュメントを読みましたが、どれも役に立たないようです。誰もこれにアプローチする方法について何か考えを持っていますか?

4

2 に答える 2

0

FormEncodeを使用してこれを解決する方法を誰かが知りたい場合、これが私が思いついた解決策です

class BrazilZipValidator(validators.Regex):
messages = {
    'invalid': _('Please enter a valid code (nnnnn-nnn or nnnnnnnn)')
}
regex = re.compile(r'^([0-9]{8})$|^([0-9]{5}-[0-9]{3})$')
strip = True

def _to_python(self, value, state):
    self.assert_string(value, state)
    match = self.regex.search(value)
    if not match:
        raise formencode.Invalid(self.message('invalid', state), value, \
                                 state)
    return match.group()

次に、このクラスを検証オブジェクトにアタッチします

national.PostalCodeInCountryFormat._vd.update({'BR': BrazilZipValidator})
于 2015-07-23T20:16:17.543 に答える
-2

モデル

[checkCountry(AllowCountry = "India,USA,SriLanka", ErrorMessage = ("Please choose a valid country eg.(India,USA,SriLanka"))]
public string Country { get; set; }

属性にこのコードをコピーして貼り付け、エラーを回避するためにネームスペース名を変更してください

using System.Linq;


namespace WebApp.Models
{
    public class checkCountryAttribute : ValidationAttribute
    {
        public String AllowCountry { get; set; }

        protected override ValidationResult IsValid(object country, ValidationContext validationContext)

        {

            string[] myarr = AllowCountry.ToString().Split(',');

            if (myarr.Contains(country))

            {

                return ValidationResult.Success;

            }

            else

            {

                return new ValidationResult("Please choose a valid country eg.(India,USA,SriLanka)");

            }

        }
    }
}

コントローラ:

[ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Student(Student submit)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Message = "Succesfully Submitted";
                return View("Student");
            }
            return View();
        }

間違いなくそれはうまくいく

このコードが機能しない場合は、以下にコメントしてください

于 2019-02-11T17:35:15.390 に答える