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 [email protected].
To post to this group, send email to [email protected].
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