当我尝试更新信息时遇到问题,按下按钮编辑信息会加载一个视图,该视图具有从表中获取信息的相同文本字段,但是当我编辑它们并尝试提交请求时出现错误,因为我在请求中定义的ruc属性中的示例required|numeric|max:13
,当提交已输入数据的表单时,屏幕上会出现必须输入字段或必须是数字的字段,也就是说,就好像我的表单发送了几个字段并且警报消失了。即使我不编辑表中的信息并使用相同的信息提交,警报仍然会响起。
我打算更新的表
要进入该视图,我按下索引中的编辑按钮,这是代码。
<a href="/info_junta/{{$i->idjunta}}/edit" class="btn btn-success"><li class="fa fa-edit"></li> Editar información</a>
控制器
<?php
namespace juntaAgua\Http\Controllers;
use juntaAgua\InfoJunta;
use juntaAgua\Http\Requests\InfoJuntaRequest;
use Illuminate\Http\Request;
use juntaAgua\Http\Requests;
use DB;
use Response;
use Illuminate\Support\Facades\Input;
class MantenimientoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if ($request) {
$query = DB::table('junta')
->where('idjunta', '=', '1')
->get();
return view('mantenimiento.info_junta', ["infoJunta" => $query]);
} else {
return 'No existe informacion referente a la Junta';
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return view('mantenimiento.edit_junta', ["infoJunta" => InfoJunta::findOrFail($id)]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(InfoJuntaRequest $request, $id)
{
$infoJunta = InfoJunta::findOrFail($id);
$infoJunta->nombre = $request->get('nombre');
$infoJunta->mision = $request->get('mision');
$infoJunta->vision = $request->get('vision');
$infoJunta->ruc = $request->get('ruc');
$infoJunta->logo = $request->get('logo');
$infoJunta->update();
return Redirect::to('/info_junta');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
return 'intento delete!';
}
}
要求
<?php
namespace juntaAgua\Http\Requests;
use juntaAgua\Http\Requests\Request;
class InfoJuntaRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'nombre'=>'required|max:100',
'info'=>'max:500',
'logo'=>'max:100',
'ruc'=>'required|max:13|numeric',
'mision'=>'max:500',
'vision'=>'max:500'
];
}
}
编辑表格
@extends('layouts.admin')
@section('contenido')
<div id="lol">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">Datos informativos de la Junta Administradora de Agua</div>
<div class="panel-body">
@if(count($errors)>0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
<div class="row">
{!!Form::model($infoJunta,['method'=>'patch','route'=>['info_junta.destroy',$infoJunta->idjunta]])!!}
{{Form::token()}}
<div class="form-group has-success">
<div class="col-lg-6 col-sm-6 col-md-6 col-xs-6">
<label for="">IdJunta</label>
<input disabled type="text" class="form-control" value="{{$infoJunta->idjunta}}">
</div>
<div class="col-lg-6 col-sm-6 col-md-6 col-xs-6">
<label for="">Ruc</label>
<input type="number" class="form-control" name="ruc" value="{{$infoJunta->ruc}}">
</div>
<div class="col-lg-12">
<label for="">Nombre de la Junta</label>
<input type="text" class="form-control" name="nombre"
value="{{$infoJunta->nombre}}">
</div>
<div class="col-lg-6">
<label for="">Misión</label>
<textarea class="form-control" name="mision" id="" cols="30"
rows="10">{{$infoJunta->mision}}</textarea>
</div>
<div class="col-lg-6">
<label for="">Visión</label>
<textarea class="form-control" name="vision" id="" cols="30"
rows="10">{{$infoJunta->vision}}</textarea>
</div>
<div class="col-lg-12">
<label for="">Foto</label>
<input type="file" class="form-control" name="foto" value="{{$infoJunta->logo}}">
</div>
</div>
</div>
</div>
<div class="panel-footer text-center">
<button class="btn btn-success" type="submit">
<li class="fa fa-save"></li>
Actualizar
</button>
{!!Form::close()!!}
<a href="/info_junta" class="btn btn-default"><li class="fa fa-times"></li> Cancelar</a>
</div>
</div>
</div>
</div>
</div>
@endsection
路由.php
Route::resource('/info_junta','MantenimientoController');
php工匠路线:列表
| | GET|HEAD | info_junta/create | info_junta.create | juntaAgua\Http\Controllers\MantenimientoController@create | web |
| | GET|HEAD | info_junta/{info_junta} | info_junta.show | juntaAgua\Http\Controllers\MantenimientoController@show | web |
| | PUT|PATCH | info_junta/{info_junta} | info_junta.update | juntaAgua\Http\Controllers\MantenimientoController@update | web |
| | DELETE | info_junta/{info_junta} | info_junta.destroy | juntaAgua\Http\Controllers\MantenimientoController@destroy | web |
| | GET|HEAD | info_junta/{info_junta}/edit | info_junta.edit | juntaAgua\Http\Controllers\MantenimientoController@edit | web |
我认为您在滥用
max:13
. 正如文档所说,此规则以整数验证特定数字,而不是字段的长度。https://laravel.com/docs/5.4/validation#rule-size
如果要验证位数,可以使用
digits:13
if 数字始终为 13 位,或digits_between:1,13
假设允许 1-13 位。https://laravel.com/docs/5.4/validation#rule-digits
您误用了验证。你说'name'的类型是varchar(100),所以你必须验证一个字符串而不是一个数字。
这样您就说 Name 字段是必需的,它是字符串类型,可以有最小值 1 和最大值 100。