【问题标题】:Issue in Validation when updating the record : Laravel 5.2更新记录时验证问题:Laravel 5.2
【发布时间】:2016-01-07 17:58:57
【问题描述】:

我的规则如下。请检查以下代码中的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 组合的错误。我认为验证代码只是为了在插入新记录之前进行检查。对于更新它不起作用。

下面是子类表的Sc​​hema

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

【问题讨论】:

    标签: php laravel-5.1 laravel-5.2


    【解决方案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'
        ];
    }
    

    现在让我解释一下上面的代码发生了什么

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

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

    通过这种方式,我开始知道在whereNotIn 块中放置什么列。因此,在我的情况下,它是 whereNotIn 中的 SubCategoryID 值。最后在验证函数中,# 被从列 Name 中删除。

    【讨论】:

    • 你使用*是不好的编码习惯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2015-12-26
    • 2016-07-25
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多