Validate Field optinos according to field type specific validation
This commit is contained in:
parent
8d078a4d34
commit
e5f4bc3b68
|
@ -154,6 +154,9 @@ class Entity {
|
|||
|
||||
/**
|
||||
* Validate the class values/properties, to be used before insert/save/update
|
||||
*
|
||||
* If Entity is not valid, validation error messages are available via get_errors() method
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate() {
|
||||
|
|
|
@ -140,7 +140,7 @@ class Field extends Entity {
|
|||
function get_field_type_object(){
|
||||
$class_name = $this->get_field_type();
|
||||
$object_type = new $class_name();
|
||||
$object_type->set_options( $this->get_field_options() );
|
||||
$object_type->set_options( $this->get_field_type_options() );
|
||||
return $object_type;
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ class Field extends Entity {
|
|||
*
|
||||
* @return array Configurations for the field type object
|
||||
*/
|
||||
function get_field_options(){
|
||||
function get_field_type_options(){
|
||||
return $this->get_mapped_property('field_type_options');
|
||||
}
|
||||
|
||||
|
@ -298,7 +298,7 @@ class Field extends Entity {
|
|||
}
|
||||
|
||||
/**
|
||||
* save the field type class name
|
||||
* set the field type class name
|
||||
*
|
||||
* @param string | \Tainacan\Field_Types\Field_Type $value The name of the class or the instance
|
||||
*/
|
||||
|
@ -313,6 +313,16 @@ class Field extends Entity {
|
|||
function set_accept_suggestion( $value ) {
|
||||
return $this->set_mapped_property('accept_suggestion', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Field type options
|
||||
*
|
||||
* @param [string || integer] $value
|
||||
* @return void
|
||||
*/
|
||||
function set_field_type_options( $value ){
|
||||
$this->set_mapped_property('field_type_options', $value);
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
|
@ -342,4 +352,40 @@ class Field extends Entity {
|
|||
function is_required() {
|
||||
return $this->get_required() === 'yes';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc }
|
||||
*
|
||||
* Also validates the field, calling the validate_options callback of the Field Type
|
||||
*
|
||||
* @return bool valid or not
|
||||
*/
|
||||
public function validate() {
|
||||
|
||||
$is_valid = parent::validate();
|
||||
|
||||
if (false === $is_valid)
|
||||
return false;
|
||||
|
||||
$fto = $this->get_field_type_object();
|
||||
|
||||
if (is_object($fto)) {
|
||||
$is_valid = $fto->validate_options($this->get_field_type_options());
|
||||
}
|
||||
|
||||
if (true === $is_valid)
|
||||
return true;
|
||||
|
||||
if (!is_array($is_valid))
|
||||
throw new Exception("Return of validate_options field type method should be an Array in case of error");
|
||||
|
||||
foreach ($is_valid as $field => $message) {
|
||||
$this->add_error($field, $message);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,5 +94,17 @@ abstract class Field_Type {
|
|||
return $attributes;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the options Array
|
||||
*
|
||||
* This method should be declared by each field type sub classes
|
||||
*
|
||||
* @param Array $options Options to be saved as field_type_optins
|
||||
* @return true|Array True if optinos are valid. If invalid, returns an array where keys are the field keys and values are error messages.
|
||||
*/
|
||||
public function validate_options(Array $options) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -97,4 +97,14 @@ class Relationship extends Field_Type {
|
|||
<?php endif; ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function validate_options(Array $options) {
|
||||
// TODO: This is just a sample validation to test validation workflow for field types. Must redo it
|
||||
if (isset($options['collection_id']) && !is_numeric($options['collection_id'])) {
|
||||
return [
|
||||
'collection_id' => 'Collection ID invalid'
|
||||
];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -141,8 +141,9 @@ class Fields extends Repository {
|
|||
'field_type_options' => [ // not showed in form
|
||||
'map' => 'meta',
|
||||
'title' => __('Field Type options', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'type' => 'array',
|
||||
'description'=> __('Options specific for field type', 'tainacan'),
|
||||
'default' => [],
|
||||
// 'validation' => ''
|
||||
],
|
||||
'collection_id' => [ // not showed in form
|
||||
|
|
|
@ -336,6 +336,38 @@ class Fields extends TAINACAN_UnitTestCase {
|
|||
$this->assertNotEquals($x->get_slug(), $y->get_slug());
|
||||
|
||||
}
|
||||
|
||||
function test_validation_of_field_types() {
|
||||
global $Tainacan_Fields;
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$validField = new \Tainacan\Entities\Field();
|
||||
$validField->set_name('test');
|
||||
$validField->set_description('test');
|
||||
$validField->set_collection($collection);
|
||||
$validField->set_field_type('Tainacan\Field_Types\Relationship');
|
||||
$validField->set_field_type_options(['collection_id' => 12]);
|
||||
|
||||
$this->assertTrue($validField->validate());
|
||||
|
||||
$invalidField = new \Tainacan\Entities\Field();
|
||||
$invalidField->set_name('test');
|
||||
$invalidField->set_description('test');
|
||||
$invalidField->set_collection($collection);
|
||||
$invalidField->set_field_type('Tainacan\Field_Types\Relationship');
|
||||
$invalidField->set_field_type_options(['collection_id' => 'string']);
|
||||
|
||||
$this->assertFalse($invalidField->validate());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue