php – 修剪空格后在jQuery数据表中使用内联编辑进行验证
使用jQuery数据表编辑器插件,以下代码按预期工作.它执行指定的验证(为简洁起见,省略了一些字段).
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,$opts ) { return htmlspecialchars($val,ENT_QUOTES,"UTF-8"); })->setFormatter( function ( $val,$opts ) { return trim(preg_replace('/s+/',$val)); }),Field::inst( 'document_title' )->validator( 'Validate::notEmpty' ) ->validator( function ($val,$val))); return $length > 50 ? 'Length must be 50 characters or less' : true; })->getFormatter( function ( $val,Field::inst( 'email_address' )->validator( 'Validate::notEmpty' ) ->validator( function ($val,$val))); return $length > 60 ? 'Length must be 60 characters or less' : true; })->getFormatter( function ( $val,$val)); }) )->where( function ( $q ) { $q->where( 'file_type',"('jpg','jpeg','gif','png')",'IN',false ); })->process( $_POST ) ->json(); 但是当验证逻辑稍微修改如下, Editor::inst( $db,$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,$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,$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,false ); })->process( $_POST ) ->json(); 在这种情况下,验证将按原样执行,但不会向数据库提交值(并且数据表不会同时更新).按下回车键后,内联编辑文本框仍保持打开状态. 可能是什么原因以及如何解决?可能,我遗漏了一些关于PHP的基本知识. 如果需要,我将发布相应的客户端脚本. 当强制执行额外条件以防止将输入值提交到抽象层数据库时,似乎会触发其他验证器.在内联单元格编辑的情况下不应该发生这种情况. 什么是补救措施? 解决方法
如果代码的唯一更改是您用注释突出显示的那些行,我怀疑问题是您使用嵌套的三元运算符. PHP标签中的这些
two单独的
questions可能会清除一些内容,但基本上快速版本是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; } 虽然这可能会更长,但调试可能会容易得多,我怀疑根据你的问题,如果这一切都被改变了,你的问题可以归结为left-associative ternary operator nesting;而在几乎所有其他语言中,三元运算符都是右关联的. 这里是one more link推荐PHP中的嵌套三元运算符. 在提交提交后保持打开的文本框是DataTables JavaScript表单错误的标准,这在CRUD操作服务器端未向客户端返回预期值时很常见(检查浏览器开发人员控制台以确保您没有收到JS错误,如果你想要一个非常强大的版本,我建议你使用Firebug for Firefox.如果修改你的代码后使用if / else块而不是三元运算符你仍然有错误,那么我会调查你的客户端代码以确保没有其他任何改变(如果这不是你可以在问题中发布它解决你的问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |