2015-10-06 48 views
0

我想驗證包含下拉菜單和文本輸入字段的表單。Symfony2僅在包含某些內容時驗證文本輸入

用戶可以從下拉菜單中選擇一個項目。如果他想創建一個新項目,他可以使用下拉菜單旁邊的文本輸入字段。

這是我上傳類型:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->setAction('upload') 
     ->setMethod('POST') 

     // project name dropdown menu 
     ->add('projectname', 'choice' , array(
      'label' => 'upload_project_label', 
      'choices' => $this->projects, 
      'attr' => array(
       'class' => 'form-control some', 
       'required' => 'true' 
      ) 
     )) 

     // newprojectname text input 
     ->add('newprojectname', 'text', array(
       'label' => false, 
       'attr' => array(
        'class' => 'form-control', 
        'required' => false, 
        'placeholder' => 'upload_newprojectname_placeholder' 
       ) 
      ) 
     ) 
... 

這是我上傳的實體的一個片段:

/** 
* @ORM\Column(type="text") 
* 
* @var string $projectname 
* @Assert\NotBlank() 
*/ 
protected $projectname; 

/** 
* @ORM\Column(type="text") 
* 
* @var string $newprojectname 
* @Assert\Length(
*  min = 3, 
*  max = 7, 
*  minMessage = "min message", 
*  maxMessage = "max message" 
*) 
*/ 
protected $newprojectname; 

我的問題是有沒有檢查該字段newproject查詢的可能性被設置(即一個字符串被輸入)?如果是這樣,讓Assert註釋完成它的工作。

+0

我不明白...你要檢查該字段不爲空,然後呢? – pabgaran

+1

您可以創建['Callback'](http://symfony.com/doc/current/reference/constraints/Callback.html)斷言。 – Fracsi

+0

@pabgaran - 我想檢查是否在該字段中輸入了內容,如果是這樣,請使用Assert註釋檢查輸入的值。這足夠清楚了嗎? – aszel

回答

1

這可以通過多種方式完成,所有這些都可能會滿足您的要求。

  1. 使用custom callback - 這是最快捷,最直接的
  2. 使用expression validator - 很多人有嵌入PHP中的元語言是完全合法的問題,但是這又是一個快速上下的方式做事
  3. 使用group sequences,特別是group sequence provider功能

的你選擇哪一個是你的,但回調是一個快速簡便的起點,你可以,如果你的VA基礎上封閉限制變得更加複雜。

+0

感謝您的回答。我通過使用@Frasci提到的自定義回調來找到解決方案。將在下面發佈代碼。 – aszel

0

這是建議的解決方案作爲自定義回調驗證的代碼塊。

我不得不添加其他功能我上傳的實體,看起來像這樣:

use Symfony\Component\Validator\Context\ExecutionContextInterface; 
/** 
* Function holds custom validation for fields in import/upload form 
* @Assert\Callback 
* @param ExecutionContextInterface $context 
*/ 
public function validate(ExecutionContextInterface $context) 
{ 
    // check new project name 
    $newProjectName = $this->getNewprojectname(); 
    if(!empty($newProjectName)) { 

     // letters only 
     $pattern = '/[a-zA-Z]/'; 
     if($this->isPatternWrong($newProjectName, $pattern)) { 
      $context 
       ->buildViolation('Please use letters only.') 
       ->atPath('newprojectname') 
       ->addViolation(); 
     } 

     // text max. 7 digits 
     $maxlength = 7; 
     if($this->isStringTooLong($newProjectName, $maxlength)) { 
      $context 
       ->buildViolation('Max. length 7 digits.') 
       ->atPath('newprojectname') 
       ->addViolation(); 
     } 
    } 
} 

private function isPatternWrong($string, $pattern) 
{ 
    $result = preg_match($pattern, $string); 
    if($result === 0) { 
     return true; 
    } 
    return false; 
} 

private function isStringTooLong($string, $length) 
{ 
    if(strlen($string) > $length) { 
     return true; 
    } 
    return false; 
}