Number Inputs using Django Crispy with Parsley

Sometimes I use Parsley for client-side validation. When I need the user to enter an amount of money (e.g. allow 2 decimal places), I use a Django DecimalField. When used with Crispy,  the field is rendered with type=”number” and step=0.01. All just as I want.

On my desktop, the input simply does not accept invalid characters. The problem is on the iPhone. It accepts the invalid characters. On submit, Parsley removes them and the decides that the field is empty and gives the error message “This field is required”. I turns out that the easy way to fix this is to change the Parsley error message. Here is how:

class AmountForm(forms.Form):
    description = forms.CharField(max_length=256)
    amount = forms.DecimalField(decimal_places=2)

    def __init__(self, *args, **kwargs):
        super(AmountForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        
        self.helper.layout = Layout(
            Field('description', required=True),
            Field('amount', required=True, 
                  data_parsley_required_message="Please enter a valid number"),
            Submit('submit', 'Submit'),
            Button('cancel', 'Cancel')
        )
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s