Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(isset($_POST['submit'])) {
- $author = $_POST['author'];
- $article_body = $_POST['article_body'];
- $article_title = $_POST['article_title'];
- $publication_date = $_POST['publication_date'];
- ...
- $category = $_POST['category'];
- #validation code
- ...
- }
- foreach($_POST as $key => $value) {
- ${"$key"} = $value;
- }
- //now I access all of that variables
- echo $author; //will work
- echo $publication_date; //will also work
- $Form = new Form;
- $Form->get_all_posted_values()->unset_field(['submit']);
- foreach(array_keys($Form->fields) as $key) {
- ${"$key"} = $Form->fields["$key"];
- }
- $author->required()->type("name")->range(8, 31)->label("Author")->error_message("The author name cannot have numbers or special characters.");
- $title->required()->type("string")->range(8, 127)->label("Article title")->error_message("The title must be descriptive and should not be too short or too long");
- $category->required()->type("string")->range(3 ,31)->label("Article category")->error_message("Category must be descriptive and must have be at least 3 characters long");
- $body->required()->type("html")->range(600, 65535)->label("Article body")->error_message("The article should have at least 600 characters and at the most 65535 characters");
- $publication_date->required()->type("date")->min("06/01/2019")->max("01/01/2020")->label("Publication date")->error_message("Set a publication date for this year");
- Class Form {
- #code
- ...
- public function create_vars_for_fields() {
- foreach(array_keys($Form->fields) as $key) {
- ${"$key"} = $Form->fields["$key"];
- }
- return $this;
- }
- }
- require_once 'Form.php';
- $Form = new Form;
- $Form->get_all_posted_values()->create_vars_for_fields();
- //here I should be able to access my variables, which are now objects of the class Fields
- echo $author->value; //should echo the name of the author, which is equal to $_POST['author']
- echo $title->value; //should echo the article title, which is equal to $_POST['title']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement