Jump to content

mjjdesignworks

New Members
  • Posts

    4
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

mjjdesignworks's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. As Jacques1 points out there is no security and right now this isn't an issue, these are my first steps into learning oop and it is just a work in progress, criticism's are welcome and noted, the objective from my point of view was to get it working and to see how it flows, it will be tidied up (maybe even rewritten) and improved with xss and csrf protection and proper form validation.
  2. ironically enough i have just done exactly that around an hour ago, just a few small changes were required: this at the top of the Formbuilder class: class FormBuilder { public $form; Still in Formbuilder (renderForm method) changed this: $html .= '</form>'; $form = sprintf($html); to this: $html .= '</form>'; $this->form = sprintf($html) and then carried the new $form variable over to the Registration class (buildForm method) and changed this $regform->renderForm(); } public function registerAction() { $newform = $this->buildForm(); View::renderTwigTemplate('Frontend/Tests/tests.html.twig',[ 'newform' => $newform ]); }; to this $regform->renderForm(); $newform = $regform->form; return $newform; } public function registerAction() { $newform = Registration::buildForm(); View::renderTwigTemplate('Shared/Userforms/register.html.twig',[ 'newform' => $newform ]); } } Only issue i had was with Twig auto-escaping the ouput and the form rendering as on page html which meant having to declare autoescaping to "false" in the Twig template file.
  3. The only problem I have found with Twig is its reliance at times on Symfony components, Twig is a standalone bundle but it's still a layer of Symfony framework, once you dive deeper into it beyond rendering simple stuff it can be a lot to get ones head around, particularly where forms are concerned, so then we get to the stage of the Documentation on Twigs website which tbh is ok for front end devs but needs more clarity for backend devs I feel. All that said it's good stuff though and blazing fast rendering and you can mix Twig syntax and plain old HTML in the templates and it's no issue.
  4. Firstly the code: Formbuilder.php class FormBuilder { protected $input; private $method; private $action; public function __construct($method, $action = NULL) { $this->method = $method; $this->action = $action; } public function newInput($type, $name, $value, $placeholder = NULL, $class = NULL, $id = NULL) { $this->input[] = array ( 'type' => $type, 'name' => $name, 'value' => $value, 'placeholder' => $placeholder, 'class' => $class, 'id' => $id ); } public function newLabel($name, $title = NULL) { $this->input[] = array ( 'type' => 'label', 'name' => $name, 'title' => $title ); } public function newTextarea($name, $value = NULL, $placeholder = NULL) { $this->input[] = array ( 'type' => 'textarea', 'name' => $name, 'value' => $value, 'placeholder' => $placeholder ); } public function renderForm() { $html = '<form action="' . $this->action . '" method="' . $this->method . '">'; for ($i = 0; $i < count($this->input); $i++) { switch($this->input[$i]['type']) { case "label": $html .= '<div><p><label for="' . $this->input[$i]['name'] . '">' . $this->input[$i]['title'] . '</label><br />'; break; case "text": case "password": case "hidden": case "submit": case "email": $html .= '<input type="' . $this->input[$i]['type'] . '" '; $html .= ' name="' . $this->input[$i]['name'] . '" '; $html .= ' value="' . $this->input[$i]['value'] . '" '; $html .= ' placeholder="' . $this->input[$i]['placeholder'] . '" '; $html .= ' class="' . $this->input[$i]['class'] . '" '; $html .= ' id="' . $this->input[$i]['id'] . '"></p></div> '; break; case "textarea": $html .= '<textarea name="' . $this->input[$i]['name'] . ' value="' . $this->input[$i]['value'] . '" placeholder="' . $this->input[$i]['placeholder'] . '"></textarea>'; break; } } $html .= '</form>'; $form = sprintf($html); // print $form; Registration.php class Registration extends \Core\CoreController { public function buildForm() { $regform = new FormBuilder('post'); $regform->newLabel('username', 'Enter a Username:'); $regform->newInput('text','username','','Enter Username', 'form-control', 'username'); $regform->newLabel('password', 'Enter password:'); $regform->newInput('password', 'password', '', 'Enter Password', 'form-control', 'password'); $regform->newLabel('password', 'Re-enter password:'); $regform->newInput('password', 'password_chk', '', 'Password Again', 'form-control', 'password_chk'); $regform->newLabel('useremail', 'Enter email address:'); $regform->newInput('email','useremail','','Email Address', 'form-control', 'useremail'); $regform->newInput('submit','submit','Register'); $regform->renderForm(); } public function registerAction() { $newform = $this->buildForm(); View::renderTwigTemplate('Frontend/Tests/tests.html.twig',[ 'newform' => $newform ]); } } } } Twig Template % extends "base.html.twig" %} {% block body %} {{ newform }} {% endblock % The problem: As you can see in Formbuilder.php i assign a variable ($form) to a sprintf function, i have to use sprintf because if i use print it overrides the twig template and just prints the form even without being called by Twig at least with sprintf i can assign it to a variable. What i am trying to do for hours now is get my $form variable inside the buildForm method in Registration.php where i can then assign it to a Twig variable and render it where i want it inside the Twig template, as i said using print just does what its supposed to do, print the form above my call to the extends "Base.html.twig" which is obviously not what i want. I could possibly get away without having to assign a variable to sprintf function as this line of code ($regform->renderForm) in Registration.php buildForm method is what renders the print so im guessing it also holds the sprintf, i just need to find a way to assign it to the $newform variable for Twig. Thanks
  5. Often looked in these forums via Google when i have needed some advice, finally decided to give it a shot and sign up properly to add to what i have already gained from you guys..
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.