add field type validation

This commit is contained in:
Eduardo humberto 2018-02-08 13:06:19 -02:00
parent a80a9ad6e1
commit c484b33e19
3 changed files with 38 additions and 0 deletions

View File

@ -136,6 +136,17 @@ class Item_Metadata_Entity extends Entity {
$this->add_error('required', $field->get_name() . ' is required');
return false;
}
$field_type = $field->get_field_type();
if( class_exists( $field_type ) ){
$classFieldType = new $field_type();
if( method_exists ( $classFieldType , 'validate' ) ){
if( ! $classFieldType->validate( $value ) ) {
$this->add_error('field_type_error', $classFieldType->get_errors() );
return false;
}
}
}
if ($this->is_multiple()) {

View File

@ -233,4 +233,26 @@ class Item extends Entity {
}
}
}
/**
*
* {@inheritDoc}
* @see \Tainacan\Entities\Entity::validate()
*/
function validate(){
if( parent::validate() ){
$arrayItemMetadata = $this->get_fields();
if( $arrayItemMetadata ){
foreach ( $arrayItemMetadata as $itemMetadata ) {
if( !$itemMetadata->validate() ){
$errors = $itemMetadata->get_errors();
$this->add_error( $itemMetadata->get_field()->get_name(), $errors );
return false;
}
}
}
return true;
}
return false;
}
}

View File

@ -11,6 +11,7 @@ abstract class Field_Type {
private $primitive_type;
public $options;
public $errors;
abstract function render( $itemMetadata );
@ -39,6 +40,10 @@ abstract class Field_Type {
$this->primitive_type = $primitive_type;
}
public function get_errors() {
return $this->errors;
}
/**
* @param $options
*/