Example #1
0
 /**
  * Set a new field
  *
  * @param Field $field A instance from Field
  *
  * @throws Exception\FieldAlreadyRegistered
  *
  * @return $this Current instance of Input instance
  */
 public function field(Field $field)
 {
     $fieldName = $field->field();
     $fieldData = $this->source->get($fieldName);
     if (isset($this->fields[$fieldName])) {
         throw new Exception\FieldAlreadyRegistered($fieldName);
     }
     // Import the value into field instance
     if (!$field->import($fieldData)) {
         if ($errors = $field->errors()) {
             foreach ($errors as $error) {
                 $this->error(new Error($error->type(), $error->code(), $fieldName, $error->data()));
             }
         } else {
             $this->error(new Error('ERROR', 'FIELD_UNKNOWN_ERROR', $fieldName));
         }
     }
     $this->fields[$fieldName] = $field;
     return $this;
 }