title: Django forms HTML5 validate pattern
body: <p>Dugen Chen wrote a helpful post on adding the 'required' attribute in HTML5's validation to Django form fields.
  <a href="http://duganchen.ca/elegantly-adding-html5-validation-attributes-to-django-form-fields/" rel="nofollow">http://duganchen.ca/elegantly-adding-html5-validation-attributes-to-django-form-fields/</a></p>
  
  <p>I would also like to have the HTML5 pattern attribute automatically included so that if this is my form:</p>
  
  <pre><code>class AccountEditForm(forms.Form):
    email = forms.EmailField()
    phone = forms.CharField(widget=forms.IntegerField)
  </code></pre>
  
  <p>Then the following html will be created:</p>
  
  <pre><code>&lt;input type="email" name="email" id="id_email" required pattern="-email regex-" /&gt;
  &lt;input type="text" name="phone" id="id_phone" required pattern="-integer regex-" /&gt;
  </code></pre>
  
  <p>Dugen used this method:</p>
  
  <pre><code>compose = lambda f, g: lambda *args, \
            **kwargs: f(g(*args, **kwargs))
  
  def html5_required(field):
    if field.required != False:
      field.widget.attrs['required'] = 'required'
    return field
  
  CharField = compose(html5_required, CharField)
  </code></pre>
  
  <p>so that a forms could still be written like</p>
  
  <pre><code>class SampleForm(form):
    text_field = CharField()  
  </code></pre>
  
  <p>and the 'required' attribute would be added appropriately</p>
  
  <p>Could someone explain how to do something similar with for the pattern attribute.
  Could all this be applied with a decorator so this mess could reside somewhere else?</p>
  
  <p>Ideally, is there a set of regex patterns standardized to match each Django form field and has anyone built a small library to easily add it into Django?</p>
  
  <p>Thanks!</p>