0
我的文章和标签表之间有多对多关系,并且希望用户在创建/编辑文章表单中输入标签。我使用Ardent我的验证,并在我的文章模型如下:使用Laravel验证多对多关系输入字段
class Article extends Ardent {
use PresentableTrait;
protected $presenter = 'presenters\ArticlePresenter';
protected $fillable = ['category_id', 'title', 'description', 'content', 'published'];
public static $rules = array(
'title' => 'required',
'description' => 'required',
'content' => 'required|min:250',
'category_id' => 'exists:categories,id',
'tags' => 'required'
);
public function tags()
{
return $this->belongsToMany('Tag', 'article_tag', 'article_id', 'tag_id');
}
}
我的表单输入:
<div class="form-group @if ($errors->has('tags')) has-error @endif">
{{ Form::label('tags', 'Tags') }}
@if(!isset($article))
{{ Form::text('tags', null, array('class' => 'form-control')) }}
@else
{{ Form::text('tags', $article->present()->implodeTags, array('class' => 'form-control')) }}
@endif
@if ($errors->has('tags')) <p class="help-block">{{ $errors->first('tags') }}</p> @endif
</div>
但即使我在标签字段中输入一些验证失败,这是为什么那?
第一部分(将'标签'添加到可填充数组中)是否足够?我试图让这个在我的情况下工作,我无法。 – 2014-10-23 07:28:32
这取决于,在我的情况下,我实际上并没有'article'表中的'tags'字段。我只需要将它传递给Ardent。后面的代码清除'tags'变量,因此它不会将它写入表中。 – 2014-10-23 08:30:19
你误会了,但没关系:)我在主表中没有外部字段,但是我使用的是laravel管理员,它在保存之前取消了任何“外部”数据字段,从而阻止了它的工作。 – 2014-10-23 17:21:16