Using the jQuery datatable editor plug-in, the following code works as intended. It performs specified validations (some of the fields have been 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 modified like the following,
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, validations are performed as they should but values are not submitted (and concurrently the datatable is not updated) to the database. The inline editing textbox remains open after the enter key is pressed.
What could be the reason and how to fix it? Possibly, I am missing something very basic about PHP.
I will post the corresponding client script, if needed.
It appears that other validators are triggered, when extra conditions are enforced preventing input values from being submitted to the abstract layer, database. This should not happen in case of inline cell editing.
What is the remedy?