复合键验证不起作用:Laravel 5.2
Composite Key validation not working: Laravel 5.2
到目前为止我尝试了什么?
我的要求class如下
class SubCategoryRequest extends Request
{
public function authorize()
{
if(\Auth::user()->RoleID == \App\Enumeration\Role\RoleType::Admin) {
return true;
}
return false;
}
public function rules()
{
$rules = [];
$rules['SubCategory'] = 'required|max:25|min:5|unique:tblsubcategory,CategoryID';
$rules['CategoryID'] = 'required';
return $rules;
}
public function response(array $errors){
return \Redirect::back()->withErrors($errors)->withInput();
}
}
下面是数据库Table架构
CREATE TABLE `tblsubcategory` (
`SubCategoryID` int(11) NOT NULL,
`SubCategory` varchar(25) NOT NULL,
`CategoryID` int(11) NOT NULL,
`IsActive` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tblsubcategory`
MODIFY `SubCategoryID` int(11) NOT NULL AUTO_INCREMENT;
下面是唯一键约束
ALTER TABLE `tblsubcategory`
ADD PRIMARY KEY (`SubCategoryID`),
ADD UNIQUE KEY `UK_tblSubCategory_SubCategory_CategoryID` (`CategoryID`,`SubCategory`);
问题
我是不是漏掉了什么?
我不是很明白你的问题,但是你的复合键是CategoryID对吧?所以您想验证 CategoryID 和 SubCategory 的输入字段在输入数据时不应该为空并且是唯一的?你能 post 你的 HTML 表格吗?
无论如何,你可以在你的请求中尝试这样的事情 class:
public function rules()
{
return [
'txtSubCategory' => 'required|max:25|min:5|unique:tblsubcategory,SubCategory',
'txtCategoryID' => 'required|unique:tblsubcategory,CategoryID'
];
}
如果违反规则则显示消息:
public function messages () {
return [
'txtSubCategory.required' => 'Sub category name cannot be empty !!',
'txtSubCategory.unique' => 'Sub category name is exist !!',
'txtCategoryID.required' => 'Category ID cannot be empty !!',
'txtCategoryID.unique' => 'Category ID is exist !!'
];
}
然后,您的 html 表单必须类似于:
<form action="" method="">
<div>
<label>Sub Category</label>
<input name="txtSubCategory" placeholder="Please Enter Sub Category" />
</div>
<div>
<label>Category ID</label>
<input name="txtCategoryID" placeholder="Please Enter Category ID" />
</div>
</form>
并使用它在您的 html 页面中显示错误消息:
@if (count($errors) > 0)
<div>
<ul>
@foreach ($errors->all() as $error)
<li>{!! $error !!}</li>
@endforeach
</ul>
</div>
@endif
Reference. As per laracasts.com
方法 #1
到classValidationServiceProvider
,你可以在下面提到的路径中找到它。
vendor\laravel\framework\src\Illuminate\Validation\ValidationServiceProvider.php
在此class
中添加以下方法
public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
// remove first parameter and assume it is the table name
$table = array_shift( $parameters );
// start building the conditions
$fields = [ $attribute => $value ];
// iterates over the other parameters
//and build the conditions for all the required fields
while ( $field = array_shift( $parameters ) ) {
$fields[ $field ] = \Request::get( $field );
}
// query the table with all the conditions
$result = \DB::table( $table )->select( \DB::raw( 1 ) )->where( $fields )->first();
return empty( $result ); // edited here
});
}
最终规则将如下所示。
public function rules()
{
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
方法 #2
规则函数必须如下所示
public function rules()
{
// extends Validator only for this request
\Validator::extend( 'composite_unique',
function ( $attribute, $value, $parameters, $validator ) {
// remove first parameter and assume it is the table name
$table = array_shift( $parameters );
// start building the conditions
$fields = [ $attribute => $value ]; // current field, SubCategory in my case
// iterates over the other parameters
//and build the conditions for all the required fields
while ( $field = array_shift( $parameters ) ) {
$fields[ $field ] = $this->get( $field );
}
// query the table with all the conditions
$result = \DB::table( $table )->select( \DB::raw( 1 ) )->where( $fields )->first();
return empty( $result ); // edited here
}, 'Category and Sub Category combination already exists' );
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
到目前为止我尝试了什么?
我的要求class如下
class SubCategoryRequest extends Request
{
public function authorize()
{
if(\Auth::user()->RoleID == \App\Enumeration\Role\RoleType::Admin) {
return true;
}
return false;
}
public function rules()
{
$rules = [];
$rules['SubCategory'] = 'required|max:25|min:5|unique:tblsubcategory,CategoryID';
$rules['CategoryID'] = 'required';
return $rules;
}
public function response(array $errors){
return \Redirect::back()->withErrors($errors)->withInput();
}
}
下面是数据库Table架构
CREATE TABLE `tblsubcategory` (
`SubCategoryID` int(11) NOT NULL,
`SubCategory` varchar(25) NOT NULL,
`CategoryID` int(11) NOT NULL,
`IsActive` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tblsubcategory`
MODIFY `SubCategoryID` int(11) NOT NULL AUTO_INCREMENT;
下面是唯一键约束
ALTER TABLE `tblsubcategory`
ADD PRIMARY KEY (`SubCategoryID`),
ADD UNIQUE KEY `UK_tblSubCategory_SubCategory_CategoryID` (`CategoryID`,`SubCategory`);
问题
我是不是漏掉了什么?
我不是很明白你的问题,但是你的复合键是CategoryID对吧?所以您想验证 CategoryID 和 SubCategory 的输入字段在输入数据时不应该为空并且是唯一的?你能 post 你的 HTML 表格吗?
无论如何,你可以在你的请求中尝试这样的事情 class:
public function rules()
{
return [
'txtSubCategory' => 'required|max:25|min:5|unique:tblsubcategory,SubCategory',
'txtCategoryID' => 'required|unique:tblsubcategory,CategoryID'
];
}
如果违反规则则显示消息:
public function messages () {
return [
'txtSubCategory.required' => 'Sub category name cannot be empty !!',
'txtSubCategory.unique' => 'Sub category name is exist !!',
'txtCategoryID.required' => 'Category ID cannot be empty !!',
'txtCategoryID.unique' => 'Category ID is exist !!'
];
}
然后,您的 html 表单必须类似于:
<form action="" method="">
<div>
<label>Sub Category</label>
<input name="txtSubCategory" placeholder="Please Enter Sub Category" />
</div>
<div>
<label>Category ID</label>
<input name="txtCategoryID" placeholder="Please Enter Category ID" />
</div>
</form>
并使用它在您的 html 页面中显示错误消息:
@if (count($errors) > 0)
<div>
<ul>
@foreach ($errors->all() as $error)
<li>{!! $error !!}</li>
@endforeach
</ul>
</div>
@endif
Reference. As per laracasts.com
方法 #1
到classValidationServiceProvider
,你可以在下面提到的路径中找到它。
vendor\laravel\framework\src\Illuminate\Validation\ValidationServiceProvider.php
在此class
中添加以下方法public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
// remove first parameter and assume it is the table name
$table = array_shift( $parameters );
// start building the conditions
$fields = [ $attribute => $value ];
// iterates over the other parameters
//and build the conditions for all the required fields
while ( $field = array_shift( $parameters ) ) {
$fields[ $field ] = \Request::get( $field );
}
// query the table with all the conditions
$result = \DB::table( $table )->select( \DB::raw( 1 ) )->where( $fields )->first();
return empty( $result ); // edited here
});
}
最终规则将如下所示。
public function rules()
{
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
方法 #2
规则函数必须如下所示
public function rules()
{
// extends Validator only for this request
\Validator::extend( 'composite_unique',
function ( $attribute, $value, $parameters, $validator ) {
// remove first parameter and assume it is the table name
$table = array_shift( $parameters );
// start building the conditions
$fields = [ $attribute => $value ]; // current field, SubCategory in my case
// iterates over the other parameters
//and build the conditions for all the required fields
while ( $field = array_shift( $parameters ) ) {
$fields[ $field ] = $this->get( $field );
}
// query the table with all the conditions
$result = \DB::table( $table )->select( \DB::raw( 1 ) )->where( $fields )->first();
return empty( $result ); // edited here
}, 'Category and Sub Category combination already exists' );
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}