1

我的文章和标签表之间存在多对多关系,并且希望要求用户在创建/编辑文章表单中输入标签。我正在使用Ardent进行验证,并且在我的文章模型中有以下内容:

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>

但是,即使我在标签字段中输入了一些内容,验证也会失败,这是为什么呢?

4

2 回答 2

0

现在可以在最新版本的 Administrator 中验证关系数据。

于 2014-11-17T02:32:30.713 回答
0

我找到了原因;该tags变量没有被传递给Ardent

为了解决这个问题,我添加tagsfillable变量:

protected $fillable = ['category_id', 'title', 'description', 'content', 'published', 'tags'];

此外,我添加了以下代码,在Ardent 自述文件中进行了说明:

public $autoPurgeRedundantAttributes = true;

function __construct($attributes = array()) {
    parent::__construct($attributes);

    $this->purgeFilters[] = function($key) {
        $purge = array('tags');
        return ! in_array($key, $purge);
    };
}
于 2014-09-17T11:17:00.080 回答