Custom field validator for IP address with subnet included

Custom field validator for IP address with subnet included

axel_tsysaxel_tsys Posts: 19Questions: 5Answers: 0

Hi,

I have to use the postgresql field type 'inet'.
This is an field for an IP address with netmask included (ex: 1.1.0.0/16) .
IPv4 and IPv6 did work.
The build in validator for IP did not work with a subnetmask.
It looks like I need a custom field validator.

Can someone give me a hint how to start with this validator or did someone have this already in place?
I have already some php classes in place wich are able to check this but I am not sure who to include this.

Thanks and regards
Axel

Answers

  • axel_tsysaxel_tsys Posts: 19Questions: 5Answers: 0

    Sorry, I forgot the current code with the standard ip check:

    <?php
    
    // DataTables PHP library and database connection
    include( $_SERVER["DOCUMENT_ROOT"] . "DataTables/lib/DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use
            DataTables\Editor,
            DataTables\Editor\Field,
            DataTables\Editor\Format,
            DataTables\Editor\Mjoin,
            DataTables\Editor\Options,
            DataTables\Editor\Upload,
            DataTables\Editor\Validate,
            DataTables\Editor\ValidateOptions;
    
    
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'adminnetworkscomplete', 'id' )
            ->fields(
                    Field::inst( 'adminnetworkscomplete.interface' ),
                    Field::inst( 'adminnetworkscomplete.networkv4' )
                        ->validator( Validate::ip( ValidateOptions::inst()
                        ->allowEmpty(true))),
                    Field::inst( 'adminnetworkscomplete.networkv6' )
                        ->validator( Validate::ip( ValidateOptions::inst()
                        ->allowEmpty(true))),
                    Field::inst( 'adminnetworkscomplete.gatewayv4' )
                            ->validator( Validate::ip( ValidateOptions::inst()
                                    ->allowEmpty(true))),
                    Field::inst( 'adminnetworkscomplete.gatewayv6' )
                            ->validator( Validate::ip( ValidateOptions::inst()
                                    ->allowEmpty(true)))
    
            )
    
            ->process( $_POST )
            ->json();
    

    Thanks

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Hi,

    You'll need a custom validator function for this. You could use a regex if you just need to check for the IP with a netmask, or if you need to confirm that IP is in a certain range a function like this might help.

    Allan

This discussion has been closed.