How to get values from multiple names and insert in database?

How to get values from multiple names and insert in database?

robertutzurobertutzu Posts: 11Questions: 1Answers: 0
edited November 2015 in Free community support

I want to insert in database values from other fields/names

For example i want to insert the value from field "parcela", "zona", "rand" and "loc" to "idunic" for example
Parcela value is 5
Zona value is B
Rand value is 22
Loc value is 563

I want to insert in database field "idunic" with the value "5-B-22-563"

So i have this:


<?php /* * Example PHP implementation used for the index.html example */ // DataTables PHP library include( "../php/DataTables.php" ); // Alias Editor classes so they are easy to use use DataTables\Editor, DataTables\Editor\Field, DataTables\Editor\Format, DataTables\Editor\Join, DataTables\Editor\Upload, DataTables\Editor\Validate; // Build our Editor instance and process the data coming from _POST Editor::inst( $db, 'loc' ) ->fields( Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ), Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'rand' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'loc' ) -> validator( 'Validate::unique' ), Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'idunic' ) -> validator( 'Validate::notEmpty' ) //here i want to be inserted value from parcela-zona-rand-loc, it would look like a code for example 6-B-22-547 ) ->where( $key = 'loc', $value = '', $op = '!=' ) ->process( $_POST ) ->json();

and the javascript:
maybe i could put it like a default value, but i dont know how to parse the values from parcela-zona-rand-loc


<script> var editor; // use a global for the submit and return data rendering in the examples $( document ).ready(function() { editor = new $.fn.dataTable.Editor( { ajax: "ajax/loc.php", table: "#loc", fields: [ {label: "Parcela:", name: "parcela", type: "select"}, {label: "Zona:", name: "zona", type: "select"}, {label: "Rand:", name: "rand"}, {label: "Loc:", name: "loc"}, {label: "Nord:", name: "nord"}, {label: "Sud:", name: "sud"}, {label: "Est:", name: "est"}, {label: "Vest:", name: "vest"}, {label: "Id Unic:", name: "idunic", def: "parcela-zona-rand-loc" } ] } ); var table = $('#loc').DataTable( { ajax: { url: "ajax/loc.php", type: "POST" }, processing: true, serverSide: true, columns: [ { data: "parcela" }, { data: "zona" }, { data: "rand" }, { data: "loc" }, { data: "nord" }, { data: "sud" }, { data: "est" }, { data: "vest" }, { data: "idunic"} ], select: true } ); // Display the buttons new $.fn.dataTable.Buttons( table, [ { extend: "create", editor: editor }, { extend: "edit", editor: editor }, { extend: "remove", editor: editor }, { extend: 'collection', text: 'Export', buttons: ['copy', 'excel', 'csv', 'pdf', 'print']} ] ); table.buttons().container() .appendTo( $('.col-sm-6:eq(0)', table.table().container() ) ); } ); </script>

I apreciate any help !

Replies

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    Hi,

    It sounds like you need to use a get formatter which can be used to pre-process the data submitted by the client. Another option might be to use a server-side event but I think a formatter would probably be best here.

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0

    Thx a lot allan, after a little digging i found the answer

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'loc' )
        ->fields(
            Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ),
            Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'randx' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'loc' ) -> validator( 'Validate::unique' ),
            Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'idunic' ) ->setFormatter( function($val, $data, $field) {return $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'];})
        )
        ->where( $key = 'loc', $value = '', $op = '!=' )
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    Excellent - good to hear your got it sorted!

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0

    Ok, sorted that out but now the unique doesnt work, it doesnt validates that field "idunic" is unique.

    Any help is apreciated.


    // Build our Editor instance and process the data coming from _POST Editor::inst( $db, 'loc' ) ->fields( Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ), Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'randx' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'loc' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'idunic' ) ->validator( 'Validate::unique' ) -> setFormatter( function($val, $data, $field) {return $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'];}) ) ->where( $key = 'loc', $value = '', $op = '!=' ) ->process( $_POST ) ->json();
  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    The issue there is that the validation is done on the data before the formatter is executed (the formatter assumes that valid data is received).

    You would need to use a server-side event if you want to add such a validator.

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0
    edited December 2015

    Ok i went on Events but still didnt works, i think i did something wrong but i dont know what...


    // Build our Editor instance and process the data coming from _POST Editor::inst( $db, 'loc' ) ->fields( Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ), Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'randx' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'loc' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'idunic' ) -> set( Field::SET_CREATE ) -> validator( 'Validate::unique' ) ) ->on( 'preCreate', function ( $editor, $data ) { $editor ->field( 'idunic' ) ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] ); } ) ->where( $key = 'loc', $value = '', $op = '!=' ) ->process( $_POST ) ->json();
  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0
    edited December 2015

    ok i got it working this way:

    editor.on( 'preSubmit', function ( e, o, a ) {
        if(a==='create' || a==='edit'){
        var vparcela = document.getElementById("DTE_Field_parcela").value;
        var vzona = document.getElementById("DTE_Field_zona").value;
        var vrandx = document.getElementById("DTE_Field_randx").value;
        var vloc = document.getElementById("DTE_Field_loc").value;
            o.data[0].idunic = vparcela+'-'+vzona+'-'+vrandx+'-'+vloc;
        }
    } );
    
    

    create works as expexted but edit makes error:
    TypeError: o.data[0] is undefined

  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    The event method should have worked - are you using 1.5.3 of the PHP libraries? If so, I'll try to replicate the issue locally.

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0
    edited December 2015

    i have 1.5.2, ill put soon the full code
    edit:// updated to 1.5.3 still doesnt validate

    ps: just to let you know, you are the BEST!!!

    php code:


    // Build our Editor instance and process the data coming from _POST Editor::inst( $db, 'loc' ) ->fields( Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ), Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'randx' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'loc' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ), Field::inst( 'idunic' ) -> set( Field::SET_CREATE ) -> validator( 'Validate::unique' ) ) ->on( 'preCreate', function ( $editor, $data ) { $editor ->field( 'idunic' ) ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] ); } ) ->on( 'preEdit', function ( $editor, $data ) { $editor ->field( 'idunic' ) ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] ); } ) ->where( $key = 'loc', $value = '', $op = '!=' ) ->process( $_POST ) ->json();

    javascript code:



    <script> var editor; // use a global for the submit and return data rendering in the examples $( document ).ready(function() { editor = new $.fn.dataTable.Editor( { ajax: "ajax/loc.php", table: "#loc", fields: [ {label: "Parcela:", name: "parcela", type: "select"}, {label: "Zona:", name: "zona", type: "select"}, {label: "Rand:", name: "randx"}, {label: "Loc:", name: "loc"}, {label: "Nord:", name: "nord"}, {label: "Sud:", name: "sud"}, {label: "Est:", name: "est"}, {label: "Vest:", name: "vest"}, {label: "Id Unic:", name: "idunic"} ] } ); var table = $('#loc').DataTable( { ajax: { url: "ajax/loc.php", type: "POST" }, processing: true, serverSide: true, columns: [ { data: "parcela" }, { data: "zona" }, { data: "randx" }, { data: "loc" }, { data: "nord" }, { data: "sud" }, { data: "est" }, { data: "vest" }, { data: "idunic"} ], select: true } ); // Display the buttons new $.fn.dataTable.Buttons( table, [ { extend: "create", editor: editor }, { extend: "edit", editor: editor }, { extend: "remove", editor: editor }, { extend: 'collection', text: 'Export', buttons: ['copy', 'excel', 'csv', 'pdf', 'print']} ] ); table.buttons().container() .appendTo( $('.col-sm-6:eq(0)', table.table().container() ) ); }); </script>
  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0

    and if add preEdit gives error"

     ->on( 'preEdit', function ( $editor, $data ) {
            $editor
               ->field( 'idunic' )
               ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] );
        } )
    

    errors:

    <br />
    <b>Warning</b>:  Illegal string offset 'parcela' in <b>/home/cim/public_html/administrare/app/ajax/loc.php</b> on line <b>40</b><br />
    <br />
    <b>Warning</b>:  Illegal string offset 'zona' in <b>/home/cim/public_html/administrare/app/ajax/loc.php</b> on line <b>40</b><br />
    <br />
    <b>Warning</b>:  Illegal string offset 'randx' in <b>/home/cim/public_html/administrare/app/ajax/loc.php</b> on line <b>40</b><br />
    <br />
    <b>Warning</b>:  Illegal string offset 'loc' in <b>/home/cim/public_html/administrare/app/ajax/loc.php</b> on line <b>40</b><br />
    {"fieldErrors":[{"name":"idunic","status":"This field must have a unique value"}],"data":[]}
    
  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    ->on( 'preEdit', function ( $editor, $data ) {

    The arguments are wrong here. It should be:

    ->on( 'preEdit', function ( $editor, $id, $data ) {

    See the PHP events documentation for the full list of parameters and their description.

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0
    edited December 2015

    i made the changes and nope validate still doesnt works, please check
    php script:

    <?php
     
    /*
     * Example PHP implementation used for the index.html example
     */
     
    // DataTables PHP library
    include( "../php/DataTables.php" );
     
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
     
     
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'loc' )
        ->fields(
            Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ),
            Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'randx' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'loc' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ),
            Field::inst( 'idunic' ) -> set( Field::SET_CREATE ) -> validator( 'Validate::unique' ),
            Field::inst( 'idunic' ) -> set( Field::SET_EDIT ) -> validator( 'Validate::unique' )
        ) ->on( 'preCreate', function ( $editor, $data ) {
            $editor
               ->field( 'idunic' )
               ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] );
        } ) ->on( 'preEdit', function ( $editor, $id, $data ) {
            $editor
               ->field( 'idunic' )
               ->setValue( $data['parcela'].'-'.$data['zona'].'-'.$data['randx'].'-'.$data['loc'] );
        } ) 
        ->where( $key = 'loc', $value = '', $op = '!=' )
        ->process( $_POST )
        ->json();
    
    
    
    
  • allanallan Posts: 61,734Questions: 1Answers: 10,110 Site admin

    Yes - that isn't going work either since the value won't have been created at the point the validator is run.

    Sorry to lead you up the garden path here - it sounds like client-side code is going to be the best option here if you want a unique validator - specifically use preSubmit:

    editor.on( 'preSubmit', function ( e, d, action ) {
      if ( action === 'create' || action === 'edit' ) {
        $.each( d.data, function ( key, row ) {
          row.idunic = row.parcela+'-'+row.zona+'-'+row.randx+'-'+row.loc;
        } );
      }
    }
    

    That will submit the required idunic value which can then be validated like any other field.

    Allan

  • robertutzurobertutzu Posts: 11Questions: 1Answers: 0

    just beautiful, works amazing

    many thanks Allan

This discussion has been closed.