Validate a collumn with a value from another collumn
Validate a collumn with a value from another collumn
caue@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
This discussion has been closed.
Answers
This thread should help, it's demonstrating the same thing.
Cheers,
Colin
Thanks!!!! That's what I was looking for.
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?
Could you post all your Editor script, please, that'll help to get context,
Colin
Colin, thanks for you support. The image shows how it is.
$db->sql( 'set names utf8' );
Editor::inst( $db, 'pedidoestrutura' )
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.
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' )
Its returning...
Notice: Undefined index: pedidoestrutura in [...] on line 47
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"}]}
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']"
This is confusing: here you say the message is this:
and here:
I think the first message is the one you saw. The second one is ... I don't know.
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;
} ),
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
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?
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
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
Worked!!!!
Steps:
The field wasn't define. I did it:
This function now is sendding all data:
I did the validation:
Result:
Thakss
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?
Yes indeed - use the
formOptions.inline
object which can be used to specify all the same options as the second parameter of theinline()
method.Allan