title: CAKEPHP 3.x - Error on entity update - missing fields claimed even when have values
body: <p>I have an entity ( booking) for which I set some values manually and other values are set using patchEntity.
  But when it comes to save, I got an error stating that two required fields are missing.</p>
  
  <p>This is what the entity debug looks like:</p>
  
  <pre><code>object(App\Model\Entity\Booking) {
  
      'provider_pk' =&gt; (int) 2,
      'date' =&gt; object(Cake\I18n\Time) {
  
          'time' =&gt; '2016-03-31T00:00:00+00:00',
          'timezone' =&gt; 'UTC',
          'fixedNowTime' =&gt; false
  
      },
      'start' =&gt; object(Cake\I18n\Time) {
  
          'time' =&gt; '2016-03-31T08:00:00+00:00',
          'timezone' =&gt; 'UTC',
          'fixedNowTime' =&gt; false
  
      },
      'modules_count' =&gt; (int) 1,
      'requester_pk' =&gt; (int) 1,
      'remarks' =&gt; '',
      'status_id' =&gt; (int) 1,
      'end' =&gt; object(Cake\I18n\Time) {
  
          'time' =&gt; '2016-03-31T09:00:00+00:00',
          'timezone' =&gt; 'UTC',
          'fixedNowTime' =&gt; false
  
      },
      '[new]' =&gt; true,
      '[accessible]' =&gt; [
          '*' =&gt; true
      ],
      '[dirty]' =&gt; [
          'provider_pk' =&gt; true,
          'date' =&gt; true,
          'start' =&gt; true,
          'modules_count' =&gt; true,
          'requester_pk' =&gt; true,
          'remarks' =&gt; true,
          'status_id' =&gt; true,
          'end' =&gt; true
      ],
      '[original]' =&gt; [],
      '[virtual]' =&gt; [],
      '[errors]' =&gt; [
          'date' =&gt; [
              '_required' =&gt; 'This field is required'
          ],
          'start' =&gt; [
              '_required' =&gt; 'This field is required'
          ]
      ],
      '[invalid]' =&gt; [],
      '[repository]' =&gt; 'Bookings'
  
  }
  </code></pre>
  
  <p>As you can see, the 'date', and 'start' fields have a value, but in the errors section, it shows that they are missing.</p>
  
  <p>This is how I create the new entity ( $date and $start have the corresponding values):</p>
  
  <pre><code>    $booking = $this-&gt;Bookings-&gt;newEntity( [ 'provider_pk' =&gt; $provider_pk, 'date' =&gt; $date, 'start' =&gt; $start ] );
  </code></pre>
  
  <p>Then I use patchEntity, and before saving it, I manually set another field, and then try to save it like this:</p>
  
  <pre><code>   $booking = $this-&gt;Bookings-&gt;patchEntity($booking, $this-&gt;request-&gt;data);
  
      $booking-&gt;end = $end-&gt;modify( '+' . $booking-&gt;modules_count .  ' hours' );
      if ($this-&gt;Bookings-&gt;save($booking)) 
      {
          $this-&gt;Flash-&gt;success(__('The booking has been saved.'));
          return $this-&gt;redirect( ['action' =&gt; 'show' ] );
      } else ...
  </code></pre>
  
  <p>And then the $booking->errors() gives this:</p>
  
  <pre><code>[
  
      'date' =&gt; [
          '_required' =&gt; 'This field is required'
      ],
      'start' =&gt; [
          '_required' =&gt; 'This field is required'
      ]
  ]
  </code></pre>
  
  <p>As you can see the date and start fields are claimed to be required even when the originally have a value.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here's the table model :</p>
  
  <pre><code>&lt;?php
  namespace App\Model\Table;
  
  use App\Model\Entity\Booking;
  use Cake\ORM\Query;
  use Cake\ORM\RulesChecker;
  use Cake\ORM\Table;
  use Cake\Validation\Validator;
  
  /**
   * Bookings Model
   *
   * @property \Cake\ORM\Association\BelongsTo $Statuses
   */
  class BookingsTable extends Table
  {
  
      /**
       * Initialize method
       *
       * @param array $config The configuration for the Table.
       * @return void
       */
      public function initialize(array $config)
      {
          parent::initialize($config);
  
          $this-&gt;table('bookings');
          $this-&gt;displayField('id');
          $this-&gt;primaryKey('id');
  
          $this-&gt;belongsTo('Statuses', [
              'foreignKey' =&gt; 'status_id',
              'joinType' =&gt; 'INNER'
          ]);
  
          // ---------------------------------------------------------------------
          // customize for every project
  
          $this-&gt;hasMany( 'Aircrafts' );
          $this-&gt;belongsTo( 'Pilots', [ 'foreignKey' =&gt; 'requester_pk', 'joinType' =&gt; 'LEFT' ] );
  
      }
  
      /**
       * Default validation rules.
       *
       * @param \Cake\Validation\Validator $validator Validator instance.
       * @return \Cake\Validation\Validator
       */
      public function validationDefault(Validator $validator)
      {
          $validator
              -&gt;integer('id')
              -&gt;allowEmpty('id', 'create');
  
          $validator
              -&gt;date('date')
              -&gt;requirePresence('date', 'create')
              -&gt;notEmpty('date');
  
          $validator
              -&gt;dateTime('start')
               -&gt;requirePresence('start', 'create')
              -&gt;notEmpty('start');
  
          $validator
              -&gt;dateTime('end')
               -&gt;requirePresence('end', 'create')
              -&gt;notEmpty('end');
  
          $validator
              -&gt;integer('modules_count')
               -&gt;requirePresence('modules_count', 'create')
              -&gt;notEmpty('modules_count');
  
          $validator
              -&gt;integer('requester_pk')
              -&gt;requirePresence('requester_pk', 'create')
              -&gt;notEmpty('requester_pk');
  
          $validator
              -&gt;integer('provider_pk')
              -&gt;allowEmpty('provider_pk');
  
          $validator
              -&gt;integer('assistant_pk')
              -&gt;allowEmpty('assistant_pk');
  
          $validator
              -&gt;allowEmpty('remarks');
  
          return $validator;
      }
  
      /**
       * Returns a rules checker object that will be used for validating
       * application integrity.
       *
       * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
       * @return \Cake\ORM\RulesChecker
       */
      public function buildRules(RulesChecker $rules)
      {
          $rules-&gt;add($rules-&gt;existsIn(['status_id'], 'Statuses'));
          return $rules;
      }
  }
  </code></pre>
  
  <p>And here's the controller method :</p>
  
  <pre><code>/** form for booking confirmation
  *
  * @param string|null $id cell id.
  * @return \Cake\Network\Response|null Redirects to bookings / show .
  **/
  
  public function confirmBooking()
  {
      if( isset( $this-&gt;request-&gt;query['id']) )
      {
          $id = $this-&gt;request-&gt;query['id'];
  
          $token = strpos( $id, '-' );
  
          $day = substr( $id, $token + 1 , 2 );
          $month = substr( $id, $token + 3 , 2 );
          $year = substr( $id, $token + 5 , 4 );
          $hour = substr( $id, $token + 9 , 2 );
          $minutes = substr( $id, $token + 11, 2 );
  
          $date = Time::createFromTimestamp( mktime( 0, 0, 0, $month, $day, $year ) );
          $start = Time::createFromTimestamp( mktime( $hour, $minutes, 0, $month, $day, $year ) );
          $end = Time::createFromTimestamp( mktime( $hour + 1, $minutes, 0, $month, $day, $year ) );
          $provider_pk = substr( $id, 1, $token - 1 );
      }
  
      $aircrafts =  $this-&gt;Bookings-&gt;Aircrafts-&gt;find( 'all', [ 'order' =&gt; 'registration' ] );
      $this-&gt;set( 'aircrafts', $aircrafts );
  
      // ---------------------------------------------------------------------
      // depending on add or update
  
      if( isset( $this-&gt;request-&gt;query['booking_id'] ) )
          $booking = $this-&gt;Bookings-&gt;get($this-&gt;request-&gt;query['booking_id'], [ 'contain' =&gt; [ 'Pilots' ] ] );
      else
      {
          $booking = $this-&gt;Bookings-&gt;newEntity( [ 'provider_pk' =&gt; $provider_pk, 'date' =&gt; $date, 'start' =&gt; $start, 'end' =&gt; $end, 'modules_count' =&gt; 1 ] );
      }
  
      if ($this-&gt;request-&gt;is('post') || ( $this-&gt;request-&gt;is('put') &amp;&amp;  isset( $this-&gt;request-&gt;query['booking_id'] ) ) ) {
  
          debug( $this-&gt;request-&gt;data );
  
          $booking = $this-&gt;Bookings-&gt;patchEntity($booking, $this-&gt;request-&gt;data);
  
          $booking-&gt;end = $end-&gt;hour( $this-&gt;request-&gt;data[ 'end_value' ][ 'hour' ] );
  
          $booking-&gt;modules_count = $booking-&gt;end-&gt;diff( $booking-&gt;start )-&gt;format('%h');
  
  
          if ($this-&gt;Bookings-&gt;save($booking)) 
          {
              $this-&gt;Flash-&gt;success(__('The booking has been saved.'));
              return $this-&gt;redirect( ['action' =&gt; 'show' ] );
          } else 
          {
              $this-&gt;Flash-&gt;error(__('The booking could not be saved. Please, try again.')  );
              return $this-&gt;redirect(['action' =&gt; 'show']);
          }
      }
  
      $statuses = $this-&gt;Bookings-&gt;Statuses-&gt;find('list', ['limit' =&gt; 200]);
      $assistants = $this-&gt;Bookings-&gt;Pilots-&gt;find( 'list' , [ 'conditions' =&gt; [ 'is_instructor =' =&gt; true ], 'fields' =&gt; [ 'id', 'firstname' ] ] );        
      $providers = $this-&gt;Bookings-&gt;Aircrafts-&gt;find( 'list' , [ 'fields' =&gt; [ 'id', 'registration' ] ] );        
      $requesters = $this-&gt;Bookings-&gt;Pilots-&gt;find( 'list' , [ 'fields' =&gt; [ 'id', 'lastname' ] ] );        
  
      $this-&gt;set(compact('booking', 'statuses'));
  
      $this-&gt;set('_serialize', ['booking']);
      $this-&gt;set( 'assistants', $assistants );
      $this-&gt;set( 'providers', $providers );
      $this-&gt;set( 'requesters', $requesters );
  }        
  </code></pre>