Validation with inline editing in jQuery datatables after trimming spaces

Using the jQuery datatable editor plugin, the following code works as intended. It performs these checks (some of the fields were omitted for brevity).

Editor::inst( $db, 'file_upload' )
    ->fields(
        Field::inst( 'id' )->validator( 'Validate::notEmpty' ),

        Field::inst( 'name' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            return $length > 30 ? 'Length must be 30 characters or less' : true;
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        }),

        Field::inst( 'document_title' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            return $length > 50 ? 'Length must be 50 characters or less' : true;
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        }),

        Field::inst( 'email_address' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            return $length > 60 ? 'Length must be 60 characters or less' : true;
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        })
    )->where( function ( $q ) {
        $q->where( 'file_type', "('jpg', 'jpeg', 'gif', 'png')", 'IN', false );
    })->process( $_POST )
    ->json();

But when the validation logic is slightly changed, as shown below,

Editor::inst( $db, 'file_upload' )
    ->fields(
        Field::inst( 'id' )->validator( 'Validate::notEmpty' ),

        Field::inst( 'name' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            // The following line has been modified
            return $length === 0 ? 'This field is required' : ($length > 30 ? 'Length must be 30 characters or less' : true);
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        }),

        Field::inst( 'document_title' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            // The following line has been modified
            return $length === 0 ? 'This field is required' : ($length > 50 ? 'Length must be 50 characters or less' : true);
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        }),

        Field::inst( 'email_address' )->validator( 'Validate::notEmpty' )
        ->validator( function ($val, $data, $opts) {
            $length = strlen(trim(preg_replace('/\s+/', ' ', $val)));
            // The following line has been modified
            return $length === 0 ? 'This field is required' : ($length > 60 ? 'Length must be 60 characters or less' : true);
        })->getFormatter( function ( $val, $data, $opts ) {
            return htmlspecialchars($val, ENT_QUOTES, "UTF-8");
        })->setFormatter( function ( $val, $data, $opts ) {
            return trim(preg_replace('/\s+/', ' ', $val));
        })
    )->where( function ( $q ) {
        $q->where( 'file_type', "('jpg', 'jpeg', 'gif', 'png')", 'IN', false );
    })->process( $_POST )
    ->json();

In this case, the checks are performed as they should, but the values ​​are not transferred (and the data are not updated at the same time) to the database. The embedded editing text field remains open after pressing the enter key.

What could be the reason and how to fix it? Perhaps I am missing something very important about PHP.

If necessary, send the appropriate client script.


, , , , . .

?

+4
1

- , , , , . questions PHP , PHP- , . if/else, , ,

if($length === 0){
    return 'This field is required';
}
else if($length > 50){
    return 'Length must be 50 characters or less';
}
else{
    return true;
}

, , , , , , , , - nesting; -.

, PHP.

, submit, JavaScript DataTables, , CRUD ( , , JS, Firebug Firefox, ). if/else , , , ( , ).

+1

All Articles