1: <?php
2:
3: /**
4: * Pry Framework
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file LICENSE.txt.
10: *
11: */
12:
13: namespace Pry\Form\Element;
14:
15: use Pry\Form\Input;
16: use Pry\Form\Error;
17:
18: /**
19: * Input de type Text
20: * @category Pry
21: * @package Form
22: * @subpackage Form_Element
23: * @version 1.0.6
24: * @author Olivier ROGER <oroger.fr>
25: */
26: class Text extends Input
27: {
28:
29: /**
30: * Taille minimal
31: *
32: * @var int
33: * @access protected
34: */
35: protected $minLength;
36:
37: /**
38: * Constructeur
39: *
40: * @param string $nom
41: * @param Form_Form $form
42: * @access public
43: */
44: public function __construct($nom, $form)
45: {
46: parent::__construct($nom, $form);
47: if (!isset($this->attrs['type']))
48: $this->setAttributes('type', 'text');
49: $this->minLength = 0;
50: }
51:
52: /**
53: * Défini une taille minimal pour le contenu
54: *
55: * @param int $taille
56: * @access public
57: * @return Form_Element_Text
58: */
59: public function minLength($taille)
60: {
61: if (ctype_digit((string) $taille) && $taille >= 0)
62: $this->minLength = $taille;
63: return $this;
64: }
65:
66: /**
67: * Vérifie que la valeur est valide
68: *
69: * @param string $value
70: * @return boolean
71: */
72: public function isValid($value)
73: {
74: if (parent::isValid($value))
75: {
76: if (!$this->required && empty($value))
77: return true;
78: if (!$this->minLength == 0)
79: $carac = $this->minLength - 1;
80: else
81: return true;
82:
83: if (isset($value[$carac]))
84: return true;
85: else
86: $this->errorMsg = Error::TOOSHORT . $this->minLength;
87: }
88: return false;
89: }
90:
91: /**
92: * Ecris l'élément avec toutes ses options
93: *
94: * @param array $array
95: * @return string
96: */
97: public function __toString()
98: {
99:
100: $css = $this->cssClass();
101: $label = '';
102: if (!empty($this->label))
103: {
104: $label = "\t" . '<label for="' . $this->attrs['id'] . '" class="' . $this->cssLabel . '">' . $this->label . '</label>' . "\n";
105: if ($this->labelNewLine)
106: $label.="\t" . '<br />' . "\n";
107: }
108: $attributs = $this->attrsToString();
109: //Posted value ou value par défaut
110: $value = $this->form->getPostedvalue($this->attrs['name']);
111: if (empty($value))
112: $value = $this->value;
113:
114: $field = "\t" . '<input ' . $css . ' value="' . htmlspecialchars($value) . '" ' . $attributs . ' />' . "\n";
115:
116: $error = '';
117: if (!is_null($this->errorMsg))
118: {
119: $error = '<span class="' . $this->errorClass . '">' . $this->errorMsg . '</span><br />';
120: }
121: if (!empty($this->info))
122: $field.="\t" . '<img src="' . $this->imgInfo . '" id="' . $this->attrs['name'] . '_tooltip" class="form_tooltip" title="' . $this->info . '" alt="" style="cursor:help;" />';
123: if ($this->fieldNewLine)
124: $field.="\t" . '<br />' . "\n";
125: return $label . $field . $error;
126: }
127:
128: }
129:
130: ?>