<p>I have a form for updating user info, with a field of birth date.
However, I am struggling to develop a validation for this date field by PHP.</p>

<p>Its frontend code is this;</p>

<pre><code>{% set tz = craft.app.getTimezone() %}
{% set df = craft.app.locale.getDateFormat('short', 'php') %}

&lt;div class="form-group"&gt;
    &lt;label for="testDate" class="form-label"&gt;Test Date&lt;/label&gt;
    &lt;input type="text" id="testDate" name="fields[testDate][date]" {%- if user is defined %} value="{{ user.testDate|date(df, tz) }}"{% endif %}&gt;
    {{ hiddenInput('fields[testDate][timezone]', tz) }}
&lt;/div&gt;
</code></pre>

<p>In PHP, the input date is gotten by this code.</p>

<pre><code>$testDate = $event-&gt;sender-&gt;testDate;
</code></pre>

<p>But this <code>$testDate</code> has already been converted to Date.
Even the input is invalid date (i.e. 5/32/2020), the form converts it to Jun 01.</p>

<p>How can I validate this date?</p>

<p>[Added on Jun 09]</p>

<p>The form's header is like this;</p>

<pre><code>&lt;form method="post" accept-charset="UTF-8" class="members-form updateprofile-form"&gt;
    {{ csrfInput() }}
    &lt;input type="hidden" name="action" value="users/save-user"&gt;
    &lt;input type="hidden" name="formType" id="formType" value="updateprofile"&gt;
    &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;
</code></pre>

<p>And, the PHP code is triggered by <code>EVENT_BEFORE_VALIDATE</code> event.</p>

CC BY-SA 4.0
2020-05-26T19:44:16.580Z
``$testDate`` is going to be a string unless you convert it to a DateTime object or use something like ``strtotime``. What's the rest of your code look like?
RitterKnight
CC BY-SA 4.0
2020-05-26T20:14:10.987Z
Actually, this is the all of my code relating this date field. On Craft back-end side, testDate is a Date/Time field, but that's what I want to do.
Kota.M
CC BY-SA 4.0
2020-06-05T18:21:01.583Z
Looks like you are using an event handler to retrieve the testDate. You should show some more code. Which controller action are you using? What is the rest of the form?
Johannes
CC BY-SA 4.0
2020-06-09T19:49:30.050Z
I have added some more code.
Kota.M
<craft3><php><datetime>
Date Field Validation by PHP plugin
# Date Field Validation by PHP plugin

(Asked by: username_0 on 2020-05-26)

I have a form for updating user info, with a field of birth date.
However, I am struggling to develop a validation for this date field by PHP.
Its frontend code is this;
{% set tz = craft.app.getTimezone() %}
{% set df = craft.app.locale.getDateFormat('short', 'php') %}


    Test Date
    
    {{ hiddenInput('fields[testDate][timezone]', tz) }}


In PHP, the input date is gotten by this code.
$testDate = $event->sender->testDate;

But this $testDate has already been converted to Date.
Even the input is invalid date (i.e. 5/32/2020), the form converts it to Jun 01.
How can I validate this date?
[Added on Jun 09]
The form's header is like this;

    {{ csrfInput() }}
    
    
    

And, the PHP code is triggered by EVENT_BEFORE_VALIDATE event.

**Tags:** craft3, php, datetime
> **Comments:**
> **username_1**: ``$testDate`` is going to be a string unless you convert it to a DateTime object or use something like ``strtotime``. What's the rest of your code look like?  
> **username_0**: Actually, this is the all of my code relating this date field. On Craft back-end side, testDate is a Date/Time field, but that's what I want to do.  
> **username_2**: Looks like you are using an event handler to retrieve the testDate. You should show some more code. Which controller action are you using? What is the rest of the form?  
> **username_0**: I have added some more code.  

---

## 0 answers

