symfony2 PostTypeクラスで検証制約 "Field is required"を追加するにはどうすればよいですか?助言がありますか?私はSF2で新しく、以前の開発者が行ったことを編集しています。クエリを含むフィールドは空白であってはなりません
use Symfony\Component\Validator\Constraints\NotBlank;
class BlogPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$blogPostCategories = BlogPostCategoryQuery::create()
->filterByBlogPost($builder->getData())
->find();
$categoryIds = array();
foreach ($blogPostCategories as $blogPostCategory) {
$categoryIds[] = $blogPostCategory->getCategory()->getId();
}
$queryOptions = array(
'option_status' => Categorypeer::STATUS_ACTIVE,
'option_category_ids' => $categoryIds
);
$categories = CategoryQuery::create()
->filterActiveCategoriesByOptions($queryOptions)
->find();
$builder->add('category_ids', 'model', array(
'label' => 'Category',
'mapped' => false,
'class' => 'Deal\MainBundle\Model\Category',
'query' => CategoryQuery::create()
->filterActiveCategoriesByOptions()
->orderByName(),
'property' => 'name',
'empty_value' => 'Select categories',
'empty_data' => null,
'required' => true,
'multiple' => true,
'data' => $categories,
'constraints' => array(
new NotBlank(array(
'message' => 'Your message can not be blank! Ouch!'
)),
)
));
呼び出すときは、(それがあなたのBlogPostType
を使用してフォームタイプです)、親フォームから、このタイプのrequired => true
を設定する必要があり
私は自分のコードを更新し、それが – phpmeter
@jlacsonph仕事doesntのあなたは、私が上記の書いたものを実装する方法を投稿することができますか? – martin
こんにちは、私の更新されたコードをもう一度チェックしてください、ありがとうございます。 – phpmeter