Jump to content

Marko_Simke

─îlanovi
  • Posts

    1
  • Joined

  • Last visited

Previous Fields

  • Ime i prezime
    Marko Simke

Marko_Simke's Achievements

Newbie

Newbie (1/14)

  • First Post
  • Conversation Starter
  • Week One Done
  • One Month Later
  • One Year In

Recent Badges

0

Reputation

  1. Pozdrav iz Beca imam jedno malo kompleksnije pitanje (barem za mene :) ) , ako neko uopste shvati sta zelim. Uglavnom u pitanju je formular, programiran u php oop. Programiran tako da sam kreirao klase sa svim validacijama polja i posle polja formulara dodajem samo jednom funckijom u neogranicenom broju, to je ideja :) e sad je pitanje kod dokumenta checkbox.php gdje zelim konstruktor pozvati iz glavne klasse (formfield.class.php) i produziti ga u novu klasu (Najbolje je ako imate vremena da bacite pogled na kod jasnije ce vam biti) ali ne funkcionise, da li je neko imao iskustva sa pozivanjem konstruktora u pod konstruktoru sa promenama elemanata konstruktora u podkonstruktoru? kako to odraditi najbolje?neki tip? hvala index.php: <?php require_once 'inc/init.inc.php'; require_once 'inc/FormField.class.php'; require_once 'inc/Textarea.class.php'; require_once 'inc/Checkbox.class.php'; ?> <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Form Renderer</title> <link rel="stylesheet" href="css/pure.min.css"> <link rel="stylesheet" href="css/layout.css"> </head> <body> <div class="wrapper"> <?php $htmlAttributes = [ 'class' => 'field-class blabla', 'title' => 'Username' ]; $field1 = new FormField('user', 'textarea', 'User', '', 'userId', 'Fehler!', $htmlAttributes); $field2 = new FormField('password', 'password', 'Passwort'); $field3 = new Textarea('nachricht', 'text', 'Nachricht'); $field4 = new Checkbox('newsletter', 'Newsletter abonnieren'); echo $field1->render(); echo '<br>'; echo $field2->render(); echo '<br>'; echo $field3->render(); echo '<br>'; echo $field4->render(); ?> </div> </body> </html> formfield.php <?php class FormField public $value; public $errorMsg; protected $type; protected $name; protected $label; protected $id; protected $htmlAttributes; public function __construct( $name, $type, $label = '', $value = '', $id = '', $errorMsg = '', $htmlAttributes = [] ) { if ($name !== '' && $name !== null) { $this->name = $name; } else { echo 'FormField Error: name not set'; exit(); } if ($type !== '' && $type !== null) { $this->type = $type; } else { echo 'FormField Error: type not set'; exit(); } if( $label === '' ) { $this->label = $name; } else { $this->label = $label; } $this->value = $value; $this->id = $id; $this->errorMsg = $errorMsg; if ( !is_array($htmlAttributes) ) { $this->htmlAttributes = []; } else { $this->htmlAttributes = $htmlAttributes; } } public function render() { return $this->renderLabel() . $this->renderField() . $this->renderError(); } public function renderLabel() { $output = '<label for="' . $this->id . '">'; $output .= $this->label . '</label>'; return $output; } public function renderField() { $output = '<input type="' . $this->type; $output .= '" name="' . $this->name . '"'; $output .= ' value="' . $this->value . '"'; $output .= $this->renderHtmlAttributes(); $output .= '>'; return $output; } public function renderError() { if ( $this->errorMsg === '' ) { return ''; } $output = '<span class="formfield-error">' . $this->errorMsg . '</span>'; return $output; } protected function renderHtmlAttributes() { if ( count($this->htmlAttributes) === 0 ) { return ''; } $attrs = ''; foreach( $this->htmlAttributes as $key => $value ) { $attrs .= ' ' . $key . '="' . $value . '"'; } return $attrs; } } checkbox.classs.php <?php class Checkbox extends FormField { public function __construct( $name, $value, $label = '', $id = '', $errorMsg = '', $htmlAttributes = [] ) { $type = 'checkbox'; FormField::__construct( $name, $type, $label, $value, $id, $errorMsg, $htmlAttributes ); } public function render() { return $this->renderField() . $this->renderLabel() . $this->renderError(); } }
×
×
  • Create New...