Seite 1 von 1
wie in rex_form Pflichtfeld erstellen
Verfasst: 23. Jan 2010, 16:33
von timo.huber
Hallo zusammen
ich verwende die Klasse rex_form. Es wurde mit addTextField ein Textfeld angefügt, und mit setLabel beschriftet.
Nun möchte ich ein bestimmtes Textfeld zu einem Pflichtfeld machen UND validieren (email).
Ist das Feld nicht, oder nicht korrekt ausgefüllt, soll nicht in die DB geschrieben, sondern stattdessen eine rexWarning() ausgegeben werden.
Kann mir bitte jemand auf die Sprünge helfen?
MFG
Timo
Verfasst: 25. Jan 2010, 19:42
von timo.huber
kleiner *Schubs* nach oben.
Geht es wirklich nicht ein Pflichtfeld im Backend für ein Addon mir rex_form anzugeben?
Ich hoffe ja auf irgendeinen Geistesblitz von euch
MFG
Timo
Verfasst: 26. Jan 2010, 12:22
von timo.huber
Hallo,
ich führe hier einmal meinen Monolog fort
Also da wohl nichts drum herum führt, werde ich nun doch auf das Addon Framework zurückgreifen (nur als Info für diejenigen, die das selbe Problem haben)
MFG
Verfasst: 26. Jan 2010, 14:20
von darwin
Hi, in der RexForm (unterhalb des 1. textfeldes)
sind einige Typen beschrieben, auch ein Validate:
email -> prüft ob email, beispiel: validate|email|emaillabel|warning_message
bsp:
validate|email|email|Bitte überprüfen Sie die E-Mail-Adresse.
grz. Chris
Verfasst: 26. Jan 2010, 15:05
von timo.huber
Hallo Chris,
die Syntax sieht mir schwer nach doform aus, sicher dass das RexForm, bzw rex_form ist?
Wenn ja, hab ich das doch glatt übersehen.
Wie gesagt ich nutze nun das Addon Framework, das Problem ist damit quasi gelöst
mfg Timo
Verfasst: 26. Jan 2010, 15:22
von darwin
Rex_Form != xForm?
is da n Unterschied? Hab isch was verpasst ... sry
grz. Chris
Verfasst: 26. Jan 2010, 15:42
von timo.huber
Huhu,
ja, es gibt einen Unterschied. xform ist ja ein Formular Addon.
rex_form ist z.b. um für Addons im Backend ein Formular für die Konfiguration zur Verfügung zu stellen.
Zu finden unter
MFG
Verfasst: 19. Mär 2010, 16:04
von ra-V-en
Ich weiß jetzt nicht ob das Problem noch aktuell ist, aber eine Möglichkeit ist den EP zu nutzen, der in der Klasse existiert:
Code: Alles auswählen
functionsname($params){
//Anweisungen/Validierungen
//Im Erfolgsfall true ausgeben
//sonst false
return true
}
$form = new rex_form('tabelle', 'fieldset', 'where');
rex_register_extension('REX_FORM_'. strtoupper($form->getName()). '_SAVE', 'functionsname');
/*...weitere Formularanweisungen...*/
Ich habe damit eigene Validierungen hinbekommen, und es scheint ohne Problemen beim Anlegen und Speichern zu funktionieren.
MfG
Stefan
Verfasst: 20. Mär 2010, 11:46
von timo.huber
Servus,
ich habe in meinem Fall nun einfach die Klasse erweitert und die presave Methode angepasst.
Aber das mit dem EP geht natürlich auch =)
mfg
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 30. Dez 2010, 17:34
von darwin
Hi,
kann hier jemand noch ein Beispiel posten wie
es aussehen könnte... egal ob mit EP oder Class Erweiterung.
(steh da mal wieder aufm Schlauch)
Danke. grz. Chris
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 30. Dez 2010, 19:56
von jdlx
darwin hat geschrieben: wie es aussehen könnte... egal ob mit EP oder Class Erweiterung.
Hab nix konkretes anzubieten, aber evtl. wär auch ne JS Validierung ein Weg.
lg,
j.
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 30. Dez 2010, 22:47
von Xong
Formulare sollte man mit dem viel mächtigeren und flexibleren XForm erstellen. Da gibt es dann die validate-Klassen. Außerdem kann man sich bei XForm beliebige weitere Eingabe-, Validate und Action-Klassen schreiben. Im Prinzip stehen einem keine Wünsche offen.
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 2. Jan 2011, 14:34
von darwin
Hey XONG,
also auch fürs Backend die xForm? ... Ich nutze die rex_form für die Backend Eingaben und die rex_list
zur Ausgabe im Backend. Mit der xForm im backend? ist das genauso einfach?... hast du evtl mal ein
beispiel Addon parat das dies aufzeigt wie, was wo... (bin ned so der Classes held *leider*) ...
hilft sicherlich noch mehr hier im Forum
DANKE.
grz. Chris
PS: "Edit me" ... hab mich noch nicht wirklich damit angefreundet...
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 6. Jan 2011, 09:08
von Mediastuttgart
hallo chris,
um mit rex_form felder zu validieren, kann die klasse zum beispiel wie folgt erweitert werden
Code: Alles auswählen
<?php
/**
* custom_rex_form Class
*
* A Class that provides Validation Functionality.
*
* This source file is subject to the MIT license that
* is bundled with this source code in the file LICENSE.
*
* PHP Version 5.2.4
*
* @category rex_form
* @package redaxo
* @author michael alexander freund <freund@mediastuttgart.de>
* @copyright Copyright (c) 2011 MEDIASTUTTGART, digitalisierte Kommunikation.
* @license http://www.opensource.org/licenses/mit-license.php
*
*/
class custom_rex_form extends rex_form {
/**
* Elements to Validate
*
* @var array
*/
protected $validationElements;
/**
* Validation Error
*
* @var array
*/
protected $validationError;
/**
* validate
*
* Override Method
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
public function validate()
{
foreach ($this->validationElements as $label => $type)
{
/**
* Fieldvalue
*/
$fieldValue = $this->elementPostValue($this->getFieldsetName(), $label);
/**
* Fieldlabel
*/
$fieldLabel = $this->getElement($this->getFieldsetName(), $label);
/**
* Validate given Field
*/
$this->validationError = $this->_validate($type, $fieldValue, $fieldLabel);
/**
* Return failed Validation Error
*/
if (isset($this->validationError))
{
return $this->validationError;
}
}
return parent::validate();
}
/**
* addValidationMethod
*
* Method to validate given Field
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
public function addValidationMethod($label, $type)
{
$this->validationElements[$label] = $type;
}
/**
* _validate
*
* Internal Validation Method
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
private function _validate($type, $fieldValue, $fieldLabel)
{
switch ($type)
{
/**
* Empty Field Validation
*/
case 'empty':
if (empty($fieldValue))
{
return 'Das Eingabefeld ' . $fieldLabel->label . ' darf nicht leer sein.';
}
break;
}
}
}
vg
michael
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 13. Jan 2011, 14:21
von darwin
Hallo Michael,
vielen Dank für die class Erweiterung..
nur hackts gerade am Aufruf:
Code: Alles auswählen
$field = & $form->addTextField('begin');
$field->setLabel($I18N_eventcal->msg("label_time"));
$form->validate('begin', 'empty');
Fehler:
Warning: Invalid argument supplied for foreach()
Danke. grz. Chris
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 25. Feb 2011, 23:14
von Evolis2k2
Habs auch mal mit deiner Klassenerweiterung probiert, Michael. Stehe irgendwie auch aufm Schlauch...
Formular hiermit erstellt:
Validierung & Anzeige:
Code: Alles auswählen
$form->addValidationMethod('title', 'empty'); // title vorher mit addTextField('title') erstellt.
$form->validate();
$form->show();
Der Server rotzt mir aber andauernd folgende Fehlermeldung aus:
Code: Alles auswählen
Fatal error: Call to undefined method rex_form::addValidationMethod() in ....
Die Klassenerweiterung wird schon richtig erkannt, aber irgendwie will er die Funktionen nicht erkennen.
Bitte um Hilfe
Danke euch,
Nico
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 26. Feb 2011, 08:22
von Mediastuttgart
hallo zusammen,
die addValidationMethod steht im bezug auf die form und nicht das field
Code: Alles auswählen
$field = & $form->addTextField('title');
$field->setLabel('Titel');
//$form-> und nicht $field
$form->addValidationMethod('title', 'empty');
in der klasse steht momentan nur die empty validierung zur verfügung.
Code: Alles auswählen
case 'empty':
if (empty($fieldValue))
{
return 'Das Eingabefeld ' . $fieldLabel->label . ' darf nicht leer sein.';
}
hier könnt ihr dann eigene cases hinzufügen.
Code: Alles auswählen
case 'email':
if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i', $fieldValue)
{
return 'Das Eingabefeld ' . $fieldLabel->label . ' ist keine E-Mail-Adresse.';
}
außerdem gibts noch einen kleinen fix, falls keine validierung hinzugefügt wurde
Code: Alles auswählen
<?php
/**
* custom_rex_form Class
*
* A Class that provides Validation Functionality.
*
* This source file is subject to the MIT license that
* is bundled with this source code in the file LICENSE.
*
* PHP Version 5.2.4
*
* @category rex_form
* @package redaxo
* @author michael alexander freund <freund@mediastuttgart.de>
* @copyright Copyright (c) 2011 MEDIASTUTTGART, digitalisierte Kommunikation.
* @license http://www.opensource.org/licenses/mit-license.php
*
*/
class custom_rex_form extends rex_form {
/**
* Elements to Validate
*
* @var array
*/
protected $validationElements;
/**
* Validation Error
*
* @var array
*/
protected $validationError;
/**
* validate
*
* Override Method
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
public function validate()
{
/**
* return true if no validations are requested
*/
if(empty($this->validationElements)) return true;
/**
* loop through validations
*/
foreach ($this->validationElements as $label => $type)
{
/**
* Fieldvalue
*/
$fieldValue = $this->elementPostValue($this->getFieldsetName(), $label);
/**
* Fieldlabel
*/
$fieldLabel = $this->getElement($this->getFieldsetName(), $label);
/**
* Validate given Field
*/
$this->validationError = $this->_validate($type, $fieldValue, $fieldLabel);
/**
* Return failed Validation Error
*/
if (isset($this->validationError))
{
return $this->validationError;
}
}
return parent::validate();
}
/**
* addValidationMethod
*
* Method to validate given Field
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
public function addValidationMethod($label, $type)
{
$this->validationElements[$label] = $type;
}
/**
* _validate
*
* Internal Validation Method
*
* @param <type> $variable description
* @throws <type> $exception
* @return <type> $value
*
*/
private function _validate($type, $fieldValue, $fieldLabel)
{
switch ($type)
{
/**
* Empty Field Validation
*/
case 'empty':
if (empty($fieldValue))
{
return 'Das Eingabefeld ' . $fieldLabel->label . ' darf nicht leer sein.';
}
break;
}
}
}
lg
michael
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 26. Feb 2011, 10:46
von Evolis2k2
Hallo Michael,
danke für deine Hinweise, aber irgendwie komme ich nicht weiter. Die addValidationMethod habe ich ja im Bezug auf die Form angewandt (siehe den Post über dir).
Ich hab deine custom_rex_form in einer php-Datei gespeichert, und in der config.inc.php vom Addon includet.
Dann hab ich die Form-Instanz erstellt:
Danach Felder hinzugefügt etc...
Code: Alles auswählen
$field = &$form->addTextField('title');
$field->setLabel('Titel');
$form->addValidationMethod('title', 'empty');
//$form->validate();
$form->show();
Aber leider gibts den Fehler:
Code: Alles auswählen
Fatal error: Call to undefined method rex_form::addValidationMethod() in /Applications/MAMP/htdocs/redaxo4_3/redaxo/include/addons/testaddon/pages/form.inc.php on line 143
Viele Grüße,
Nico
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 26. Feb 2011, 11:32
von Mediastuttgart
poste mal deine form.inc.php
lg
michael
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 26. Feb 2011, 11:56
von Evolis2k2
Code: Alles auswählen
<?
$func = rex_request('func', 'string');
$id = rex_request('id', 'int');
if($func=='edit' or $func=='add') {
$form = custom_rex_form::factory($REX['TABLE_PREFIX'].'addonarticles', "Artikel", "id=".$id, 'post', $debug);
$field = &$form->addTextField('title');
$field->setLabel('Titel');
$field = &$form->addTextAreaField('newstext');
$field->setLabel('Text');
$field = &$form->addHiddenField('status');
$field->setLabel('Status');
if($func == 'edit') $form->addParam('id', $id);
$form->addValidationMethod('title', 'empty');
$form->show();
}
?>
Grüße,
Nico
EDIT:
Okay, Fehler gefunden...
hab vergessen, in der factory die 'custom_rex_form' als letztes Argument zu übergeben. Sorry für die Umstände!
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 26. Feb 2011, 21:09
von Mediastuttgart
wie meinst du als letztes argument?
Re: wie in rex_form Pflichtfeld erstellen
Verfasst: 27. Feb 2011, 11:31
von Evolis2k2
Den letzten Parameter meinte ich...
statt:
Code: Alles auswählen
$form = custom_rex_form::factory($REX['TABLE_PREFIX'].'addonarticles', "Artikel", "id=".$id, 'post', $debug);
einfach:
Code: Alles auswählen
$form = rex_form::factory($REX['TABLE_PREFIX'].'addonarticles', "Artikel", "id=".$id, 'post', $debug, 'custom_rex_form');