Validate a collumn with a value from another collumn

Validate a collumn with a value from another collumn

caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

Hi, there

I'm trying to validate a collumn with a value from another collumn. There are in the same table.

Buts im doing sometring wrong

I need to get a value from 'qtdmax', and use on the validator, something like this:

Field::inst( 'qtdmax' ), Field::inst( 'qtd' )->validator( Validate::maxNum(field('qtdmax'), ".", ValidateOptions::inst() ->message( 'Stock must be less then field('qtdmax') ' ) ) );

I tried many times all ways, and didn't work. Can anyone help me on that?
Tks

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    This thread should help, it's demonstrating the same thing.

    Cheers,

    Colin

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    Thanks!!!! That's what I was looking for.

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    All right, its stilll now working. Lets see..

    Field::inst( 'qtdemax' ), Field::inst( 'quantidade' )->validator( function ( $val, $data, $opts ) { return $data['qtdemax']; } ),

    Its returnin me this:

    Notice: Undefined index: qtdemax in X {"fieldErrors":[{"name":"quantidade","status":null}],"data":[]}

    How could i fix it?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Could you post all your Editor script, please, that'll help to get context,

    Colin

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    Colin, thanks for you support. The image shows how it is.

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    $db->sql( 'set names utf8' );

    Editor::inst( $db, 'pedidoestrutura' )

    ->fields(
        Field::inst( 'idpedido' ),
        Field::inst( 'produto' ),
        Field::inst( 'nomeproduto' ),
        Field::inst( 'pativo' ),
        Field::inst( 'fp' ),
        Field::inst( 'promo' ),
        Field::inst( 'laboratorio' ),
        Field::inst( 'qtdemax' ),
        Field::inst( 'quantidade' )->validator( function ( $val, $data, $opts ) { return $data['qtdemax']; } ),  
        Field::inst( 'unitario' ),
        Field::inst( 'stunitario' ),
        Field::inst( 'ean' ),
        Field::inst( 'pmc' ),
        Field::inst( 'pf' )
    
    )
    
    ->where( 'idpedido', $_POST['pedidosession'])
    ->where( 'laboratorio', 'Politech', '!=')
    ->where( 'laboratorio', 'Brinde', '!=')
    ->where( 'laboratorio', 'Não informado', '!=')
    ->where( 'laboratorio', 'Teste', '!=')
    
    ->process( $_POST )
    
    ->json();
    
  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited July 2020

    not sure what your problem really is but this is an example that works with multiple fields in it ...
    Don't forget to return "true" if the validation is passed without an error and an error message when validation fails.

    if ($lang === 'de') {     
        $msg[0] = 'Feld darf nicht leer sein.';
        $msg[1] = 'Bitte geben Sie eine Zahl ein.';
        ein.';
    } else {
        $msg[0] = 'Field may not be empty.';
        $msg[1] = 'Please enter a number.';
    }
    Editor::inst( $db, 'table' )
    ->field(
        Field::inst( 'table.contract_id' )->set(Field::SET_CREATE)
                                            ->setValue( $_POST['contract_id'] ),
        Field::inst( 'table.include' ),
        Field::inst( 'table.interface_status' )->set(Field::SET_EDIT)
                                                 ->setValue( 2 ),
        Field::inst( 'table.external_id' )
            ->validator( function ( $val, $data, $opts ) use ( $msg ) {
                if ( $data['table']['include'] > 0 ) {
                    if ($val <= '') {
                        return $msg[0];
                    }
                }
                return true;
            } ),
        Field::inst( 'table.partner_id' )
            ->validator( function ( $val, $data, $opts ) use ( $msg ) {
                if ( $data['table']['include'] > 0 ) {
                    if ( ( $val <= '' ) &&
                         ( $data['table2']['acct_system'] == 'SAP' ||
                           $data['table2']['acct_system'] == 'HH'    ) ) { //Only SAP and HH need the partner id
                        return $msg[0];    
                    }
                }
                return true;
            } ),
        Field::inst( 'table2.acct_system' )->set( false ), .....
    }
    ->leftJoin( 'table2', 'table.table2_id', '=', 'table2.id' )
    
  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    Hi rf1234,

    It still no well. Let me show you:

    $msg[0] = 'Field may not be empty.';
    $msg[1] = 'Please enter a number.';

    $db->sql( 'set names utf8' );

    Editor::inst( $db, 'pedidoestrutura' )
    ->fields(
    Field::inst( 'idpedido' ),
    Field::inst( 'produto' ),
    Field::inst( 'nomeproduto' ),
    Field::inst( 'pativo' ),
    Field::inst( 'fp' ),
    Field::inst( 'promo' ),
    Field::inst( 'laboratorio' ),
    Field::inst( 'qtdemax' ),
    Field::inst( 'quantidade' )

        ->validator( function ( $val, $data, $opts ) use ( $msg ) {
            if ( $data['pedidoestrutura']['qtdemax'] > 0 ) {
                if ($val <= '') {
                    return $msg[0];
                }
            }
            return true;
        } ),
    
        Field::inst( 'unitario' ),
        Field::inst( 'stunitario' ),
    

    Its returning...

    Notice: Undefined index: pedidoestrutura in [...] on line 47

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    The full response is that:

    Notice: Undefined index: qtdemax in [...] on line 47
    {"data":[{"DT_RowId":"row_40442", "idpedido":"787", "produto":"561", "nomeproduto":"Acebrofilina Xpe Ad 10mg 120ml Framb Gen - Geolab", "pativo":"Acebrofilina*", "fp":"N", "promo":"N", "laboratorio":"Geolab Gen", "qtdemax":"0", "quantidade":"1", "unitario":"9.40", "stunitario":"1.40", "ean":"7899095233027", "pmc":"29.61", "pf":"21.42"}]}

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited July 2020

    since the field you are using in the data table ist not called "pedidoestrutura.qtdemax" but simply "qtdemax" using "$data['pedidoestrutura']['qtdemax']" is not working.

    The response tells you what the field is called. It is "$data['qtdemax']"

    ->validator( function ( $val, $data, $opts ) use ( $msg ) {
        if ( $data['qtdemax'] > 0 ) {
            if ($val <= '') {
                return $msg[0];
            }
        }
        return true;
    } ),
    

    This is confusing: here you say the message is this:

    Its returning...
    Notice: Undefined index: pedidoestrutura in [...] on line 47

    and here:

    The full response is that:
    Notice: Undefined index: qtdemax in [...] on line 47

    I think the first message is the one you saw. The second one is ... I don't know.

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    If I use $data['qtdemax'], i get Notice: Undefined index: qtdemax in [...] on line 47

    ->validator( function ( $val, $data, $opts ) use ( $msg ) {
    if ( $data['qtdemax'] > 0 ) {
    if ($val <= '') {
    return $msg[0];
    }
    }
    return true;
    } ),

    If I use $data['pedidoestrutura']['qtdemax'] , i get Notice: Undefined index: pedidoestrutura in [...] on line 47

    ->validator( function ( $val, $data, $opts ) use ( $msg ) {
    if ( $data['pedidoestrutura']['qtdemax'] > 0 ) {
    if ($val <= '') {
    return $msg[0];
    }
    }
    return true;
    } ),

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Are you using inline editing? If so, you'll need to have it submit the data for all fields by using the submit: 'allIfChanged' option - see this example.

    Assuming you have name: 'qtdemax' on the client-side configuration for this field, then $data['dtdemax'] would be the correct way to access it.

    Allan

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    Allan, thanks for the answer. I got what you mean.

    I didnt want to get the information from the client-side. I wanted to get from the database selection, from the "Editor::inst( $db, 'pedidoestrutura' )".

    Something like this...

    Field::inst( 'qtd' )->validator( Validate::maxNum(Field::inst( 'qtdmax' )));

    I wasnt looking to get the information from de Submitted data. I was looking to get from thue the database collumn

    Is that possible?

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Oh I see - yes, you can query the database using the $db object. The API documentation for that is here.

    However, isn't qtdmax something that is also present in the form? If you validate against what is in the database, then it would be using the information that was previously saved, rather than using the information that was submitted in the form?

    Allan

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    I wasnt looking to get the information from de Submitted data. I was looking to get from thue the database collumn
    Is that possible?

    If you don't want to retrieve the field separately as per @allan's suggestion using the db handler you need to send it back and forth to the client as a hidden field unless it is in your form.

    Here is a more detailed thread on this:
    https://datatables.net/forums/discussion/comment/168837/#Comment_168837

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    Worked!!!!

    Steps:

    The field wasn't define. I did it:

    This function now is sendding all data:

    I did the validation:

    Result:

    Thakss

  • caue@comercialtrimed.com.brcaue@comercialtrimed.com.br Posts: 18Questions: 6Answers: 0

    The last question:

    Can i define the submit:'allIfChanged' not in a onClick function?

    I'm using "Excel like keyboard navigation", and onClick function is not helping on the client expiriense.

    I tryed on here, but didn't work:

    Any idea?

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Yes indeed - use the formOptions.inline object which can be used to specify all the same options as the second parameter of the inline() method.

    Allan

This discussion has been closed.