Explorar o código

Added support for help text, min, max, number fields and max lengths.

Alan Lam %!s(int64=8) %!d(string=hai) anos
pai
achega
fed6165ade
Modificáronse 1 ficheiros con 67 adicións e 2 borrados
  1. 67 2
      engine/form.php

+ 67 - 2
engine/form.php

@@ -71,6 +71,10 @@ class Form {
 				self::renderTextField($options);
 				break;
 
+			case "number":
+				self::renderNumberField($options);
+				break;
+
 			case "textarea":
 				self::renderTextareaField($options);
 				break;
@@ -93,11 +97,47 @@ class Form {
 		$placeholder = "Enter " . strtolower($options->label);
 		$value = $options->value ?? http::value($options->name);
 		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
+		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		?>
 		<div class="row mb-1">
 			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
 			<div class="col-sm">
-				<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
+				<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
+			</div>
+		</div>
+		<?php
+	}
+
+	private static function renderNumberField($options) {
+		$id = str_replace("_", "-", $options->name);
+		$placeholder = "Enter " . strtolower($options->label);
+		$value = $options->value ?? http::value($options->name);
+		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
+		$min = ($options->min ?? false) ? "min=\"$options->min\"" : "";
+		$max = ($options->max ?? false) ? "max=\"$options->max\"" : "";
+		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
+
+		?>
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<div class="col-sm">
+				<input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
 			</div>
 		</div>
 		<?php
@@ -107,6 +147,7 @@ class Form {
 		$id = str_replace("_", "-", $options->name);
 		$selected_value = $options->value ?? http::value($options->name);
 		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
 
 		?>
 		<div class="row mb-1">
@@ -123,6 +164,13 @@ class Form {
 				}
 				?>
 				</select>
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
 			</div>
 		</div>
 		<?php
@@ -132,11 +180,19 @@ class Form {
 		$id = str_replace("_", "-", $options->name);
 		$placeholder = "Enter " . strtolower($options->label);
 		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
 		?>
 		<div class="row mb-1">
 			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
 			<div class="col-sm">
 				<input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" capture="camera" <?=$required?> />
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
 			</div>
 		</div>
 		<?php
@@ -147,11 +203,20 @@ class Form {
 		$placeholder = "Enter " . strtolower($options->label);
 		$value = $options->value ?? http::value($options->name);
 		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
+		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		?>
 		<div class="row mb-1">
 			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
 			<div class="col-sm">
-				<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?>><?=htmlspecialchars($value)?></textarea>
+				<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
 			</div>
 		</div>
 		<?php