Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to process complex datatypes in CakePHP?
    primarykey
    data
    text
    <h1>My Goal: Replace the Default Cake Datepicker</h1> <p>I <em>hate</em> the default CakePHP FormHelper date-picker select elements and I'd like to use jQuery UI Datepicker widget instead. In order to degrade gracefully, I want to have a date field (enabled with the widget) and a time-picker select box constrained to 15-minute increments.</p> <p>In case I explained that poorly, here is what it looks like:</p> <p><img src="https://i.stack.imgur.com/oQT1f.png" alt="date-picker sample"></p> <h1>My Ideal Solution</h1> <p>To minimize repetition, I want to put the HTML in a layout Element and process it using a Behavior function. This way I could do something like the following:</p> <p><em>view.ctp</em> </p> <pre><code>echo $this-&gt;element( 'datepicker', array( 'data' =&gt; $data ) ); </code></pre> <p><em>model.php</em> </p> <pre><code>$actsAs = array( 'Datepicker' ); function beforeSave( $options ){ $this-&gt;parseDatepickers(); //behavior function would alter $this-&gt;data return true; } </code></pre> <h1>The Problem</h1> <p>Unfortunately, by the time it gets to the <code>beforeSave</code> (or <code>beforeValidate</code>) callback, the datepicker &amp; timepicker fields have been destroyed by the model's <code>deconstruct</code> function. <code>deconstruct</code> seems to be looking for dates &amp; times to be reported the way FormHelper creates them.</p> <p>In short, it's looking for:</p> <pre><code>[date_field] =&gt; Array ( [year] =&gt; 2011 [month] =&gt; 11 [day] =&gt; 11 [hour] =&gt; 8 [min] =&gt; 00 [meridian] =&gt; pm ) </code></pre> <p>but instead it's finding:</p> <pre><code>[date_field] =&gt; Array ( [datepicker] =&gt; 11/11/2011 [timepicker] =&gt; 8:00 pm ) </code></pre> <p>And because it doesn't find the structure it expects, I end up with this:</p> <pre><code>[date_field] =&gt; </code></pre> <p><em>I know I can have jQuery update hidden inputs with the appropriately-named fields, but this wouldn't degrade well.</em></p> <h1>Current Workaround</h1> <p>What I'm doing, for the moment, is washing the data through my behavior function prior to saving -- but this doesn't feel like the right way to do it:</p> <pre><code>$this-&gt;request-&gt;data = $this-&gt;Event-&gt;fixDates( $this-&gt;data ); $this-&gt;Event-&gt;save( $this-&gt;data ); </code></pre> <h1>So...............</h1> <p>What is the best way to do this? Putting it in <code>beforeSave</code> or <code>beforeValidate</code> seems like "the Cake way", but <code>deconstruct</code> kills me. Do I need to extend AppModel and override <code>deconstruct</code>? That seems ugly too.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload