You are here

function eck__entity__form_validate in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7.2 eck.entity.inc \eck__entity__form_validate()

Validation function for entity form for validating the fields.

File

./eck.entity.inc, line 463
All the menus, pages, and functionality related to administering entities.

Code

function eck__entity__form_validate($form, &$state) {
  $entity = $state['values']['entity'];
  field_attach_form_validate($entity
    ->entityType(), $entity, $form, $state);

  // Lets validate our properties by trying to set them.
  $entity_type_name = $entity
    ->entityType();
  $entity_type = EntityType::loadByName($entity_type_name);
  $properties = $entity_type->properties;

  // If we find a value set for a property lets just set it.
  foreach ($properties as $property => $info) {
    $form_value = _eck_form_property_value($state, $property);
    if (isset($form_value)) {

      // @todo This should be a widget hook not a behavior function.
      $vars = array(
        'data' => $form_value,
      );
      $data = eck_property_behavior_invoke_plugin($entity_type, 'pre_set', $vars);
      if (array_key_exists($property, $data)) {
        $form_value = $data[$property];
      }
      try {
        $entity->{$property} = $form_value;
      } catch (Exception $e) {

        // If there was an exception lets set up the proper for error.
        form_set_error($property, "Invalid property value {$form_value}, value should be of type {$info['type']}");
      }
    }
  }
}