TimeInput Format Validation - django

I have a template with an input field. The input field uses a timepicker plugin which uses a format like "1:00 AM" or "4:30 PM".
In forms.py, ive tried:
timepicker = forms.TimeField(label='Time', widget=forms.TimeInput(format='%H:%M %p'))
timepicker = forms.TimeField(label='Time', widget=forms.TimeInput(format='%I:%M %p'))
But with both these snippets, i continue to get a input validation error that says it does not match the format.

Have you tried the following?
forms.TimeField(label='time',
input_formats=['%I:%M %p'],
widget=forms.TimeInput(format='%I:%M %p'))

Related

Data format in Ext JS forms and Django models is equivalent, but not works

When I POST my form, I receive the following exception:
act_date: ["Date has wrong format. Use one of these formats instead: YYYY-MM-DD."]
And the same validation error for other DateFields.
I changed the default date format in extjs (Ext.util.Format.defaultDateFormat= 'Y-m-d') which did not work.
So next I define date format in Django setting:
'DATE_FORMATS': [("%Y-%m-%d"),],
This also hasn't worked.
When you said I changed the default date format in extjs (Ext.util.Format.defaultDateFormat= 'Y-m-d') which did not work. it's the date format don't work or again you'r server validation ?
Cause actually if i do this :
var d = new Date();
Ext.Date.format(d, 'Y-m-d');
That give me : "2019-03-05" and it's seem to be correct.
Have you check inside of you'r POST request the format of the sending date ?
You maybe have a unexpected date format before sending you'r request.

How to validate input text for integers and decimals only?

I cannot find the right answer for my case, so I posted my question here.
I'm validating the form in ASP.NET MVC and looking for the way to validate a text field to allow only numeric and decimal numbers like
1 or 1.5 or 1.65
If I have 1,65 I do not want this to be validated.
I have put a metadata on my Model's field like this: [RegularExpression(#"^(((\d{1})*))$")]
And have
#Html.ValidationMessageFor(m => m.ResolvedAmount, "", new { #class = "error" })
to validate the field.
Also, in my function I have the following to check the validation:
var validator = $("#main").kendoValidator().data('kendoValidator');
if(validator.validate()){some logic}
I validate for the required field, but cannot get my int/decimal validation working.
What do I need to have in order to validate it?

Silverstripe 3 show date picker on template

Is there a certain way to get the datepicker to show on the template?
I have used the following code:
TextField::create('DateFrom','Date From')
->setAttribute('data-datepicker', true)
->setAttribute('data-date-format', 'DD-MM-YYYY'),
I added this to my config yaml file:
DateField:
default_config:
showcalendar: true
The text box shows but no date picker shows when the textbox is clicked on.
There are two ways. Either you use the SilverStripe DateField like so:
DateField::create('DateFrom','Date From')->setConfig('showcalendar', true);
The SilverStripe DateField will render a date-picker using JavaScript.
Or you use an HTML5 date field that relies on the browser implementation for the date-picker. For that to work, you need to also set the type of the field to date (it defaults to text):
DateField::create('DateFrom','Date From')
->setAttribute('type', 'date')
->setConfig('datavalueformat', 'dd-MM-yyyy') // Server side validation
->setAttribute('data-datepicker', true)
->setAttribute('data-date-format', 'DD-MM-YYYY');
You should be using a DateField instead of a TextField.
TextField::create('DateFrom','Date From')
->setAttribute('data-datepicker', true)
->setAttribute('data-date-format', 'DD-MM-YYYY'),
should be
DateField::create('DateFrom','Date From')
->setAttribute('data-datepicker', true)
->setAttribute('data-date-format', 'DD-MM-YYYY'),

Django view DateField Format headache

Help would be appreciated. Whatever i search for I find everything else but what I need it seems. wwwaaah....
I have a DateField on my template that i need to get user specified date for an API i am building. My trouble is I need it in a specific format. I have googled and trying various params but cant get to work. Django's DateField default format is yyyy-mm-dd, but due to how our API works I need yyyy/mm/dd.
specs:
-django1.5
-python27
What works....
views.py
class AddForm(forms.Form):
new_timestamp = forms.DateField(required=False)
class TimeView(View):
def post(self, request)
time_content = AddForm(request.POST)
content['client_time'] = time_content
if time_content.is_valid():
new_timestamp = time_content.cleaned_data["new_timestamp"]
...
...blah blah
template:
<div id="div_date_section">Date: <input type="text" name="new_timestamp"> YYYY/MM/DD </div>
Gives me the following output (captured in my debug logs):
2016-11-10 23:46:48,520: DEBUG: views.py:296 timestamp: 2016-01-01 00:00:00-08:00
2016-11-10 23:46:48,520: DEBUG: views.py:297 timestamp type: <type 'datetime.datetime'>
What I would like is a string formatted as: 2016/01/01.
I have tried various methods to no effect. As mentioned I have been googling for hours, and one of thing i found was as follows, however this makes my code not work. I get absolutely no error to log or console, my code just breaks complaining about something unrelated down a few lines of code later on in my code.
new_timestamp = forms.DateField(widget=forms.widgets.DateInput(format="%Y/%m/%d"),required=False)
Can anyone advise please? Thanks in advance.
After struggling for a bit longer, I realized the requirements of what i was trying to do was slightly different. While i still needed to alter what the user entered, I found I could manipulate the DateTime object as such to give me what I needed (which turned out to be epoch format):
class AddForm(forms.Form):
new_timestamp = forms.DateField(required=False)
class TimeView(View):
def post(self, request)
time_content = AddForm(request.POST)
content['client_time'] = time_content
if time_content.is_valid():
new_timestamp = time_content.cleaned_data["new_timestamp"]
...
...
results = some_def(new_timestamp)
...
...
def some_def:
...
...
# the new line that gave me what i needed
epoch_time = time.mktime(new_timestamp.timetuple())
...
...
return epoch_time
I realize there is not enough context here but hopefully this is clear enough how to pass user input via DateField on a template and convert. This example is for converting something like 2016-01-01 to epoch time format.
just a note, to avoid my initial problem the default DateField format is yyyy-mm-dd and if you use yyyy/mm/dd without any widgets or format manipulation trying so with .is_valid() will fail.

TimeField format in Django template

I'm writing an application in Django and using several TimeFields in a model. I only want to work with the time format hh:mm (not hh:mm:ss or hh p.m./a.m. or other things).
I have set the django setting TIME_FORMAT = 'H:i' and USE_L10N = False according to the documentation,
USE_L10N overrides TIME_FORMAT if set to True
In my template I'm using input boxes automatically created by Django like:
{{formName.timeField}}
The problem is that as long as I introduce something like "10:00" and do a POST, when the POST is done Django (or whatever) turns it into "10:00:00" (so it adds the seconds).
Is there a way to specify the format in which datetime fields display saved dates? If I could just use something like |date:"H:i" in the value of the input box that would do the trick.
On the other side I know I could do the input box manually (not directly using {{formName.timeField}}), but I've had some problems in the past with that so I would like to avoid that (at least for the moment).
There is also this similar question in Django TimeField Model without seconds, but the solution does not work (it gives me 'NoneType' object has no attribute 'strptime')
I had exactly this problem in my app, and solved it by passing a format parameter to Django's TimeInput widget:
from django import forms
class SettingsForm(forms.Form):
delivery_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
Assuming you have a format you want all TimeFields to use, you can add the TIME_INPUT_FORMATS to your settings file. For example, TIME_INPUT_FORMATS = ('%I:%M %p',) will format your times as "5:30 PM".
The docs: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TIME_INPUT_FORMATS