我的请求类位于
之下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();
}
}
以下是数据库表架构
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`);
问题
我错过了什么吗?
答案 0 :(得分:0)
我不明白你的问题,但你的复合键是CategoryID吗?所以你想要验证CategoryID和SubCategory的输入字段不应该是空的&输入数据时要独特吗?你可以发布你的HTML表单吗?
无论如何,您可以在Request类中尝试这样的事情:
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
答案 1 :(得分:0)
转到课程ValidationServiceProvider
,您可以在下面提到的路径中找到它。
供应商\ laravel \框架\ SRC \照亮\验证\ ValidationServiceProvider.php
在此课程中添加以下方法
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'
];
}
规则功能必须如下
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'
];
}