Validation using PHP library

Validation using PHP library

tadasstyrtadasstyr Posts: 11Questions: 5Answers: 0
edited February 2019 in General

I have this array

        Array
        (
            [action] => create
            [data] => Array
                (
                    [0] => Array
                        (
                            [employee_age] => 8
                            [employee_name] => Patrik
                            [employee_salary] => 1233
                            [lat] => 55.3324
                            [lng] => 25.32342
                        )
        
                )
        
        )

Can I use Datables Editor php validation library in this case :

$data['employee_age'] = $_POST['data'][0]["employee_age"];

Below function do not work:
Field::inst("employee_age")
->validator(Validate::minNum( 16 ));

Not sure if it helps but this is debuger information: ifezor

Answers

  • allanallan Posts: 52,525Questions: 1Answers: 8,020 Site admin

    Can you show me your full PHP code for Editor please?

    Thanks,
    Allan

  • tadasstyrtadasstyr Posts: 11Questions: 5Answers: 0

    Hi,

    This is Javascript file with cutted out some unnecessary info

                        editor = new $.fn.dataTable.Editor( {
                                ajax: "employee.php",
                                rowId: 'id',
                                "idSrc": "id",
                                table: "#example",
                                dom: 'Bfrtip',
                                fields: [ {
                                        label: "Age:",
                                        name: "employee_age"
                                    }, {
                                        label: "Name:",
                                        name: "employee_name"
                                    }, {
                                        label: "Salary:",
                                        name: "employee_salary"
                                    }, {
                                        label: "Latitude:",
                                        name: "lat"
                                    }, {
                                        label: "Longitude:",
                                        name: "lng"
                                    }]
                            } );
    
    
     var url = 'employee.php';
                        table = $('#example').DataTable({
                                dom: 'Bfrtip',
                                ajax: url,
                                rowId: 'id',
                                "idSrc": "id",
                                rowReorder: {
                                    dataSrc: 'order',
                                    selector: 'tr'
                                },
    
                                columns: [
                                    {
                                        data: null,
                                        defaultContent: '',
                                        className: 'select-checkbox',
                                        orderable: false
                                    },
    
    
                                    {
                                        data: 'employee_age'
                                    }, {
                                        data: 'employee_name'
                                    }, {
                                        data: 'employee_salary'
                                    }, {
                                        data: 'lat'
                                    }, {
                                        data: 'lng'
                                    }],
                                select: true,
                                buttons: [
                                    {
                                        extend: "create", editor: editor
                                    },
                                    {
                                        extend: "edit",   editor: editor,formMessage: 'Enter the data for the new row and click the "Save" button below to save permanently.'
                                    },
                                    {
                                        extend: "remove", editor: editor
                                    }
                                ]
                            });
    
    

    This is PHP file with cutted out some unnecessary info

    <?php
    include ("connection.php");
    include( "../Editor-PHP-1.8.1/lib/DataTables.php" );
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;
    
    $empCls = new Employee(); //sukuriam objekta
    $empCls->getList(); //kreipiames i funkcija
    class Employee
    {
        protected $conn;
        protected $data = array();
        function __construct()
        {
            $db         = new dbObj();
            $connString = $db->getConnstring();
            $this->conn = $connString;
        }
        function insertEmployee()
        {
            $employee_data = array();
            $resp['status'] = false;
            $data['employee_name'] = $_POST['data'][0]["employee_name"];
            $data['employee_salary'] = $_POST['data'][0]["employee_salary"];
            $data['employee_age'] = $_POST['data'][0]["employee_age"];
            $data['lat'] = $_POST['data'][0]["lat"];
            $data['lng'] = $_POST['data'][0]["lng"];
    
            $sql = "INSERT INTO employee (employee_name,employee_salary,employee_age,lat,lng) VALUES ('" . $data['employee_name'] . "'," . (int)$data['employee_salary'] . ",  " . (int)$data['employee_age'] . "," .$data['lat'] . "," . (float)$data['lng'] . "  ) RETURNING id;";
            $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
            $idresult = pg_fetch_object($queryRecords);
            $employee_data['data'][] = array('id'             => $idresult->id,'employee_name'  => $data['employee_name'],'employee_salary'=> $data['employee_salary'],'employee_age'   => $data['employee_age'],'lat'            => $data['lat'],'lng'            => $data['lng']);
            header('Content-Type: application/json');
            die(json_encode($employee_data));
        }
    
    function getList()
        {
            if ($_POST) {
                if ($_POST['action'] == 'remove') {
                    $this->deleteEmployee();
                } else
                if ($_POST['action'] == 'create') {
                    $this->insertEmployee();
                } else
                if ($_POST['action'] == 'edit') {
                    $this->updateEmployee();
                }
            } else {
                $employee_data = $this->getEmployeesDT();
                header('Content-Type: application/json');
                die(json_encode($employee_data));
            }
        }
    
     function getList()
        {
            if ($_POST) {
                if ($_POST['action'] == 'remove') {
                    $this->deleteEmployee();
                } else
                if ($_POST['action'] == 'create') {
                    $this->insertEmployee();
                } else
                if ($_POST['action'] == 'edit') {
                    $this->updateEmployee();
                }
            } else {
                $employee_data = $this->getEmployeesDT();
                header('Content-Type: application/json');
                die(json_encode($employee_data));
            }
        }
    }
    

    We managed to make validator to work like this, but maybe it is easer way

            $error = array('fieldErrors'=>array());
            if ((int)$data['employee_age'] < 10 ) {
                $error["fieldErrors"][] = array("name"  =>"employee_age","status"=>"test");
            }
    
            if (!empty(    $error["fieldErrors"])) {
                $error['data'] = array();
                die(json_encode($error));
            }
    
  • allanallan Posts: 52,525Questions: 1Answers: 8,020 Site admin

    I don't actually see the minNum validator being applied anywhere there. Am I missing it?

    Allan

  • tadasstyrtadasstyr Posts: 11Questions: 5Answers: 0

    We have tried to use validation like in this example and it didin't worked.

    function insertEmployee()
        {
            $employee_data = array();
            $resp['status'] = false;
            $data['employee_name'] = $_POST['data'][0]["employee_name"];
            $data['employee_salary'] = $_POST['data'][0]["employee_salary"];
            $data['employee_age'] = $_POST['data'][0]["employee_age"];
            $data['lat'] = $_POST['data'][0]["lat"];
            $data['lng'] = $_POST['data'][0]["lng"];
        
        Field::inst( "employee_age" )
                ->validator( Validate::minNum(
            16,
            new ValidateOptions::inst()
                ->message( 'You are to young :)' )
        ) );
    
        
            $sql = "INSERT INTO employee (employee_name,employee_salary,employee_age,lat,lng) VALUES ('" . $data['employee_name'] . "'," . (int)$data['employee_salary'] . ",  " . (int)$data['employee_age'] . "," .$data['lat'] . "," . (float)$data['lng'] . "  ) RETURNING id;";
            $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
            $idresult = pg_fetch_object($queryRecords);
            $employee_data['data'][] = array('id'             => $idresult->id,'employee_name'  => $data['employee_name'],'employee_salary'=> $data['employee_salary'],'employee_age'   => $data['employee_age'],'lat'            => $data['lat'],'lng'            => $data['lng']);
            header('Content-Type: application/json');
            die(json_encode($employee_data));
        }
    
    
  • allanallan Posts: 52,525Questions: 1Answers: 8,020 Site admin

    I don't see your Field instance being assigned to anything. Its creating a Field instance, but nothing ever calls it.

    You need to attach it to an Editor instance using the Editor->field() method.

    Allan

  • olidevolidev Posts: 1Questions: 0Answers: 0

    Which PHP library you are using for the validation? There are a lot of them out there you know?

  • tadasstyrtadasstyr Posts: 11Questions: 5Answers: 0

    Allan. If you have possibility, can you explain how "You need to attach it to an Editor instance using the Editor->field() method." to make it function according sample code which is above. I just need example how to validate using native Editor tool if salary is number or not empty and after that I can figure out why and how it works...

  • colincolin Posts: 9,558Questions: 0Answers: 1,582

    This manual page here should help - it's explaining how to define fields.

    Colin

  • tadasstyrtadasstyr Posts: 11Questions: 5Answers: 0
    edited December 2019

    I'm not very experienced in PHP so maybe i miss something, but issue is that i want(I need) to use custom SQL, in my simple example above to insert data I use

     $sql = "INSERT INTO employee (employee_name,employee_salary,employee_age,lat,lng) VALUES ('" . $data['employee_name'] . "'," . (int)$data['employee_salary'] . ",  " . (int)$data['employee_age'] . "," .$data['lat'] . "," . (float)$data['lng'] . "  ) RETURNING id;";
            $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
    

    I do not use

    $editor = Editor::inst( $db, 'employee' );
    ...........
    

    Therefore i do not know how to skip $editor = Editor::inst( $db, 'employee' );... part and still be able to use validation.

  • allanallan Posts: 52,525Questions: 1Answers: 8,020 Site admin

    Therefore i do not know how to skip $editor = Editor::inst( $db, 'employee' );... part and still be able to use validation.

    You cannot I'm afraid. If you want to write your own query to enter information in the database, you'd also need your own validation.

    That said, I don't see anything in your custom SQL query that PHP / Postgres wouldn't sort out themselves. The Editor libraries should be able to construct that insert for you.

    Have you tried using them, and if so, did you run into any issues?

    Thanks,
    Allan

Sign In or Register to comment.