Yii Framework Forum: Validating fails - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validating fails [solved]

#1 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 415
  • Joined: 20-April 17
  • Location:Hannover

Posted 13 January 2018 - 09:44 PM

Hi guys,
following code in Controller prevents writing data into database. If I negate boolean variable $valid to !valid, everything is gonna all right. However, I am concerned about not validating my input.
What do you think: Should I remove code,which is validating data or shouldn't I?

            if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) {
                $valid = $model->validate();
                $isValid = $model_person->validate();
                $isValid = $valid & $isValid;
                if ($isValid) { //will be true after having pushed submittButton
   				$model_person->id_person_art = 3;
                    $model_person->save();
                    $model->id_person = $model_person->id;
                    $model->save();
                    return $this->redirect(['view', 'id' => $model->id]);
                } else {
   				$error_person = $model_person->getErrors();
                    $error_bewerber = $model->getErrors();
   				foreach ($error_person as $values) {
                        foreach ($values as $ausgabe) {
                            echo"<p>" . $ausgabe . "</p>";
                        }
                    }
                    foreach ($error_bewerber as $values) {
                        foreach ($values as $ausgabe) {
                            echo"<p>" . $ausgabe . "</p>";
                        }
                    }
                    echo Growl::widget([
                        'type' => Growl::TYPE_DANGER,
   					'title' => 'Oh snap!',
                        'icon' => 'glyphicon glyphicon-remove-sign',
[                        'body' => 'for unknown reasons,your input could not be validated...',
                        'showSeparator' => true,
                        'delay' => 2000,
                        'pluginOptions' => [
   						'showProgressbar' => true,
                            'placement' => [
                                'from' => 'top',
                                'align' => 'center',
                            ]
                        ]
                    ]);
   				return $this->render('create', [
                                'model' => $model,
                                'model_person' => $model_person,
                    ]);
                }
            } else {
                return $this->render('create', [
                            'model' => $model,
                            'model_person' => $model_person,
   			]);
            }

P.S.:
echo"<p>" . $ausgabe . "</p>";
in loop won't show any errors. $valid just is falsePosted Image
0

#2 User is offline   Alexander Chertovsky 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 15-February 16

Posted 14 January 2018 - 01:49 AM

$valid & $isValid seem weird onlg to me?
And in php look like && as i remember :)

If no errors inside of models - u have syntax issue. Also maybe weird override of beforeSave.
0

#3 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,546
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 14 January 2018 - 03:14 AM

you are using a unary and (&) to combine model and person model validation result

$isValid = $valid & $isValid;

// should be

$isValid = $valid && $isValid;

0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,630
  • Joined: 16-February 11
  • Location:Japan

Posted 14 January 2018 - 07:27 AM

View Postalrazi, on 14 January 2018 - 03:14 AM, said:

you are using a unary and (&) to combine model and person model validation result

$isValid = $valid & $isValid;

// should be

$isValid = $valid && $isValid;



I would also use the logical operator '&&' instead of the bitwise operator '&' here.

But if $valid is 'true' and $isValid is 'true', then '$valid & $isValid' should be 'true', shouldn't it?
0

#5 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 415
  • Joined: 20-April 17
  • Location:Hannover

Posted 14 January 2018 - 08:52 AM

View Postsoftark, on 14 January 2018 - 07:27 AM, said:

I would also use the logical operator '&&' instead of the bitwise operator '&' here.

But if $valid is 'true' and $isValid is 'true', then '$valid & $isValid' should be 'true', shouldn't it?

U are right, all of you. No idea why using bitoperator. That's nonsens,,of course.
I changed operator as suggested but didn't succeed. Nevertheless,validation is failing. I assume faulty <div> tags in formular is causing this failing. Publishing formular is not possible, 'cause it's simply to extensive (<760 lines of code)
Because data will be saved into database negating $valid, I will do it. Any statements about my proposal of yii2-experts are urgently required!

P.S.: Publishing code via Copy&Paste is a hard thing in this forum. I always have to remove tags like below at each line of code, manually. That's annoying me.
Any ideas how to avoid this curiosity?
Here an example of same code in terms of this curiosity

[size=3]          if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) {[/size]
[size=3]                $valid = $model->validate();[/size]
[size=3]                $isValid = $model_person->validate();[/size]
[size=3]                $isValid = $valid && $isValid;[/size] //changed operator,without succeeding
[size=3]                if (!$isValid) {[/size]  //negating boolean will save data into database
[size=3]                    $model_person->id_person_art = 3;[/size]
[size=3]                    $model_person->save();[/size]
[size=3]                    $model->id_person = $model_person->id;[/size]
[size=3]                    $model->save();[/size]
[size=3]                    return $this->redirect(['view', 'id' => $model->id]);[/size]
[size=3]                } else {[/size]
[size=3]                    $error_person = $model_person->getErrors();[/size]
[size=3]                    $error_bewerber = $model->getErrors();[/size]
[size=3]                    foreach ($error_person as $values) {[/size]
[size=3]                        foreach ($values as $ausgabe) {[/size]
[size=3]                            echo"<p>" . $ausgabe . "</p>";[/size]
[size=3]                        }[/size]
[size=3]                    }[/size]
[size=3]                    foreach ($error_bewerber as $values) {[/size]
[size=3]                        foreach ($values as $ausgabe) {[/size]
[size=3]                            echo"<p>" . $ausgabe . "</p>";[/size]
[size=3]                        }[/size]
[size=3]                    }[/size]
[size=3]                    echo Growl::widget([[/size]
[size=3]                        'type' => Growl::TYPE_DANGER,[/size]
[size=3]                        'title' => 'Oh snap!',[/size]
[size=3]                        'icon' => 'glyphicon glyphicon-remove-sign',[/size]
[size=3]                        'body' => 'Aus unbekannten Gründen konnten die Tabellen nicht validiert werden<br>Bitte informieren Sie den Hersteller gemäß obiger Angaben!',[/size]
[size=3]                        'showSeparator' => true,[/size]
[size=3]                        'delay' => 2000,[/size]
[size=3]                        'pluginOptions' => [[/size]
[size=3]                            'showProgressbar' => true,[/size]
[size=3]                            'placement' => [[/size]
[size=3]                                'from' => 'top',[/size]
[size=3]                                'align' => 'center',[/size]
[size=3]                            ][/size]
[size=3]                        ][/size]
[size=3]                    ]);[/size]
[size=3]                    return $this->render('create', [[/size]
[size=3]                                'model' => $model,[/size]
[size=3]                                'model_person' => $model_person,[/size]
[size=3]                    ]);[/size]
[size=3]                }[/size]
[size=3]            } else {[/size]
[size=3]                return $this->render('create', [[/size]
[size=3]                            'model' => $model,[/size]
[size=3]                            'model_person' => $model_person,[/size]
[size=3]                ]);[/size]
[size=3]            }[/size]

0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,630
  • Joined: 16-February 11
  • Location:Japan

Posted 14 January 2018 - 06:09 PM

Quote

Because data will be saved into database negating $valid, I will do it.


No, you should not do it. It's a worst kind of practice.

I would try to find out what causes the validation error.
    if ($model->load(Yii::$app->request->post()) && $model_person->load(Yii::$app->request->post())) {
        $valid = $model->validate();
        if (!$valid) {
            $error_bewerber = $model->getErrors();
            foreach ($error_bewerber as $attr => $errors) {
                echo "<p>attribute = $attr</p>";
                foreach ($errors as $error) {
                    echo "<p>$error</p>";
                }
            }
        }
        $isValid = $model_person->validate();
        if (!$isValid) {
            $error_person = $model_person->getErrors();
            foreach ($error_person as $attr => $errors) {
                echo "<p>attribute = $attr</p>";
                foreach ($errors as $error) {
                    echo "<p>$error</p>";
                }
            }
        }
        if ($valid && $isValid) {
            ...


I noticed that you misunderstand the format of model errors.
http://www.yiiframew...html#getErrors()-detail
0

#7 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 415
  • Joined: 20-April 17
  • Location:Hannover

Posted 17 January 2018 - 06:42 AM

View Postsoftark, on 14 January 2018 - 06:09 PM, said:

I noticed that you misunderstand the format of model errors.
http://www.yiiframew...html#getErrors()-detail

So, I did in understanding format of model errors., finally.


var_dump($model_person->getErrors());
die();

showed up an attribute,which was in required rules, but not in formular. I write this attribute statically into database, so I returned it from rules. Validating still is in use.
So,this thread can be succesfully closed!
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,630
  • Joined: 16-February 11
  • Location:Japan

Posted 17 January 2018 - 07:59 AM

Yeah, you've got the right solution. :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users