更新记录时的验证问题:Laravel 5.2

时间:2016-01-07 17:58:57

标签: php laravel-5.1 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();
}

我的Validator类如下所示。请检查以下代码中的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的组合。我认为验证代码只是在插入新记录之前进行检查。对于更新,它无法正常工作。

以下是SubCategory表的架构

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`);

1 个答案:

答案 0 :(得分:1)

我在ValidationServiceProvider类中的引导函数中做了一些操作。以下是完成的工作。

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'
    ];
}

现在让我解释上面代码中发生的事情

在rules函数中,您可以检查最后一个逗号分隔值是#SubCategoryID#。这是因为我的查询如下所示。

Select SubCategoryID from tblSubCategory 
Where (CategoryID = ? and SubCategory = ?) 
and SubCategoryID not in(?)

通过这种方式,我将了解whereNotIn块中要放置的列。所以,就我而言,它是whereNotIn中的SubCategoryID值。最后,在验证函数中,#正从名称列中删除。