Validate::minNum

Validate::minNum

aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

Please see my code below.
Look at POINT_Y and POINT_X fields.
They both are being validated for minNum
The minNum validation works if you do not specify a custom message, like in POINT_X, and you will get the generic message.
Once you add a custom message, it breaks, like in POINT_Y.

What am I doing wrong with the custom message?
Any ideas?

POINT_Y generates this error in the response


<br />
<b>Fatal error</b>: Uncaught Error: Object of class DataTables\Editor\ValidateOptions could not be converted to string in C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor\Validate.php:422
Stack trace:

0 C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor\Validate.php(422): str_replace(Object(DataTables\Editor\ValidateOptions), '.', '2')

1 C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor\Validate.php(460): DataTables\Editor\Validate::DataTables\Editor{closure}('2', Array, Object(DataTables\Editor\Field), Array)

2 C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor\Field.php(690): DataTables\Editor\Validate::DataTables\Editor{closure}('2', Array, Object(DataTables\Editor\Field), Array)

3 C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor.php(814): in <b>C:\HostedSites\dev\dev.mapbiocontrol.asets.msu.edu\wwwroot\htdocs\0\DataTables\Editor-PHP-1.9.0\lib\Editor\Validate.php</b> on line <b>422</b><br />


<?php

// DataTables PHP library
include("../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, 'APHIS_RELEASE_SITES', 'OBJECTID')
    ->debug(true)
    ->fields(
        Field::inst('OBJECTID'),
        Field::inst('STATE'),
        Field::inst('OB_DATE'),
        Field::inst('SITE_ID'),
        Field::inst('SITE_NAME'),
        Field::inst('SITE_LOCATION'),
        Field::inst('PLOT_TYPE'),
        Field::inst('ORIGIN'),
        Field::inst('STATUS'),
        Field::inst('USERID'),
        Field::inst('POINT_Y')
            ->validator(
                Validate::minNum(
                    10,
                    ValidateOptions::inst()
                        ->message('This field is nasty') 
                ),
            ),
        Field::inst('POINT_X')
            ->validator(Validate::minNum(10)),
        Field::inst('SIZE_WOODED_AREA'),
        Field::inst('ASH_PCT'),
        Field::inst('OVERSTORY_AB_SP1'),
        Field::inst('OVERSTORY_AB_SP2'),
        Field::inst('OVERSTORY_AB_SP3'),
        Field::inst('EAB_DENSITY'),
        Field::inst('TOPOGRAPHIC_POSITION'),
        Field::inst('FLOODING'),
        Field::inst('DEGREE_ISOLATION'),
        Field::inst('SITE_ID_OF_PAIR_RC'),
        Field::inst('COMMENTS')
    )
    ->process($_POST)
    ->json();

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,453Questions: 1Answers: 10,055 Site admin
    Answer ✓

    Hi,

    The second parameter of the minNum validator is not the validations object, but rather a string which can be used to specify the decimal separator. See the function signature here.

    What you want is:

                    Validate::minNum(
                        10,
                        '.',
                        ValidateOptions::inst()
                            ->message('This field is nasty')
                    ),
    

    Allan

  • aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

    Great! Thank you very much. It did it. So simple, and I could not figure it out.

    Would you care to give me a quick run of the purpose/usage of the second string? why it would not be enough just to have a number in the first parameter?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583
    Answer ✓

    It would be for internationalisation, as not all languages uses decimal points - for example German's use ",". It needs that to be able to parse the number to check the validity,

    Colin

  • aziegler3aziegler3 Posts: 21Questions: 3Answers: 0
    edited December 2020

    Just a last comment. The example in
    https://editor.datatables.net/manual/php/validation#Field-validation-examples
    probably needs to be updated, right? that is where I got the wrong sintaxis.
    Thank you all for your help

  • allanallan Posts: 61,453Questions: 1Answers: 10,055 Site admin
    Answer ✓

    Completely agree! Sorry about that - I've committed the change now and it is live on the site, although the cache might need a little bit of time to clear.

    We've got it required at the moment to keep the arguments the same - using a comma for the decimal is actually really wide spread. But I'll probably make that second parameter optional in a future update (assumed to be a . if the second parameter is a ValidateOptions object) - you aren't the first to hit this (I've done it myself!).

    Allan

  • aziegler3aziegler3 Posts: 21Questions: 3Answers: 0

    Thank you

This discussion has been closed.