Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. if(isset($_POST['submit'])) {
  2. $author = $_POST['author'];
  3. $article_body = $_POST['article_body'];
  4. $article_title = $_POST['article_title'];
  5. $publication_date = $_POST['publication_date'];
  6. ...
  7. $category = $_POST['category'];
  8.  
  9. #validation code
  10. ...
  11. }
  12.  
  13. foreach($_POST as $key => $value) {
  14. ${"$key"} = $value;
  15. }
  16.  
  17. //now I access all of that variables
  18. echo $author; //will work
  19. echo $publication_date; //will also work
  20.  
  21. $Form = new Form;
  22. $Form->get_all_posted_values()->unset_field(['submit']);
  23.  
  24. foreach(array_keys($Form->fields) as $key) {
  25. ${"$key"} = $Form->fields["$key"];
  26. }
  27.  
  28. $author->required()->type("name")->range(8, 31)->label("Author")->error_message("The author name cannot have numbers or special characters.");
  29.  
  30. $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");
  31.  
  32. $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");
  33.  
  34. $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");
  35.  
  36. $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");
  37.  
  38. Class Form {
  39. #code
  40. ...
  41.  
  42. public function create_vars_for_fields() {
  43. foreach(array_keys($Form->fields) as $key) {
  44. ${"$key"} = $Form->fields["$key"];
  45. }
  46. return $this;
  47. }
  48. }
  49.  
  50. require_once 'Form.php';
  51.  
  52. $Form = new Form;
  53. $Form->get_all_posted_values()->create_vars_for_fields();
  54.  
  55. //here I should be able to access my variables, which are now objects of the class Fields
  56. echo $author->value; //should echo the name of the author, which is equal to $_POST['author']
  57. echo $title->value; //should echo the article title, which is equal to $_POST['title']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement