depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Creditcard.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
46class Creditcard extends Fieldset
47{
51 protected $labelNumber;
52
56 protected $labelCheck;
57
62
66 protected $labelOwner;
67
71 protected $cardtypes;
81 protected function setDefaults(): void
82 {
83 parent::setDefaults();
84
85 $this->defaults['required'] = false;
86 $this->defaults['labelNumber'] = "Creditcard Number";
87 $this->defaults['labelCheck'] = "CVV/CVC";
88 $this->defaults['labelExpirationDate'] = "Expiration Date MM/YY";
89 $this->defaults['labelOwner'] = "Card Owner";
90 $this->defaults['cardtypes'] = [
91 "visa",
92 "americanexpress",
93 "mastercard",
94 ];
95 }
96
104 public function addChildElements(): void
105 {
106 parent::addChildElements();
107
108 $cardnames = [
109 'visa' => "Visa",
110 'americanexpress' => "American Express",
111 'mastercard' => "MasterCard",
112 ];
113 $options = [];
114 foreach ($this->cardtypes as $card) {
115 if (isset($cardnames[$card])) {
116 $options[$card] = $cardnames[$card];
117 }
118 }
119
120 $this->addSingle($this->name . "_card_type", [
121 'label' => "",
122 'list' => $options,
123 'skin' => 'select',
124 ]);
125 $this->addText($this->name . "_card_number", [
126 'label' => $this->labelNumber,
127 'required' => $this->required,
128 'validator' => "/^(?:\d[ -]*?){13,16}$/",
129 ]);
130 $this->addText($this->name . "_card_numbercheck", [
131 'label' => $this->labelCheck,
132 'required' => $this->required,
133 'validator' => "/^\d{3,4}$/",
134 ]);
135 $this->addText($this->name . "_card_expirydate", [
136 'label' => $this->labelExpirationDate,
137 'required' => $this->required,
138 'validator' => "/^\d{2}\/\d{2}$/",
139 ]);
140 $this->addText($this->name . "_card_owner", [
141 'label' => $this->labelOwner,
142 'required' => $this->required,
143 ]);
144 }
145
154 public function validate(): bool
155 {
156 return parent::validate();
157 }
158}
159
160/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Default creditcard fieldset.
addChildElements()
adds creditcard-inputs to fieldset
validate()
Validate the creditcard data.
$cardtypes
list of cardtypes to choose from
$labelNumber
label for the creditcard-number field
$labelOwner
label for the creditcard-owner field
setDefaults()
collects initial values across subclasses.
$labelCheck
label for the creditcard-check field
$labelExpirationDate
label for the creditcard-expirydate field
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition Fieldset.php:52