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 ($this->is_multiple()) {
if (is_array($value)) { 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 // if its required, at least one must be filled
$one_filled = false; $one_filled = false;
$valid = true; $valid = true;

View File

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

View File

@ -85,15 +85,15 @@
class="empty-label"> class="empty-label">
{{ $i18n.get('info_no_value_compound_metadata') }} {{ $i18n.get('info_no_value_compound_metadata') }}
</p> </p>
<a <a
v-if="isMultiple" v-if="isMultiple && (maxMultipleValues === undefined || maxMultipleValues === 0 || (maxMultipleValues !== 1 && maxMultipleValues > childItemMetadataGroups.length))"
:disabled="itemMetadatum.item.id && (childItemMetadataGroups.length > 0 && !someValueOnLastInput)" :disabled="itemMetadatum.item.id && (childItemMetadataGroups.length > 0 && !someValueOnLastInput)"
@click="addGroup" @click="addGroup"
class="is-inline-block add-link"> class="is-inline-block add-link">
<span class="icon is-small"> <span class="icon is-small">
<i class="tainacan-icon has-text-secondary tainacan-icon-add"/> <i class="tainacan-icon has-text-secondary tainacan-icon-add"/>
</span> </span>
&nbsp;{{ $i18n.get('label_add_value') }} &nbsp;{{ $i18n.get('label_add_value') }}
</a> </a>
</div> </div>
@ -124,7 +124,10 @@
}, },
someValueOnLastInput() { someValueOnLastInput() {
return this.childItemMetadataGroups && this.childItemMetadataGroups[this.childItemMetadataGroups.length - 1] && this.childItemMetadataGroups[this.childItemMetadataGroups.length - 1].findIndex((childItemMetadatum) => childItemMetadatum.value) >= 0; 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: { watch: {
/* This will create the input object structure for /* This will create the input object structure for

View File

@ -83,7 +83,7 @@
</template> </template>
</transition-group> </transition-group>
</template> </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 <a
@click="addValue" @click="addValue"
class="is-inline-block add-link"> 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>"; 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);
}
} }