Skip to content

Instantly share code, notes, and snippets.

@brendonexus
Last active November 30, 2016 09:59
Show Gist options
  • Save brendonexus/17f508e0b8258bfabc6dc4cbe5eba831 to your computer and use it in GitHub Desktop.
Save brendonexus/17f508e0b8258bfabc6dc4cbe5eba831 to your computer and use it in GitHub Desktop.
Boostrap 4 Form Validation
<?php
$field_name = 'field';
$field_label = 'Field';
?>
//INPUT FIELD
<div class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>">
<label for="<?php echo $field_name; ?>"
class="col-sm-0 control-label"><?php echo $field_label; ?></label>
<div class="col-sm-0">
<input name="<?php echo $field_name; ?>" type="text" class="form-control"
id="<?php echo $field_name; ?>"
value="" placeholder="">
<?php echo $_ERROR[$field_name] ? '<span class="help-block">' . $_ERROR[$field_name] . '</span>' : ''; ?>
</div>
</div>
//TEXTAREA FIELD
<div class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>">
<label for="<?php echo $field_name; ?>"><?php echo $field_label; ?></label>
<div class="col-sm-0">
<textarea name="<?php echo $field_name; ?>" class="form-control" id="<?php echo $field_name; ?>" cols="100" rows="3"></textarea>
<?php echo $_ERROR[$field_name] ? '<span class="help-block">' . $_ERROR[$field_name] . '</span>' : ''; ?>
</div>
</div>
//SELECT FIELD
<div
class="form-group <?php echo $_ERROR[$field_name] ? ' has-danger' : ''; ?>">
<label class="control-label"
for="method"><?php echo $field_label; ?></label>
<select class="form-control" name="<?php echo $field_name; ?>" id="<?php echo $field_name; ?>">
<option value="stripe">Credit / Debit Card</option>
</select>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_ERROR = array();
if (strlen($_POST['field']) == 0){
$_ERROR['field'] = 'Message';
}
if (!count($_ERROR)) {
$field = $_POST['field'];
} else {
$_SESSION['message']['error'] = 'Please correct the highlighted fields and submit again';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment