original_string: final public function validate() {
          global $CFG;
  
          // Before validate hook.
          $this->before_validate();
  
          // If this object has not been validated yet.
          if ($this->validated !== true) {
  
              $errors = array();
              $properties = static::properties_definition();
              foreach ($properties as $property => $definition) {
  
                  // Get the data, bypassing the potential custom getter which could alter the data.
                  $value = $this->raw_get($property);
  
                  // Check if the property is required.
                  if ($value === null && static::is_property_required($property)) {
                      $errors[$property] = new lang_string('requiredelement', 'form');
                      continue;
                  }
  
                  // Check that type of value is respected.
                  try {
                      if ($definition['type'] === PARAM_BOOL && $value === false) {
                          // Validate_param() does not like false with PARAM_BOOL, better to convert it to int.
                          $value = 0;
                      }
                      if ($definition['type'] === PARAM_CLEANHTML) {
                          // We silently clean for this type. It may introduce changes even to valid data.
                          $value = clean_param($value, PARAM_CLEANHTML);
                      }
                      validate_param($value, $definition['type'], $definition['null']);
                  } catch (invalid_parameter_exception $e) {
                      $errors[$property] = static::get_property_error_message($property);
                      continue;
                  }
  
                  // Check that the value is part of a list of allowed values.
                  if (isset($definition['choices']) && !in_array($value, $definition['choices'])) {
                      $errors[$property] = static::get_property_error_message($property);
                      continue;
                  }
  
                  // Call custom validation method.
                  $method = 'validate_' . $property;
                  if (method_exists($this, $method)) {
  
                      // Warn the developers when they are doing something wrong.
                      if ($CFG->debugdeveloper) {
                          $reflection = new ReflectionMethod($this, $method);
                          if (!$reflection->isProtected()) {
                              throw new coding_exception('The method ' . get_class($this) . '::'. $method . ' should be protected.');
                          }
                      }
  
                      $valid = $this->{$method}($value);
                      if ($valid !== true) {
                          if (!($valid instanceof lang_string)) {
                              throw new coding_exception('Unexpected error message.');
                          }
                          $errors[$property] = $valid;
                          continue;
                      }
                  }
              }
  
              $this->validated = true;
              $this->errors = $errors;
          }
  
          return empty($this->errors) ? true : $this->errors;
      }
code: final public function validate() {
          global $CFG;
  
          // Before validate hook.
          $this->before_validate();
  
          // If this object has not been validated yet.
          if ($this->validated !== true) {
  
              $errors = array();
              $properties = static::properties_definition();
              foreach ($properties as $property => $definition) {
  
                  // Get the data, bypassing the potential custom getter which could alter the data.
                  $value = $this->raw_get($property);
  
                  // Check if the property is required.
                  if ($value === null && static::is_property_required($property)) {
                      $errors[$property] = new lang_string('requiredelement', 'form');
                      continue;
                  }
  
                  // Check that type of value is respected.
                  try {
                      if ($definition['type'] === PARAM_BOOL && $value === false) {
                          // Validate_param() does not like false with PARAM_BOOL, better to convert it to int.
                          $value = 0;
                      }
                      if ($definition['type'] === PARAM_CLEANHTML) {
                          // We silently clean for this type. It may introduce changes even to valid data.
                          $value = clean_param($value, PARAM_CLEANHTML);
                      }
                      validate_param($value, $definition['type'], $definition['null']);
                  } catch (invalid_parameter_exception $e) {
                      $errors[$property] = static::get_property_error_message($property);
                      continue;
                  }
  
                  // Check that the value is part of a list of allowed values.
                  if (isset($definition['choices']) && !in_array($value, $definition['choices'])) {
                      $errors[$property] = static::get_property_error_message($property);
                      continue;
                  }
  
                  // Call custom validation method.
                  $method = 'validate_' . $property;
                  if (method_exists($this, $method)) {
  
                      // Warn the developers when they are doing something wrong.
                      if ($CFG->debugdeveloper) {
                          $reflection = new ReflectionMethod($this, $method);
                          if (!$reflection->isProtected()) {
                              throw new coding_exception('The method ' . get_class($this) . '::'. $method . ' should be protected.');
                          }
                      }
  
                      $valid = $this->{$method}($value);
                      if ($valid !== true) {
                          if (!($valid instanceof lang_string)) {
                              throw new coding_exception('Unexpected error message.');
                          }
                          $errors[$property] = $valid;
                          continue;
                      }
                  }
              }
  
              $this->validated = true;
              $this->errors = $errors;
          }
  
          return empty($this->errors) ? true : $this->errors;
      }
code_tokens: final public function validate ( ) { global $ CFG ; // Before validate hook. $ this -> before_validate ( ) ; // If this object has not been validated yet. if ( $ this -> validated !== true ) { $ errors = array ( ) ; $ properties = static :: properties_definition ( ) ; foreach ( $ properties as $ property => $ definition ) { // Get the data, bypassing the potential custom getter which could alter the data. $ value = $ this -> raw_get ( $ property ) ; // Check if the property is required. if ( $ value === null && static :: is_property_required ( $ property ) ) { $ errors [ $ property ] = new lang_string ( 'requiredelement' , 'form' ) ; continue ; } // Check that type of value is respected. try { if ( $ definition [ 'type' ] === PARAM_BOOL && $ value === false ) { // Validate_param() does not like false with PARAM_BOOL, better to convert it to int. $ value = 0 ; } if ( $ definition [ 'type' ] === PARAM_CLEANHTML ) { // We silently clean for this type. It may introduce changes even to valid data. $ value = clean_param ( $ value , PARAM_CLEANHTML ) ; } validate_param ( $ value , $ definition [ 'type' ] , $ definition [ 'null' ] ) ; } catch ( invalid_parameter_exception $ e ) { $ errors [ $ property ] = static :: get_property_error_message ( $ property ) ; continue ; } // Check that the value is part of a list of allowed values. if ( isset ( $ definition [ 'choices' ] ) && ! in_array ( $ value , $ definition [ 'choices' ] ) ) { $ errors [ $ property ] = static :: get_property_error_message ( $ property ) ; continue ; } // Call custom validation method. $ method = 'validate_' . $ property ; if ( method_exists ( $ this , $ method ) ) { // Warn the developers when they are doing something wrong. if ( $ CFG -> debugdeveloper ) { $ reflection = new ReflectionMethod ( $ this , $ method ) ; if ( ! $ reflection -> isProtected ( ) ) { throw new coding_exception ( 'The method ' . get_class ( $ this ) . '::' . $ method . ' should be protected.' ) ; } } $ valid = $ this -> { $ method } ( $ value ) ; if ( $ valid !== true ) { if ( ! ( $ valid instanceof lang_string ) ) { throw new coding_exception ( 'Unexpected error message.' ) ; } $ errors [ $ property ] = $ valid ; continue ; } } } $ this -> validated = true ; $ this -> errors = $ errors ; } return empty ( $ this -> errors ) ? true : $ this -> errors ; }
docstring: Validates the data.
  
  Developers can implement addition validation by defining a method as follows. Note that
  the method MUST return a lang_string() when there is an error, and true when the data is valid.
  
  protected function validate_propertyname($value) {
  if ($value !== 'My expected value') {
  return new lang_string('invaliddata', 'error');
  }
  return true
  }
  
  It is OK to use other properties in your custom validation methods when you need to, however note
  they might not have been validated yet, so try not to rely on them too much.
  
  Note that the validation methods should be protected. Validating just one field is not
  recommended because of the possible dependencies between one field and another,also the
  field ID can be used to check whether the object is being updated or created.
  
  When validating foreign keys the persistent should only check that the associated model
  exists. The validation methods should not be used to check for a change in that relationship.
  The API method setting the attributes on the model should be responsible for that.
  E.g. On a course model, the method validate_categoryid will check that the category exists.
  However, if a course can never be moved outside of its category it would be up to the calling
  code to ensure that the category ID will not be altered.
  
  @return array|true Returns true when the validation passed, or an array of properties with errors.
docstring_tokens: Validates the data .