I think I've found a solution, or it might be a hack - keen to get 
confirmation or advice (and apologies for the noobness in advance)..

At the bottom of the add function of the ModelController.php, I've added 
the following code to check if the request data for the date field exists 
(when the page errors, the post data is being returned in the request 
data), and if it does to format it in the format I'm working with. It's 
doing the job, but I'd like to work out how I can make this a bit more 
generic as I have a few date fields throughout my project.

//On load or refresh of the add view, check if request data is set for date 
fields, then format the output in to specific date formatif 
(isset($this->request->data['Model']['issue_date'])) {
    $this->request->data['Model']['issue_date'] = date('d-m-Y', 
strtotime($this->request->data['Model']['issue_date']));}

I've used the same snippet on my edit function also (successfully), so that 
when a user goes to edit the record, the date is rendered in the input 
field in the correct format.

On Monday, 2 December 2013 22:50:01 UTC+11, seba wrote:
>
>
> A well-worn topic I'm sure..
>
> Some background:
>
> *In the /View/Model/add.ctp view*, I have a few of datetime fields that 
> use the Bootstrap datepicker and (successfully) sets the format of the 
> dates in the text inputs in dd-mm-yyyy format:
>
> *Example of a datetime field input:*
> echo $this->Form->input('Model.issue_date', array(
> 'fieldset' => false,
> 'label' => false,
> 'before' => '<label class="col-sm-2 control-label">Issue Date</label><div 
> class="col-sm-10">',
> 'after' => '</div>',
>          'class' => 'form-control datepicker',
>          'type' => 'text',
> 'div' => 'form-group'
> )
> );
>
> *Datapicker js at bottom of /View/Model/add.ctp:*
>
> <script type="text/javascript">
> <!--
>
> //-->
> $(document).ready(function() {
>         $('.datepicker').datepicker({
>     format: "dd-mm-yyyy",
>     todayBtn: "linked",
>     orientation: "bottom right",
>     autoclose: true,
>     todayHighlight: true
>         });
> });
> </script>
>
>
>
> *In my /Controller/ModelController.php file*, prior to saving the 
> post/form data, I convert the field in to mysql format:
>
> if (isset($this->request->data['Model']['issue_date'])) {
> $this->request->data['Model']['issue_date'] = date('Y-m-d H:i', 
> strtotime($this->request->data['Model']['issue_date']));
> }
>
>
> *And in the /Model/Model.php* file I have basic validation to ensure the 
> field isn't empty (i've removed validating datetime format temporarily):
>
> 'issue_date'=> array(
> 'allowEmpty' => array(
> 'rule'    => 'notEmpty',
> 'message'  => 'Please enter a date and time'
> ),
>
>
>
> *Problem 1:*
>
> When I add datetime validation to the field in the /Model/Model.php file, 
> I get a format validation error. Here's the code I use to validate the 
> format (one of many permutations used):
>
>         'issue_date'=> array(
>             'allowEmpty' => array(
>         'rule'    => 'notEmpty',
>                 'message'  => 'Please enter a date and time'
>             ),
>             'format' => array(
>       'rule' => array('datetime', 'dmy'),
>                 'message'  => 'Please enter a valid date'
> )
>         ),
>         
> When I remove the date format validation, the form posts fine, and the 
> (correct) datetime is committed to the datavase.
>         
> *Problem 2:*
>
> When I encounter an error on the form, like the one in Problem 1, above, 
> and return back to the add or edit views, the dates in the text input 
> fields revert back to yyyy-mm-dd H:i:s format, and I have to reselect the 
> date from the datepicker to return back to dd-mm-yyyy format.
>
> I figure I need to add beforeRender to check if a post value exists for 
> the date field, and if so to format it, but I have a feeling there's 
> something potentially a lot simpler I could be doing to manage both of 
> these issues, and am keen to see/hear some opinions/advice.
>
> Thanks in advance!
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to