Php 同时使用逗号和点分隔符
我必须修改Zend表单,其中一些字段可以包含小数。目前,您只能使用点输入小数:Php 同时使用逗号和点分隔符,php,zend-framework,Php,Zend Framework,我必须修改Zend表单,其中一些字段可以包含小数。目前,您只能使用点输入小数:34.75) 我希望用户能够用逗号或点来写小数。字段可以包含像34.75和34,75这样的数字(在本例中,两者的值相同)。我不想修改服务器上的任何配置,所以我需要在代码中这样做 现在一些字段的值是根据其他字段的函数计算的;因此,当你输入逗号时,它会把计算搞乱。这是在javascript中完成的,我需要修正这些计算——但现在,我想在检索表单时在php代码中修正这个问题 我试图在Zend网站上找到一个解决方案,但我没有找到
34.75)
我希望用户能够用逗号或点来写小数。字段可以包含像34.75
和34,75
这样的数字(在本例中,两者的值相同)。我不想修改服务器上的任何配置,所以我需要在代码中这样做
现在一些字段的值是根据其他字段的函数计算的;因此,当你输入逗号时,它会把计算搞乱。这是在javascript中完成的,我需要修正这些计算——但现在,我想在检索表单时在php代码中修正这个问题
我试图在Zend网站上找到一个解决方案,但我没有找到任何我已经在其他地方阅读过的例子。正如您在代码中看到的,我需要向zend\u form\u element\u text
添加一个过滤器或验证器。我不能使用str\u replace
,因为元素是zend\u form\u element\u text
下面是我的结果代码:
$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 ;
}
pregrade
不工作。验证器是(逗号变成a.)
。我收到一条关于数字不是浮点数的错误消息。您可以随时编写自己的验证程序。在浮动的情况下,我和你面临同样的问题:
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_"
严格地说,这个验证器是一个
数值的
验证器。它接受所有数值,如整数,并通过is\u numeric
检查浮动。请随意修改。好的,下面是代码的修改部分:
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 ;
}
但是现在我需要$element中字段的值在元素添加到$tabFormulaire之前将逗号替换为一个点。当前,当我验证表单并显示更新的值时,带有逗号的数字被截断(124,5变为124)。
这台机器似乎不起作用
编辑:看来我不需要更换。我在自定义验证器的isValid函数中使用了两个echo:一个在str_replace之前,一个在str_replace之后。当我在其中一个字段中写入一个带逗号的值时,两个回音都会显示带点的数字。我假设这是过滤器本地化非规范化的结果。
我不明白的是,为什么在保存和显示这些值后,尽管我刚刚做了一些发现,但那些带有逗号的值会被截断
Edit2:如果我写了1248,并且使用pregraced来执行没有空白的操作,那么8就不会保存;尽管pregraceworking(用我以前的echo尝试过)。尽管@bitWorking的答案100%正确,但从语义的角度来看,最好使用过滤器(因为它更多的是过滤而不是验证)
或者写你自己的 谢谢你的回答。不过我应该更清楚一点。字段可能检索字符串(我不太清楚),这就是为什么我认为它没有被识别为浮点。我不知道应该使用什么验证器来解决这个问题。@user2583853是的,表单值总是字符串,但是默认的浮点验证器
Zend\u Validate\u float
无法处理
和,
。根据区域设置,它只能处理其中一个。看看源代码Zend/Validate/Float.php,你会发现我的验证器并没有太大的不同……哦,好吧,等我回去工作的时候我会试试,我会让你知道的。谢谢如果有效,您将获得+1;)