1 <?php
2 /**
3 * FloatInput.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class describes input that must contain float values
13 *
14 * @package Form\Input
15 */
16 class FloatInput extends NumberInput{
17
18 /**
19 * The number of decimals to display
20 *
21 * @var integer
22 */
23 public $decimals = 2;
24
25 /**
26 * Constructor
27 *
28 * @param array $param The input parameters
29 */
30 public function __construct($param){
31 parent::__construct($param);
32 $this->pattern = '/^[0-9]+(.[0-9]{0, ' . $this->decimals .'})?/';
33 }
34
35
36 /**
37 * Display the input
38 *
39 * @return string The displayed HTML
40 */
41 public function display(){
42 $this->value = number_format(floatval($this->value), $this->decimals, ".", "");
43 return parent::display();
44 }
45
46
47 /**
48 * Check the format of the submitted value
49 *
50 * @param Form $form The form the input is associated with
51 *
52 * @return boolean true if the submitted value has the right syntax, else false
53 */
54 public function check(&$form = null){
55 if(parent::check($form)) {
56 if(!empty($this->value) && !is_numeric($this->value)) {
57 // The value is not numeric
58 $form->error($this->errorAt, Lang::get('form.number-format'));
59 return false;
60 }
61 return true;
62 }
63 else{
64 return false;
65 }
66 }
67
68 /**
69 * Get the value, formatted for the MySQL database
70 *
71 * @return float The formatted value
72 */
73 public function dbvalue(){
74 return (float)($this->value);
75 }
76 }
77