fix: add function validate in numeric metadata type #606.

This commit is contained in:
vnmedeiros 2021-11-30 10:16:43 -03:00
parent fc761f762c
commit d8a8bab6d9
2 changed files with 45 additions and 29 deletions

View File

@ -828,9 +828,10 @@ class CSV extends Importer {
if($itemMetadata instanceof Entities\Item_Metadata_Entity ) {
$itemMetadata->set_item( $insertedItem ); // *I told you
if( $itemMetadata->validate() ) {
$result = $Tainacan_Item_Metadata->insert( $itemMetadata );
$Tainacan_Item_Metadata->insert( $itemMetadata );
} else {
$this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name() . " in item " . $insertedItem->get_title());
$insertedItemId = $updating_item == true ? ' (special_item_id: ' . $insertedItem->get_id() . ')' : '';
$this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name() . " in item " . $insertedItem->get_title() . $insertedItemId);
$this->add_error_log($itemMetadata->get_errors());
continue;
}

View File

@ -9,32 +9,47 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Numeric extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_name( __('Numeric', 'tainacan') );
$this->set_primitive_type('float');
$this->set_component('tainacan-numeric');
$this->set_form_component('tainacan-form-numeric');
$this->set_description( __('A numeric value, integer or float', 'tainacan') );
$this->set_preview_template('
<div>
<div class="control is-clearfix">
<input type="number" placeholder="3,1415" class="input">
</div>
</div>
');
}
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_name( __('Numeric', 'tainacan') );
$this->set_primitive_type('float');
$this->set_component('tainacan-numeric');
$this->set_form_component('tainacan-form-numeric');
$this->set_description( __('A numeric value, integer or float', 'tainacan') );
$this->set_preview_template('
<div>
<div class="control is-clearfix">
<input type="number" placeholder="3,1415" class="input">
</div>
</div>
');
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'step' => [
'title' => __( 'Step', 'tainacan' ),
'description' => __( 'The amount to be increased or decreased when clicking on the metadatum control buttons. This also defines whether the input accepts decimal numbers.', 'tainacan' ),
]
];
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'step' => [
'title' => __( 'Step', 'tainacan' ),
'description' => __( 'The amount to be increased or decreased when clicking on the metadatum control buttons. This also defines whether the input accepts decimal numbers.', 'tainacan' ),
]
];
}
public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$value = is_array($value) ? $value : [$value];
foreach ($value as $numeric_value) {
if( !empty($numeric_value) && !is_numeric($numeric_value) ) {
$this->add_error( sprintf(__('The value (%s) is not a valid number', 'tainacan'), $numeric_value ) );
return false;
}
}
return true;
}
}