在我的控制器中,我使用验证器检查唯一的company_name,但在传递已存在的公司名称时显示错误。正好与其他文本一起使用。
$input['company_name'] = Input::get('company_name');
$rules = array('company_name' => "unique:subcontractors,company_name,$id");
$validator_cn = Validator::make($input, $rules);
if ($validator_cn->fails()) {
return Redirect::back()->with('alert-danger',"Company Name already exist.")->withInput();
}
给出错误" helpers.php第519行:"
htmlspecialchars() expects parameter 1 to be string, array given
查看
@extends('layouts.master')
@section('content')
@if (Session::get('alert-danger') || Session::get('alert-success') )
<script>
@if(Session::get('alert-danger'))
show_notification("error","{{ Session::get('alert-danger') }}");
@endif
@if(Session::get('alert-success'))
show_notification("success","{{ Session::get('alert-success') }}");
@endif
</script>
@endif
<!--------------DATA TABLE HEADER----------------------->
<!-- Main content -->
<div class="content-wrapper">
<!-- Page header -->
<!-- /page header -->
<!-- Content area -->
<div class="content">
<div class="panel panel-white">
<?php
$id = '';
$company_name = '';
$addressline1 = '';
$addressline2 = '';
$addressline3 = '';
if(!empty($request))
{
$company_name = $request->old('company_name');
$addressline1 = $request->old('addressline1');
$addressline2 = $request->old('addressline2');
$addressline3 = $request->old('addressline3');
$evaluation_comments = $request->old('evaluation_comments');
}
else if(isset($subcontractor_detail))
{
$subcontractor_id = $subcontractor_detail->id;
$company_name = $subcontractor_detail->company_name;
$addressline1 = $subcontractor_detail->addressline1;
$addressline2 = $subcontractor_detail->addressline2;
$addressline3 = $subcontractor_detail->addressline3;
}
?>
{{ Form::open(array('id'=>'subcontractorForm','enctype'=>"multipart/form-data",'class' => 'steps-validation','url' => (isset($subcontractor_detail)) ? "/subcontractors/$subcontractor_id" : "/subcontractors", 'method' => (isset($subcontractor_detail)) ? "put" : "post")) }}
@if(isset($subcontractor_detail)) {{ Form::hidden('id', $subcontractor_id)}}@endif
<h6>Subcontractor Details</h6>
<fieldset>
<legend class="text-bold border-info text-info">Subcontractor Details</legend>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Company Name: <span class="text-danger">*</span></label>
<div class="input-group">
<span class="input-group-addon"><i class="icon-location3"></i></span>
{{ Form:: text('company_name', $company_name, array('id'=>'company_name','class' => 'form-control initial_cap', 'required' => '', 'placeholder' => 'Company Name', 'maxlength' => '100')) }}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Address Line1: <span class="text-danger">*</span></label>
<div >{{ Form:: text('addressline1', $addressline1, array('class' => 'form-control initial_cap','required' => '', 'placeholder' => 'Address Line1', 'maxlength' => '100')) }}</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Address Line2: <span class="text-danger">*</span></label>
<div >{{ Form:: text('addressline2', $addressline2, array('class' => 'form-control initial_cap', 'required' => '','placeholder' => 'Address Line2', 'maxlength' => '100')) }}</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Address Line3: <span class="text-danger">*</span></label>
<div >{{ Form:: text('addressline3', $addressline3, array('class' => 'form-control initial_cap','required' => '', 'placeholder' => 'Address Line3', 'maxlength' => '100')) }}</div>
</div>
</div>
</div>
</fieldset>
{{ Form::close() }}
</div>
<!-- /wizard with validation -->
</div>
<!-- /content area -->
</div>
</script>
@stop
答案 0 :(得分:0)
试试这段代码。这有帮助!
$input['company_name'] = Input::get('company_name');
$rules = array(
'company_name' => 'unique:subcontractors,company_name,'.$id
);
$validator_cn = Validator::make($input, $rules);
if ($validator_cn->fails()) {
return Redirect::back()->with(['alert-danger'=>"Company Name already exist."])->withInput();
}
答案 1 :(得分:0)
只有在$id
有某些值时才需要添加"unique:subcontractors,company_name".($id ? ",$id" : '')
,否则不要像
$input['company_name'] = Input::get('company_name');
$rules = array('company_name' => "unique:subcontractors,company_name".($id ? ",$id" : ''));
$validator_cn = Validator::make($input, $rules);
if ($validator_cn->fails()) {
return Redirect::back()->with('alert-danger',"Company Name already exist.")->withInput();
}
完整代码,
unique:table,column,except,idColumn
根据unique syntax(id
),如果您想使用"unique:subcontractors,company_name".($id ? ",id,$id" : '')
,那么请使用,
onclick