更新记录时验证问题:Laravel 5.2
Issue in Validation when updating the record : Laravel 5.2
我的规则如下。请检查下面代码中的composite_unique
public function rules()
{
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
我的更新方法如下...
public function update(SubCategoryRequest $request)
{
$SubCat = $this->CacheCollection->getAllSubCategories($request->input('CategoryID'));
$SubCategory = $SubCat->SubCategories->where('SubCategoryID', 1)->first();
$SubCategory->SubCategory = $request->input('SubCategory');
$SubCategory->CategoryID = $request->input('CategoryID');
$SubCategory->save();
}
我的验证器 class 如下所示。请检查下面代码中的 composite_unique 规则。
class ValidationServiceProvider extends ServiceProvider
{
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 ];
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
});
}
}
有什么问题
当我尝试更新记录但不编辑任何内容并单击更新按钮时...我收到重复 SubCategory 和 CategoryID 组合的错误。我认为验证代码只是为了在插入新记录之前进行检查。对于更新,它不起作用。
下面是子类别的架构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`);
我在ValidationServiceProvider
class里面的boot函数做了一些操作。以下是完成的内容。
class ValidationServiceProvider extends ServiceProvider
{
public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
$table = array_shift( $parameters );
$fields = [ $attribute => $value ];
$columnName = null;
$columnValue = null;
while ( $field = array_shift( $parameters ) ) {
if(strpos($field, '#') !== false) {
$columnName = str_replace("#", "", $field);
$columnValue = \Request::get( $columnName );
}
else
$fields[ $field ] = \Request::get( $field );
}
if($columnName == null && $columnValue == null) {
$result = \DB::table( $table )
->select( \DB::raw( 1 ) )
->where( $fields )->first();
}
else {
$result = \DB::table( $table )
->select( \DB::raw( 1 ) )
->where( $fields )
->whereNotIn($columnName, [$columnValue])
->first();
}
return empty( $result ); // edited here
});
}
}
最后是rules
函数
public function rules()
{
return [
'SubCategory' => 'required|composite_unique:tblsubcategory,CategoryID,#SubCategoryID#',
'CategoryID' => 'required|min:1'
];
}
现在让我解释一下上面代码中发生的事情
在规则函数中,您可以检查最后一个逗号分隔值是#SubCategoryID#
。这是因为我的查询如下所示。
Select SubCategoryID from tblSubCategory
Where (CategoryID = ? and SubCategory = ?)
and SubCategoryID not in(?)
这样我就知道要在 whereNotIn
块中放置什么列。因此,在我的例子中,它是 whereNotIn
中的 SubCategoryID 值。最后在验证函数中,# 正在从 Name 列中删除。
我的规则如下。请检查下面代码中的composite_unique
public function rules()
{
return [
'SubCategory' => 'required|max:25|min:5|composite_unique:tblsubcategory,CategoryID',
'CategoryID' => 'required|min:1'
];
}
我的更新方法如下...
public function update(SubCategoryRequest $request)
{
$SubCat = $this->CacheCollection->getAllSubCategories($request->input('CategoryID'));
$SubCategory = $SubCat->SubCategories->where('SubCategoryID', 1)->first();
$SubCategory->SubCategory = $request->input('SubCategory');
$SubCategory->CategoryID = $request->input('CategoryID');
$SubCategory->save();
}
我的验证器 class 如下所示。请检查下面代码中的 composite_unique 规则。
class ValidationServiceProvider extends ServiceProvider
{
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 ];
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
});
}
}
有什么问题
当我尝试更新记录但不编辑任何内容并单击更新按钮时...我收到重复 SubCategory 和 CategoryID 组合的错误。我认为验证代码只是为了在插入新记录之前进行检查。对于更新,它不起作用。
下面是子类别的架构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`);
我在ValidationServiceProvider
class里面的boot函数做了一些操作。以下是完成的内容。
class ValidationServiceProvider extends ServiceProvider
{
public function boot() {
$this->app['validator']->extend('composite_unique',
function ($attribute, $value, $parameters, $validator) {
$table = array_shift( $parameters );
$fields = [ $attribute => $value ];
$columnName = null;
$columnValue = null;
while ( $field = array_shift( $parameters ) ) {
if(strpos($field, '#') !== false) {
$columnName = str_replace("#", "", $field);
$columnValue = \Request::get( $columnName );
}
else
$fields[ $field ] = \Request::get( $field );
}
if($columnName == null && $columnValue == null) {
$result = \DB::table( $table )
->select( \DB::raw( 1 ) )
->where( $fields )->first();
}
else {
$result = \DB::table( $table )
->select( \DB::raw( 1 ) )
->where( $fields )
->whereNotIn($columnName, [$columnValue])
->first();
}
return empty( $result ); // edited here
});
}
}
最后是rules
函数
public function rules()
{
return [
'SubCategory' => 'required|composite_unique:tblsubcategory,CategoryID,#SubCategoryID#',
'CategoryID' => 'required|min:1'
];
}
现在让我解释一下上面代码中发生的事情
在规则函数中,您可以检查最后一个逗号分隔值是#SubCategoryID#
。这是因为我的查询如下所示。
Select SubCategoryID from tblSubCategory
Where (CategoryID = ? and SubCategory = ?)
and SubCategoryID not in(?)
这样我就知道要在 whereNotIn
块中放置什么列。因此,在我的例子中,它是 whereNotIn
中的 SubCategoryID 值。最后在验证函数中,# 正在从 Name 列中删除。