2013-07-15 79 views
2

我必須修改一個Zend表單,其中一些字段可以包含小數。目前您只能使用一個點輸入您的小數點:34.75)同時使用逗號和點分隔符

我希望用戶能夠使用逗號或點來寫小數點。這些字段可以包含數字,如34.7534,75(在這種情況下,兩者都具有相同的值)。我不想修改服務器上的任何配置,所以我需要在代碼中執行此操作。

現在某些領域的價值在其他領域的功能計算;所以當你輸入一個逗號時,會擾亂計算。這是在JavaScript中完成的,我需要修復這些計算 - 但現在,當我檢索表單時,我想在php代碼中解決這個問題。

我試圖在Zend網站上找到一個解決方案,但是我沒有發現任何我已經閱讀過的更多示例。如您在代碼中看到的那樣,我需要將一個過濾器或驗證程序添加到zend_form_element_text。我不能使用str_replace,因爲元素是zend_form_element_text

I have found this other question for reference.

這是我的最終代碼:

$tabBilanFrais = array('txtFraisSecretariat' => array('nom' => 'Frais secrétariat', 'disabled' => true, "class"=>"calcul"), 
          'txtFraisRegion' => array('nom' => 'Frais région', 'disabled' => false), 
          'txtFraisSalle' => array('nom' => 'Salle', 'disabled' => false, "class"=>"calcul"), 
          'txtFraisPause' => array('nom' => 'Pauses', 'disabled' => false, "class"=>"calcul"), 
          'txtDivers' => array('nom' => 'Divers', 'disabled' => false, "class"=>"calcul"), 
          'txtTotalRegion' => array('nom' => 'Total région', 'disabled' => true, "class"=>"total"), 
          'txtIndemnisationAdherent' => array('nom' => 'Comm. ADH', 'disabled' => true, "class"=>"calcul"), 
          'txtIndemnisationPAP' => array('nom' => 'Comm. PAP', 'disabled' => true, "class"=>"calcul"), 
          'txtIndemnisationForNext' => array('nom' => 'Comm. ForNext', 'disabled' => true, "class"=>"calcul"), 
          'txtIndemnisationPROStages' => array('nom' => 'Comm. PROStages', 'disabled' => true, "class"=>"calcul"), 
          'txtRecettes' => array('nom' => 'Recettes', 'disabled' => true, "class"=>"totalMontant"), 
          'txtDepenses' => array('nom' => 'Dépenses', 'disabled' => true, "class"=>"totalMontant"), 
          'txtRecettesH' => array('nom' => 'Recettes', 'disabled' => false, "class"=>"hiddenTxt"), 
          'txtDepensesH' => array('nom' => 'Dépenses', 'disabled' => false, "class"=>"hiddenTxt") 
        ); 


$tabFormulaire = array() ; 

foreach($tabBilanFrais as $id => $tabElement) 
{ 
    if($tabElement['nom'] == 'Frais region') 
     $element = new Zend_Form_Element_Hidden($id, array("label" => $tabElement['nom'], "required" => false, 'decorators' => array("ViewHelper", "Errors", "Label"))) ; 
    else{ 
     $element = new Zend_Form_Element_Text($id, array("label" => $tabElement['nom'], "required" => false, 'decorators' => array("ViewHelper", "Errors", "Label"))) ; 
     //$element->addFilter('pregReplace', array('match' => '/,/', 'replace' => '.')); 
     $element->addFilter('LocalizedToNormalized'); 
     $element->addValidator('float', true, array('locale' => 'fr_FR')); 
     if(isset($tabElement['class']) && $tabElement['class']){ 
      $element->setAttrib('class', $tabElement['class']); 
     } 
    } 

    if($tabElement['disabled']) 
     $element->setAttrib('disabled', 'disabled'); 



    $tabFormulaire[] = $element ; 
} 

pregReplace不工作。驗證者是(comma becomes a .)。我收到一條關於該號碼不是浮動的錯誤消息。

回答

2

你可以隨時編寫你自己的驗證器。在浮動的情況下,我遇到像你一樣的問題:

class Yourlib_Validate_Float extends Zend_Validate_Abstract 
{ 
    const INVALID = 'floatInvalid'; 
    const NOT_FLOAT = 'notFloat'; 

    /** 
    * @var array 
    */ 
    protected $_messageTemplates = array(
     self::INVALID => "Invalid type given. String, integer or float expected", 
     self::NOT_FLOAT => "'%value%' does not appear to be a float", 
    ); 

    public function isValid($value) 
    { 
     $this->_setValue($value); 

     $value = str_replace(',', '.', $value); 

     if (!is_string($value) && !is_int($value) && !is_numeric($value)) { 
      $this->_error(self::INVALID); 
      return false; 
     } 

     if (is_numeric($value)) { 
      return true; 
     } 

     $this->_error(self::NOT_FLOAT); 
     return false; 
    } 
} 

,並添加驗證:

$element->addValidator(new Yourlib_Validate_Float()); 

請重新命名Yourlib到任何適合你。你需要像這樣的application.ini註冊您的「命名空間」:

autoloadernamespaces.Yourlib = "Yourlib_" 

嚴格地說,這驗證是numeric驗證。它通過與is_numeric的支票接受所有數值,如整數和浮點數。隨意修改。

+0

謝謝你的回答。不過,我應該更清楚一點。這些字段可能會檢索字符串(我不知道這些),這就是爲什麼它不被認爲是我認爲的浮點數。我不知道我應該用什麼驗證器來解決這個問題。 – user2583853

+0

@ user2583853是的,表單值總是字符串,但默認的浮動驗證器'Zend_Validate_Float'不能處理'.'和','。它只能根據區域設置處理其中之一。看一看源'的Zend /驗證/ Float.php',你會看到我的驗證差別不大.. – bitWorking

+0

哦好吧,我給它一個嘗試當我回去工作,我會讓你知道。謝謝。如果它有效,你會得到你的+1;) – user2583853

0

好吧,這裏是代碼修改的部分:

foreach($tabBilanFrais as $id => $tabElement) 
     { 
      if($tabElement['nom'] == 'Frais region') 
       $element = new Zend_Form_Element_Hidden($id, array("label" => $tabElement['nom'], "required" => false, 'decorators' => array("ViewHelper", "Errors", "Label"))) ; 
      else{ 
       $element = new Zend_Form_Element_Text($id, array("label" => $tabElement['nom'], "required" => false, 'decorators' => array("ViewHelper", "Errors", "Label"))) ; 
       $element->addFilter('pregReplace', array('match' => '/,/', 'replace' => '.')); 
       $element->addFilter('LocalizedToNormalized'); 
       $element->addValidator(new Anper_Validate_Float(), true, array('locale' => 'fr_FR')); 
       if(isset($tabElement['class']) && $tabElement['class']){ 
        $element->setAttrib('class', $tabElement['class']); 
       } 
      } 

      if($tabElement['disabled']) 
       $element->setAttrib('disabled', 'disabled'); 



      $tabFormulaire[] = $element ; 
     } 

但現在我需要在$元素中的字段的值已逗號取而代之的是一個點的元素被添加到$ tabFormulaire前。目前使用逗號的數字會被截斷(124,5變爲124),當我驗證窗體並顯示更新的值時。 pregreplace似乎不工作。

編輯:看來我不需要pregReplace。我在自定義驗證器的isValid函數中使用了兩個echo:一個在str_replace之前,另一個在之後。當我在其中一個字段中寫入一個帶有逗號的值時,兩個回顯都會顯示帶有一個點的數字。我認爲這是過濾器LocalizedToNormalized的結果。 我不明白的是,爲什麼一旦價值被保存,並顯示那些逗號被截斷,儘管我剛剛做出的結論。

EDIT2:如果我寫例如124 8,並使用pregReplace做這樣沒有空白,8個沒有保持在保存;儘管pregReplace工作(嘗試與我以前的回聲)。

+0

沒關係,在js中找到解決方法。 – user2583853

0

雖然@ bitWorking的答案是100%確定,從語義學的角度來看,這是更好地使用過濾器(因爲它是不是驗證更多的濾波)

無論是NumberFormat filter或寫你自己的。