CodeIgniter Forums
Validation for duplicated on form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Validation for duplicated on form (/showthread.php?tid=87898)



Validation for duplicated on form - pippuccio76 - 06-16-2023

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 ?


RE: Validation for duplicated on form - JustJohnQ - 06-16-2023

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


RE: Validation for duplicated on form - pippuccio76 - 06-16-2023

(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...


RE: Validation for duplicated on form - JustJohnQ - 06-16-2023

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.


RE: Validation for duplicated on form - pippuccio76 - 06-18-2023

(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


RE: Validation for duplicated on form - JustJohnQ - 06-19-2023

array_unique is a php function. A custom rule like this will probably work:
See: https://codeigniter4.github.io/userguide/libraries/validation.html#creating-custom-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;
    }




RE: Validation for duplicated on form - pippuccio76 - 06-22-2023

(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/libraries/validation.html#creating-custom-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 ?


RE: Validation for duplicated on form - JustJohnQ - 06-22-2023

See https://codeigniter.com/user_guide/libraries/validation.html#setting-rules-for-array-data