feat: update validate with cardinality for multiples metadatas #568

This commit is contained in:
vnmedeiros 2021-09-01 14:47:20 -03:00
parent 108a3d3baa
commit f7804cd2b5
5 changed files with 41 additions and 6 deletions

View File

@ -486,6 +486,11 @@ class Item_Metadata_Entity extends Entity {
if ($this->is_multiple()) {
if (is_array($value)) {
$cardinality = $metadatum->get_cardinality();
if ( !empty($cardinality) && $cardinality > 0 && count($value) > $cardinality ) {
$this->add_error( 'invalid', sprintf( __('Metadatum %s is set to accept a maximum of %s values.', 'tainacan'), $metadatum->get_name(), $cardinality ) );
return false;
}
// if its required, at least one must be filled
$one_filled = false;
$valid = true;

View File

@ -208,7 +208,8 @@
:disabled="editForm.multiple != 'yes'"
name="cardinality"
type="number"
step="1" />
step="1"
v-model="editForm.cardinality"/>
</b-field>
<b-field v-if="!isRepositoryLevel && isInsideImporterFlow">

View File

@ -86,7 +86,7 @@
{{ $i18n.get('info_no_value_compound_metadata') }}
</p>
<a
v-if="isMultiple"
v-if="isMultiple && (maxMultipleValues === undefined || maxMultipleValues === 0 || (maxMultipleValues !== 1 && maxMultipleValues > childItemMetadataGroups.length))"
:disabled="itemMetadatum.item.id && (childItemMetadataGroups.length > 0 && !someValueOnLastInput)"
@click="addGroup"
class="is-inline-block add-link">
@ -124,7 +124,10 @@
},
someValueOnLastInput() {
return this.childItemMetadataGroups && this.childItemMetadataGroups[this.childItemMetadataGroups.length - 1] && this.childItemMetadataGroups[this.childItemMetadataGroups.length - 1].findIndex((childItemMetadatum) => childItemMetadatum.value) >= 0;
}
},
maxMultipleValues() {
return (this.itemMetadatum && this.itemMetadatum.metadatum && this.itemMetadatum.metadatum.cardinality && !isNaN(this.itemMetadatum.metadatum.cardinality)) ? this.itemMetadatum.metadatum.cardinality : undefined;
},
},
watch: {
/* This will create the input object structure for

View File

@ -83,7 +83,7 @@
</template>
</transition-group>
</template>
<template v-if="isMultiple && (maxMultipleValues === undefined || maxMultipleValues - 1 > values.length)">
<template v-if="isMultiple && (maxMultipleValues === undefined || maxMultipleValues === 0 || (maxMultipleValues !== 1 && maxMultipleValues > values.length))">
<a
@click="addValue"
class="is-inline-block add-link">

View File

@ -762,4 +762,30 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
return "<a data-linkto='item' data-id='${id}' href='${URL}'>${title}</a>";
}
function test_multiple_with_cardinality() {
// Multiple numeric metadata
$metadatum_numeric_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Numeric important meta',
'description' => 'and its description',
'collection_id' => $this->collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'status' => 'publish',
'multiple' => 'yes',
'cardinality' => 3
),
true
);
$item_metadata_numeric_mult = new Item_Metadata_Entity($this->item, $metadatum_numeric_multiple);
$item_metadata_numeric_mult->set_value([10,22,4]);
$ok = $item_metadata_numeric_mult->validate();
$this->assertTrue($ok);
$item_metadata_numeric_mult->set_value([10,22,4,4]);
$fail = $item_metadata_numeric_mult->validate();
$this->assertFalse($fail);
}
}