Skip to content

Form Validation

RedDragonWebDesign edited this page Nov 3, 2020 · 3 revisions

Form validation is done using the custom written Form class.

The form class both builds the HTML, and does form validation.

Example

https://github.com/RedDragonWebDesign/BlueThrust5/blob/62e111534d35fc6aeb3151fc00e3b5a605888968/src/forum/search.php#L145

$formComponents = array(
	"searchbykeyword" => array(
		"options" => array("section_title" => "Search by Keyword"),
		"sortorder" => $i++,
		"type" => "section",
		"validate" => array("search_checks")
	),
	"keyword" => array(
		"type" => "text",
		"attributes" => array("class" => "formInput textBox"),
		"display_name" => "Keyword",
		"sortorder" => $i++
	),
	"filterkeyword" => array(
		"type" => "select",
		"options" => array("Search Entire Posts", "Search Titles Only"),
		"display_name" => "Filter Keyword",
		"sortorder" => $i++,
		"attributes" => array("class" => "formInput textBox"),
		"validate" => array("RESTRICT_TO_OPTIONS")
	),
	"searchbyuser" => array(
		"options" => array("section_title" => "Search by Username"),
		"sortorder" => $i++,
		"type" => "section"
	),
	"searchuser" => array(
		"type" => "autocomplete",
		"attributes" => array("class" => "formInput textBox"),
		"display_name" => "Username",
		"sortorder" => $i++,
		"options" => array("real_id" => "searchUser", "fake_id" => "fakeSearchUser", "list" => $memberList)
	),
	"filterusername" => array(
		"type" => "select",
		"options" => array("Find Posts by User", "Find Topics Started by User"),
		"display_name" => "Filter Username",
		"sortorder" => $i++,
		"attributes" => array("class" => "formInput textBox"),
		"validate" => array("RESTRICT_TO_OPTIONS")
	),
	"searchoptions" => array(
		"options" => array("section_title" => "Search Options"),
		"sortorder" => $i++,
		"type" => "section"
	),
	"filtertopics" => array(
		"type" => "select",
		"options" => array("At Least", "At Most"),
		"display_name" => "Find Topics with",
		"sortorder" => $i++,
		"attributes" => array("class" => "formInput textBox"),
		"html" => "<div class='formInput main' style='padding-left: 10px'><input type='text' style='width: 15%' value='0' name='filtertopics_replies' class='textBox'> Replies</div>",
		"validate" => array("RESTRICT_TO_OPTIONS")
	),
	"filterposts" => array(
		"type" => "select",
		"options" => array("Any Date", "Your Last Login", "Yesterday", "A week ago", "2 weeks ago", "1 month ago", "3 months ago" , "6 months ago", "A year ago"),
		"display_name" => "Find Posts from",
		"sortorder" => $i++,
		"attributes" => array("class" => "formInput textBox"),
		"html" => "<div class='formInput main' style='padding-left: 10px'><select name='filterposts_newold' class='textBox'><option value='0'>and Newer</option><option value='1'>and Older</option></select></div>",
		"validate" => array("RESTRICT_TO_OPTIONS")
	),
	"sortresults" => array(
		"type" => "select",
		"options" => array("Last post date", "Topic Title", "Number of Replies", "Number of Views", "Topic Start Date", "Forum", "Username", "Member Rank"),
		"display_name" => "Sort Results by",
		"attributes" => array("class" => "formInput textBox"),
		"sortorder" => $i++,
		"html" => "<div class='formInput main' style='padding-left: 10px'><select name='sortresults_ascdesc' class='textBox'><option value='0'>in Descending Order</option><option value='1'>in Ascending Order</option></select></div>",
		"validate" => array("RESTRICT_TO_OPTIONS")
	),
	"filterboardsection" => array(
		"type" => "section",
		"options" => array("section_title" => "Filter Boards"),
		"sortorder" => $i++
	),
	"filterboards[]" => array(
		"type" => "select",
		"display_name" => "Select Boards",
		"attributes" => array("multiple" => "multiple", "class" => "formInput textBox", "size" => $filterBoardSize, "style" => "width: 40%"),
		"options" => $filterBoardOptions,
		"sortorder" => $i++,
		"validate" => array("check_filter_boards")
	),
	"include_subforums" => array(
		"type" => "checkbox",
		"value" => 1,
		"display_name" => "Include Sub-Forums",
		"attributes" => array("class" => "formInput", "checked" => "checked"),
		"sortorder" => $i++
		
	),
	"submit" => array(
		"type" => "submit",
		"attributes" => array("class" => "submitButton formSubmitButton"),
		"sortorder" => $i++,
		"value" => "Search"
	)
);


$setupFormArgs = array(
	"name" => "search_form",
	"components" => $formComponents,
	"description" => "Use the form below to search the forum.",
	"attributes" => array("method" => "post", "action" => $MAIN_ROOT."forum/search.php")

);

$formObj = new Form($setupFormArgs);

if($_POST['submit'] && $formObj->validate()) {
	$_SESSION['btLastSearch'] = time();
	
	define("SHOW_SEARCHRESULTS", true);
	require_once("search_results.php");
}
else {
	$formObj->show();
}

Component Types

https://github.com/RedDragonWebDesign/BlueThrust5/blob/62e111534d35fc6aeb3151fc00e3b5a605888968/src/classes/form.php#L146

  • autocomplete
  • textarea
  • richtextbox
  • codeeditor
  • datepicker
  • timepicker
  • select
  • checkbox
  • radio
  • file
  • section
  • beforeafter
  • custom
  • colorpick

Validation Flags

https://github.com/RedDragonWebDesign/BlueThrust5/blob/62e111534d35fc6aeb3151fc00e3b5a605888968/src/classes/form.php#L505

  • NOT_BLANK
  • NUMBER_ONLY
  • POSITIVE_NUMBER
  • RESTRICT_TO_OPTIONS
  • IS_SELECTABLE
  • IS_NOT_SELECTABLE
  • CHECK_LENGTH
  • EQUALS_VALUE
  • NOT_EQUALS_VALUE
  • GREATER_THAN
  • LESS_THAN
  • VALIDATE_ORDER
Clone this wiki locally