original_string: public function addNested($field, Validator $validator, $message = null, $when = null)
      {
          $extra = array_filter(['message' => $message, 'on' => $when]);
  
          $validationSet = $this->field($field);
          $validationSet->add(static::NESTED, $extra + ['rule' => function ($value, $context) use ($validator, $message) {
              if (!is_array($value)) {
                  return false;
              }
              foreach ($this->providers() as $provider) {
                  $validator->setProvider($provider, $this->getProvider($provider));
              }
              $errors = $validator->errors($value, $context['newRecord']);
  
              $message = $message ? [static::NESTED => $message] : [];
  
              return empty($errors) ? true : $errors + $message;
          }]);
  
          return $this;
      }
code: public function addNested($field, Validator $validator, $message = null, $when = null)
      {
          $extra = array_filter(['message' => $message, 'on' => $when]);
  
          $validationSet = $this->field($field);
          $validationSet->add(static::NESTED, $extra + ['rule' => function ($value, $context) use ($validator, $message) {
              if (!is_array($value)) {
                  return false;
              }
              foreach ($this->providers() as $provider) {
                  $validator->setProvider($provider, $this->getProvider($provider));
              }
              $errors = $validator->errors($value, $context['newRecord']);
  
              $message = $message ? [static::NESTED => $message] : [];
  
              return empty($errors) ? true : $errors + $message;
          }]);
  
          return $this;
      }
code_tokens: public function addNested ( $ field , Validator $ validator , $ message = null , $ when = null ) { $ extra = array_filter ( [ 'message' => $ message , 'on' => $ when ] ) ; $ validationSet = $ this -> field ( $ field ) ; $ validationSet -> add ( static :: NESTED , $ extra + [ 'rule' => function ( $ value , $ context ) use ( $ validator , $ message ) { if ( ! is_array ( $ value ) ) { return false ; } foreach ( $ this -> providers ( ) as $ provider ) { $ validator -> setProvider ( $ provider , $ this -> getProvider ( $ provider ) ) ; } $ errors = $ validator -> errors ( $ value , $ context [ 'newRecord' ] ) ; $ message = $ message ? [ static :: NESTED => $ message ] : [ ] ; return empty ( $ errors ) ? true : $ errors + $ message ; } ] ) ; return $ this ; }
docstring: Adds a nested validator.
  
  Nesting validators allows you to define validators for array
  types. For example, nested validators are ideal when you want to validate a
  sub-document, or complex array type.
  
  This method assumes that the sub-document has a 1:1 relationship with the parent.
  
  The providers of the parent validator will be synced into the nested validator, when
  errors are checked. This ensures that any validation rule providers connected
  in the parent will have the same values in the nested validator when rules are evaluated.
  
  @param string $field The root field for the nested validator.
  @param \Cake\Validation\Validator $validator The nested validator.
  @param string|null $message The error message when the rule fails.
  @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  true when the validation rule should be applied.
  @return $this
docstring_tokens: Adds a nested validator .