Welcome Guest, Not a member yet? Register   Sign In
Validation for duplicated on form
#1

Hi i have a form with 20 serial number (serial_1 , serial_2 etc) , can i check with custom  validation if there are duplicate in form ?
Reply
#2

If you create an array of your serial numbers like serial[1], serial[2] etc. you can use the array_unique function to remove any duplicates from this array. Then count the number of items in the original array and compare it to the number of items in the array_unique array. If it differs, you know there were duplicates
Reply
#3

(06-16-2023, 01:26 AM)JustJohnQ Wrote: If you create an array of your serial numbers like serial[1], serial[2] etc. you can use the array_unique function to remove any duplicates from this array. Then count the number of items in the original array and compare it to the number of items in the array_unique array. If it differs, you know there were duplicates

Sorry but only serial_1 is required...
Reply
#4

Then I don’t understand your question. What do you want to check for duplicates?
Validation of serial_1 is a simple validation. Checking all serials for a duplicate value is what I gave a suggestion for.
Reply
#5

(06-16-2023, 02:38 PM)JustJohnQ Wrote: Then I don’t understand your question. What do you want to check for duplicates?
Validation of serial_1 is a simple validation. Checking all serials for a duplicate value is what I gave a suggestion for.

Yes , i want control if there are a duplicate in one or more serial.... I can do it by js but i want also server side
Reply
#6

(This post was last modified: 06-19-2023, 11:24 AM by JustJohnQ.)

array_unique is a php function. A custom rule like this will probably work:
See: https://codeigniter4.github.io/userguide...stom-rules
PHP Code:
<?php

class MyRules
{
    public function duplicates($serialarray): bool
    
{
$originalLength count($serialArray);
$duplicateArray array_unique($serialArray);
$duplicateLength count($duplicateArray); 
if (
$originalLength <> $duplicateLength){
return 
0;
}else{
return 
1;
    }

Reply
#7

(This post was last modified: 06-22-2023, 12:55 AM by pippuccio76.)

(06-19-2023, 04:02 AM)JustJohnQ Wrote: array_unique is a php function. A custom rule like this will probably work:
See: https://codeigniter4.github.io/userguide...stom-rules
PHP Code:
<?php

class MyRules
{
    public function duplicates($serialarray): bool
    
{
$originalLength count($serialArray);
$duplicateArray array_unique($serialArray);
$duplicateLength count($duplicateArray); 
if (
$originalLength <> $duplicateLength){
return 
0;
}else{
return 
1;
    }




How can i pass in validation the array with every serial of form ?
Reply
Reply




Theme © iAndrew 2016 - Forum software by © MyBB