As I assume, try/catch should perform code which is defined in catch-block,if there is any error in try-block,correct?
Following code won't do that.It will be thrown out error-in this case:
Getting unknown property: common\modules\lookup\models\LAnrede::anreden
Any ideas,how to fix this respectively what I did wrong?
<?php
use yii\helpers\Html;
use frontend\modules\bewerber\models\Bewerber;
use common\modules\basis\models\Person;
use common\modules\lookup\models\LAnrede;
use yii\base\ErrorException;
use kartik\widgets\Alert;
try {
$query_one = LAnrede::find()->innerJoin('Person', 'l_anrede.id = Person.id_anrede')->one();
$query_all = LAnrede::find()->innerJoin('Person', 'l_anrede.id = Person.id_anrede')->all();
var_dump($query_one);
var_dump($query_all);
echo"<h3>Anrede_one:" . $query_one->anrede . "</h3>";
$x = 1;
foreach ($query_all as $attributes) {
echo "<br><label>Anrede_all_$x:<br>" . $attributes->anreden . "</label>";
$x++;
}
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
?>
A hint for ponury-kostek:
I programmed in class LAnrede for each method try/catch-Block. No effect!
Here is model/class LAnrede
<?php
namespace common\modules\lookup\models\base;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use mootensai\behaviors\UUIDBehavior;
use yii\base\ErrorException;
use kartik\widgets\Alert;
/**
* This is the base model class for table "l_anrede".
*
* #property integer $id
* #property string $anrede
*
* #property \common\modules\lookup\models\Person[] $people
*/
class LAnrede extends \yii\db\ActiveRecord {
use \mootensai\relation\RelationTrait;
/**
* This function helps \mootensai\relation\RelationTrait runs faster
* #return array relation names of this model
*/
public function relationNames() {
try {
return [
'people'
];
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
* #inheritdoc
*/
public function rules() {
try {
return [
[['anrede'], 'string', 'max' => 255],
[['optimistic_lock'], 'default', 'value' => '0'],
[['optimistic_lock'], 'mootensai\components\OptimisticLockValidator']
];
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
* #inheritdoc
*/
public static function tableName() {
try {
return 'l_anrede';
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
*
* #return string
* overwrite function optimisticLock
* return string name of field are used to stored optimistic lock
*
*/
public function optimisticLock() {
try {
return 'optimistic_lock';
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
* #inheritdoc
*/
public function attributeLabels() {
try {
return [
'id' => Yii::t('app', 'ID'),
'anrede' => Yii::t('app', 'Anrede'),
];
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPeople() {
try {
return $this->hasMany(\common\modules\basis\models\Person::className(), ['id_anrede' => 'id']);
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
public function behaviors() {
try {
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'angelegt_am',
'updatedAtAttribute' => 'aktualisiert_am',
'value' => new \yii\db\Expression('NOW()'),
],
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'angelegt_von',
'updatedByAttribute' => 'aktualisiert_von',
],
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'id',
],
];
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
/**
* #inheritdoc
* #return \common\modules\lookup\models\LAnredeQuery the active query used by this AR class.
*/
public static function find() {
try {
return new \common\modules\lookup\models\LAnredeQuery(get_called_class());
} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab
echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde
'type' => Alert::TYPE_DANGER,
'title' => 'Konfigurationsfehler',
'icon' => 'glyphicon glyphicon-remove-sign',
'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,
'showSeparator' => true,
'delay' => false
]);
}
}
}
In your try block youre using $attributes->anreden. Propably there's no column with this name in your database (or you still have db cache). Pointing to the question, why try/catch didn't worked - because youre trying to catch wrong exception. Youre trying to catch yii\base\ErrorException, but it's 'yii\base\UnknownPropertyException exception. To handle it right, you should catch 'all-in-one' or expand your catch block:
All in one:
} catch(\Exception $error) {
Expanded:
} catch (ErrorException $error) {
// do some stuff here
} catch (\yii\base\UnknownPropertyException $error) {
// do some another stuff here
} catch (\Exception $error) {
// do some stuff with any other exceptions
}
It's because line that causes error use common\modules\lookup\models\LAnrede; is outside try/catch block.
More precisely you have error in file that is imported by common\modules\lookup\models\LAnrede that's why try/catch block can't
catch error. Fix error in LAnrede.
Related
I have a problem when I try to pass my phone numbers in a form to send sms with api ovh, the phone numbers on the outside of the form I can get them back but inside it sends me a null.
Thank you for the help :
public function sendSmsAction(Request $request)
{
$listphoneNumber = $request->get('telephone');
var_dump($listphoneNumber); // it returns an array phone numbers
try{
$form = $this->createForm(smsFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = $form->get('message')->getData();
$smsProvider = $this->get('app.sms.provider');
var_dump($listphoneNumber); // it returns null
$smsProvider->sendMessage($message, $listphoneNumber);
}
} catch (InvalidParameterException $e) {
sprintf("Erreur lors de l'envoie de SMS, il faut choisir un utilisateur : %s . Trace : %s", $e->getMessage(), $e->getTraceAsString()
);
throw $e;
}
return $this->render('CeUtilisateurBundle:Utilisateur:sms.html.twig', array(
'form' => $form->createView()));
}
I solved the problem
Controller
public function sendSmsAction(Request $request)
{
try{
$form = $this->createForm(smsFormType::class, array('telephone' => $request->get('telephones')));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = $form->get('message')->getData();
$smsProvider = $this->get('app.sms.provider');
$phoneNumbers=$form->getConfig()->getData();
$smsProvider->sendMessage($message, $phoneNumbers);
}
} catch (InvalidParameterException $e) {
sprintf("Erreur lors de l'envoie de SMS, il faut choisir un utilisateur : %s . Trace : %s", $e->getMessage(), $e->getTraceAsString()
);
throw $e;
}
return $this->render('CeUtilisateurBundle:Utilisateur:sms.html.twig', array(
'form' => $form->createView()));
}
formType
class smsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('telephone', HiddenType::class, array(
'data' => $options['telephone'],
))
->add('message', TextareaType::class, array(
'attr' => ['class' => 'tinymce',
'placeholder' => 'Votre message',
],
'label' => false,
'required' => true))
->add('Envoyer', SubmitType::class, array(
'attr' => array(
'class' => 'btn btn-primary',
)
));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'telephone' => null
));
}
}
following value won't be written into database, although var_dump shows value correctly.
$model=new Bewerber();
$model->umkreis=5000;
$model->wunschgehalt_brutto=7777;
var_dump($model->umkreis)
shows up
int(5000)
but value won't be written into databse. How can this be?
Here is model:
<?php
namespace frontend\modules\bewerber\models\base;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use mootensai\behaviors\UUIDBehavior;
class Bewerber extends \yii\db\ActiveRecord {
use \mootensai\relation\RelationTrait;
public $avatar;
public function relationNames() {
return [
'person',
'abrechnungsweg',
'bewerberquelle',
'kanal',
'personRekrutiertVon',
'angelegtVon',
'aktualisiertVon',
'bewerbungVorschlags',
'eMatchingbegriffs',
'ePhases',
'eStatuses',
'user0',
'user1'
];
}
public function rules() {
return [
[['id_person', 'id_person_rekrutiert_von', 'id_abrechnungsweg', 'id_kanal', 'id_bewerberquelle', 'id_ba_xml_gelernter_beruf_1', 'id_ba_xml_gelernter_beruf_2', 'id_ba_xml_gelernter_beruf_3', 'umkreis', 'avgs', 'avgs_betrag', 'wunschgehalt_brutto', 'arbeitsumfang_vollzeit', 'arbeitsumfang_teilzeit', 'arbeitszeit_bueroueblich', 'arbeitszeit_vormittag', 'arbeitszeit_nachmittag', 'arbeitszeit_abend', 'arbeitszeit_nacht', 'arbeitszeit_wochenende', 'schichtbereitschaft', 'wochenstunden_minimum', 'wochenstunden_maximum', 'quereinsteiger', 'zeitarbeit', 'pkw', 'fuehrerschein_pkw', 'fuehrerschein_lkw', 'fuehrerschein_omnibus', 'reisebereitschaft', 'kandidat', 'veroeffentlichen_bewerberboerse', 'kontakt_halten', 'aktiv', 'angelegt_von', 'aktualisiert_von', 'optimistic_lock'], 'integer'],
[['ablaufdatum_avgs', 'verfuegbar_ab', 'kandidat_seit', 'zuletzt_deaktiviert_am', 'angelegt_am', 'arbeitsuchend_seit'], 'safe'],
[['beurteilung_fachlich', 'beurteilung_persoenlich', 'sonstiges', 'anschreiben'], 'string'],
[['gesuchte_positionen', 'bemerkung_intern'], 'string', 'max' => 255],
[['id_person', 'avgs', 'arbeitsumfang_vollzeit', 'arbeitszeit_bueroueblich', 'kandidat', 'veroeffentlichen_bewerberboerse', 'kontakt_halten', 'aktiv'], 'required'],
['avgs_betrag', 'default', 'value' => '2000'],
[['optimistic_lock'], 'default', 'value' => '0'],
[['optimistic_lock'], 'mootensai\components\OptimisticLockValidator'],
[['avatar'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 3],
];
}
public static function tableName() {
return 'bewerber';
}
public function optimisticLock() {
return 'optimistic_lock';
}
public function attributeLabels() {
return [
'id' => Yii::t('app', 'ID'),
'id_person' => Yii::t('app', 'Bewerber'),
'id_person_rekrutiert_von' => Yii::t('app', 'Bewerber wurde rekrutiert von'),
'id_abrechnungsweg' => Yii::t('app', 'Abrechnungsweg'),
'id_kanal' => Yii::t('app', 'Kanal'),
'id_bewerberquelle' => Yii::t('app', 'Bewerberquelle'),
'id_ba_xml_gelernter_beruf_1' => Yii::t('app', 'Gelernter Beruf'),
'id_ba_xml_gelernter_beruf_2' => Yii::t('app', 'Alternativ Beruf'),
'id_ba_xml_gelernter_beruf_3' => Yii::t('app', 'ungelernte Tätigkeit'),
'umkreis' => Yii::t('app', 'Umkreis(km)'),
'arbeitsuchend_seit' => Yii::t('app', 'Arbeitsuchend seit'),
'avgs' => Yii::t('app', 'Avgs'),
'avgs_betrag' => Yii::t('app', 'Avgs-Betrag'),
'ablaufdatum_avgs' => Yii::t('app', 'Ablaufdatum(Avgs)'),
'wunschgehalt_brutto' => Yii::t('app', 'Wunschgehalt(brutto/€)'),
'beurteilung_fachlich' => Yii::t('app', 'Beurteilung(fachlich)'),
'beurteilung_persoenlich' => Yii::t('app', 'Beurteilung(persönlich)'),
'sonstiges' => Yii::t('app', 'Sonstiges'),
'verfuegbar_ab' => Yii::t('app', 'Verfügbar ab'),
'gesuchte_positionen' => Yii::t('app', 'Gesuchte Positionen'),
'anschreiben' => Yii::t('app', 'Anschreiben'),
'arbeitsumfang_vollzeit' => Yii::t('app', 'Arbeitsumfang(VZ)'),
'arbeitsumfang_teilzeit' => Yii::t('app', 'Arbeitsumfang(TZ)'),
'arbeitszeit_bueroueblich' => Yii::t('app', 'ArbZ/8-16pm.'),
'arbeitszeit_vormittag' => Yii::t('app', 'ArbZ(vorm.)'),
'arbeitszeit_nachmittag' => Yii::t('app', 'ArbZ(Nachm.)'),
'arbeitszeit_abend' => Yii::t('app', 'ArbZ(abds.)'),
'arbeitszeit_nacht' => Yii::t('app', 'ArbZ(nachts)'),
'arbeitszeit_wochenende' => Yii::t('app', 'ArbZ(WE)'),
'schichtbereitschaft' => Yii::t('app', 'schichtbereit'),
'wochenstunden_minimum' => Yii::t('app', 'WStd.(min.)'),
'wochenstunden_maximum' => Yii::t('app', 'WStd.(max.)'),
'bemerkung_intern' => Yii::t('app', 'interne Bemerkung'),
'quereinsteiger' => Yii::t('app', 'Quereinsteiger'),
'zeitarbeit' => Yii::t('app', 'Zeitarbeit'),
'pkw' => Yii::t('app', 'Pkw'),
'fuehrerschein_pkw' => Yii::t('app', 'FS(Pkw'),
'fuehrerschein_lkw' => Yii::t('app', 'FS(Lkw)'),
'fuehrerschein_omnibus' => Yii::t('app', 'FS(Bus)'),
'reisebereitschaft' => Yii::t('app', 'reisebereit'),
'kandidat' => Yii::t('app', 'Kandidat'),
'kandidat_seit' => Yii::t('app', 'Kandidat seit'),
'veroeffentlichen_bewerberboerse' => Yii::t('app', 'Bewerberbörse'),
'kontakt_halten' => Yii::t('app', 'Kontakt halten'),
'aktiv' => Yii::t('app', 'Aktiv'),
'zuletzt_deaktiviert_am' => Yii::t('app', 'Zuletzt deaktiviert am'),
'angelegt_am' => Yii::t('app', 'Angelegt am'),
'angelegt_von' => Yii::t('app', 'Angelegt von'),
'aktualisiert_am' => Yii::t('app', 'Aktualisiert am'),
'aktualisiert_von' => Yii::t('app', 'Aktualisiert von'),
'optimistic_lock' => Yii::t('app', 'Optimistic Lock'),
];
}
public function getPerson() {
return $this->hasOne(\common\modules\basis\models\Person::className(), ['id' => 'id_person']);
}
public function getAbrechnungsweg() {
return $this->hasOne(\common\modules\lookup\models\LAbrechnungsweg::className(), ['id' => 'id_abrechnungsweg']);
}
public function behaviors() {
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'angelegt_am',
'updatedAtAttribute' => 'aktualisiert_am',
'value' => new \yii\db\Expression('NOW()'),
],
/* das Blameable-Verhalten wurde in unsere Klasse ausgelagert */
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'id',
],
];
}
public static function find() {
return new \frontend\modules\bewerber\models\BewerberQuery(get_called_class());
}
public function upload() {
$x=0;
if ($this->validate()) {
foreach ($this->avatar as $uploaded_file) {
$uploaded_file->saveAs(Yii::getAlias('#uploading') . '/' . $uploaded_file->baseName . '.' . $uploaded_file->extension);
$x++;
}
if($x>0)
return true;
}
return false;
}
}
I had developed a block in 2.5. I installed the block successfully..and gave permissions settings on front end to view this block only for one role users say 'Manager'. So, Manager/Admin can only view this block and no one else.
But this block is still visible for all. Could you please judge me.. where I went wrong.. Here is my leisure block code
Blocks/block_leisure/block_leisure.php
<?php
class block_leisure extends block_base
{
public function init()
{
global $CFG;
$this->title = get_string('leisure', 'block_leisure');
}
public function get_content()
{
global $COURSE, $DB, $PAGE, $CFG, $USER, $CFG, $SESSION, $OUTPUT;
if ($this->content !== null)
{
return $this->content;
}
$this->content = new stdClass;
$context = $PAGE->context;
$this->content->text = 'This is a leisure block content';
$this->content->footer = 'Footer here...';
return $this->content;
} // Function - get_content().
public function getmodules()
{
return true;
}
}
Blocks/block_leisure/db/access.php
<?php
defined('MOODLE_INTERNAL') || die;
$capabilities = array(
'block/leisure:myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'user' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/leisure:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
'block/leisure:viewpages' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'legacy' => array(
'guest' => CAP_PREVENT,
'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'coursecreator' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'block/leisure:managepages' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'legacy' => array(
'guest' => CAP_PREVENT,
'student' => CAP_PREVENT,
'teacher' => CAP_PREVENT,
'editingteacher' => CAP_ALLOW,
'coursecreator' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
)
);
and as usual I have lang folder, version.php and read me file.
You need to do something with the capability you have defined, otherwise it will have no effect.
Check the capability within get_content, then return null if nothing should be displayed.
I understood the CakePHP validation that way: Only fields which are in the request (e.g. ['Post']['NAME'] and ['Post']['STREET']) are validated in the appmodel, even if in the appmodel there are declarations for many more fields.
Now I call this->Post->save($this->request->data) and Cake throws validation errors for fields which are not in the request and now I don´t know what to do.
Controller:
debug of this->request->data:
array(
'Post' => array(
'TYPE' => '1',
'B/S' => 'S',
'LATITUDE' => '50.98549',
'LONGITUDE' => '11.31551',
'CART' => 'jkl',
'MARKET' => 'jk',
'INCOMETYPE' => '1',
'INCOME' => '8',
'DEADLINE' => '1',
'DATE' => '31.08.2014',
'TIME' => '',
'PREFERREDDATES' => 'nach Absprache',
'address' => 'Fuldaer Straße, 99423, Weimar',
'STREET' => 'Fuldaer Straße',
'DELIVERYAREA' => '99423',
'CITY' => 'Weimar',
'TEL' => '1234567654321',
'saveAppendix' => 'edit'
)
)
Controller:
debug of $this->Post-validationErrors:
array(
'ZIPCODE' => array(
(int) 0 => 'Bitte eine gültige Postleitzahl eingeben.'
),
'VEHICLE' => array(
(int) 0 => 'Bitte das Fahrzeug angeben'
),
'SPACE' => array(
(int) 0 => 'Bitte den verfügbaren Platz angeben'
)
)
Controller:
Code with debug-statements:
if (isset($this->request->data['Post']['saveAppendix']) && $this->request->data['Post']['saveAppendix'] == "edit") {
debug($this->request->data);
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Dein Inserat wurde erfolgreich bearbeitet.', 'flash_success');
$this->set('action', 'goToIndex');
$this->render('addRedirect', 'ajax');
} else {
debug($this->Post->validationErrors);
...further code...
}
}
EDIT
As requested by burzum, the validation rules:
class Post extends AppModel {
public function beforeValidate(array $options = array()) {
$request = $this->data[$this->alias];
if (isset($request['INCOMETYPE']) && ($request['INCOMETYPE'] == "1" || isset($request['INCOMETYPE']) && $request['INCOMETYPE'] == "2")) {
$this->data[$this->alias]['INCOME'] = preg_replace(
'/^([^,]*).*$/', '$1', $request['INCOME']
);
$this->data[$this->alias]['INCOME'] = preg_replace(
'/\D/', '', $request['INCOME']
);
};
//für validation die schaltungsart-variablen setzen
if (isset($request['DEADLINE']) && $request['DEADLINE'] == "1") {
$this->validator()->remove('TIME');
$date = $request['DATE'];
};
if (isset($request['DEADLINE']) && $request['DEADLINE'] == "2") {
$this->validator()->remove('PREFERREDDATES');
$date = $request['DATE'];
};
if (isset($request['DEADLINE']) && $request['DEADLINE'] == "3") {
$this->validator()->remove('DATE');
$this->validator()->remove('TIME');
};
if (isset($date) && $date <> "") {
//datepicker date in sortierbares datum wandeln: Y M D
$tmpdate = list($day, $month, $year) = explode('.', $date);
$altdate = $year.$month.$day;
$this->data[$this->alias]['ALTDATE'] = $altdate;
};
if (isset($request['INCOMETYPE']) && $request['INCOMETYPE'] == "4") {
$this->validator()->remove('INCOME');
};
if (isset($request['INCOMETYPE']) && $request['INCOMETYPE'] == "5") {
$this->validator()->remove('INCOME');
};
// B/S-Feld füllen mit entsprechendem String
if (isset($request['TYPE']) && isset($request['B/S'])) {
if ($request['TYPE'] == 1 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Kurier";
};
if ($request['TYPE'] == 2 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Bildungsdienstleistung";
};
if ($request['TYPE'] == 3 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Elektronik & Technik";
};
if ($request['TYPE'] == 4 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Freizeit & Sport";
};
if ($request['TYPE'] == 5 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Haushalt und Garten";
};
if ($request['TYPE'] == 6 && $request['B/S'] == "S") {
$this->data[$this->alias]['B/S'] = "Suche Soziales";
};
if ($request['TYPE'] == 1 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Kurier";
};
if ($request['TYPE'] == 2 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Bildungsdienstleistung";
};
if ($request['TYPE'] == 3 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Elektronik & Technik";
};
if ($request['TYPE'] == 4 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Freizeit & Sport";
};
if ($request['TYPE'] == 5 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Haushalt & Garten";
};
if ($request['TYPE'] == 6 && $request['B/S'] == "B") {
$this->data[$this->alias]['B/S'] = "Biete Soziales";
};
};
//inserat-datum darf nicht in vergangenheit liegen
/*if ($request['DATE'] == date("d.m.Y") && $request['TIME'] < date("H:i")) {
unset($this->data[$this->alias]['TIME']);
};*/
return parent::beforeValidate($options);
}
var $validate = array(
'STREET' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Hidden input B/S wurde geändert'
),
'rule-2' => array(
'rule' => '/^[a-z]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'STREET' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte eine Straße angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'HOUSENUMBER' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte eine Hausnummer angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'ZIPCODE' => array(
'rule-1' => array(
'rule' => array('postal', null, 'de'),
'message' => 'Bitte eine gültige Postleitzahl eingeben.',
'last' => true
),
'rule-2' => array(
'rule' => array('ZipExists'),
'message' => 'Postleitzahl existiert nicht!'
)
),
'CITY' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte eine Stadt angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'CART' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte etwas eingeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'MISC' => array(
'rule-1' => array(
'allowEmpty' => true
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'MARKET' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte einen Markt angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'DATE' => array(
'rule' => 'notEmpty',
'required' => 'true',
'message' => 'Bitte das Datum eingeben'
),
'PREFERREDDATES' => array(
'rule' => 'notEmpty',
'required' => 'true',
'message' => 'Bitte die passenden Zeiten eingeben'
),
'DEADLINE' => array(
'rule' => 'notEmpty',
'required' => 'true',
'message' => 'Bitte die Art der Schaltung festlegen'
),
'TIME' => array(
'rule' => 'time',
'required' => 'true',
'message' => 'Bitte die Zeit eingeben'
),
'INCOME' => array(
'rule' => 'notEmpty',
'required' => 'true',
'message' => 'Bitte die Bezahlung eingeben'
),
/*'INCOME' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte die Bezahlung angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),*/
'INCOMETYPE' => array(
'rule' => 'notEmpty',
'required' => 'true',
'message' => 'Bitte die Art der Bezahlung festlegen'
),
'VEHICLE' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte das Fahrzeug angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'DELIVERYAREA' => array(
'rule-1' => array(
'rule' => array('postal', null, 'de'),
'message' => 'Bitte eine gültige Postleitzahl eingeben.',
'last' => true
),
'rule-2' => array(
'rule' => array('ZipExists'),
'message' => 'Postleitzahl existiert nicht!'
)
),
'SPACE' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte den verfügbaren Platz angeben'
),
'rule-2' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{1,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'TEL' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte die Telefonnummer eingeben'
),
'rule-2' => array(
'rule' => '/^[0-9_\/\d\w\s?!\.:,\'"\(\)=\+-]{0,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
)
),
'NAME' => array(
'rule' => '/^[a-z0-9_äÄöÖüÜߧ$%&*\/\d\w\s?!\.:,\'"\(\)=\+-]{0,}$/i',
'message' => 'Hacker kommen hier nicht weit!'
),
'LATITUDE' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte gültige Adresse angeben'
),
'rule-2' => array(
'rule' => 'numeric',
'message' => 'Bitte gültige Adresse angeben'
)
),
'LONGITUDE' => array(
'rule-1' => array(
'rule' => 'notEmpty',
'message' => 'Bitte gültige Adresse angeben'
),
'rule-2' => array(
'rule' => 'numeric',
'message' => 'Bitte gültige Adresse angeben'
)
),
'TYPE' => array(
'rule-1' => array(
'rule' => 'numeric'
),
'rule-2' => array(
'rule' => array('maxLength', 1)
)
)
);
public function ZipExists($zipcode){
$value = array_values($zipcode);
$value = $value[0];
$Zipcode = ClassRegistry::init('Zipcode');
$valid = $Zipcode->find('count', array('conditions'=> array('Zipcode.zipcode' =>$value)));
if ($valid >= 1){
return true;
}
else{
return false;
}
}
};
If i understand you correctly, than you got it all wrong.
cake will validate everything that is in the validation rules, and required. keep in mind that omitting a field, might be a validation error for itself..
edit:
Validation works in the context of the model, not the request. it validates the data that will be saved to the database, against the validation rules.
The meaning is: it will compare the POST data to the rules.
and, if a field is required in the validation-rules, it will throw an error.
Please attach the validation errors
I got RegisterController.php and inside of it I got:
class RegisterController extends AppController {
public $name = 'Register';
public $components = array('Session');
public function index() {
if ($this->request->is('account')) {
// if ($this->Post->save($this->request->data)) {
echo "got it";
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
// }
}
}
}
My /View/Register/index.tcp file:
<?php
echo $this->Form->create('Account');
echo $this->Form->input('username');
echo $this->Form->input('password');
$options = array(
'label' => 'Register',
'class' => 'submit'
);
echo $this->Form->end($options);
?>
And my /Model/Account.php file:
<?php
class Account extends AppModel {
public $name = 'Account';
public $validate = array(
'username' => array(
'rule' => 'notEmpty'
),
'password' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Can not be empty.'
),
'minLength' => array(
'rule' => array('minLength', '8'),
'message' => 'Min. 8 chars.'
)
)
);
}
?>
The problem is that, I'm clicking on the submit button, and nothing happends. It should at least check for validation.
Where is the error?
Should work if you add the action in options:
echo $this->Form->create('Account', array('action' => 'your/url'));