PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 2, 2022

[FIXED] Fatal error: Call to a member function isAttributeRequired()

 January 02, 2022     php, yii     No comments   

Issue

I am having the following form

<?php $form = $this->beginWidget('CActiveForm', array(
    'id'=>'change-password',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'focus'=>array($model,'old-password')
    ));
?>
<div class="row">
    <?php echo $form->labelEx($model,'oldPassword'); ?>
    <?php echo $form->passwordField($model,'oldPassword'); ?>
    <?php echo $form->error($model,'oldPassword'); ?>
</div>
<div class="row">
<?php echo CHtml::submitButton('Submit');
?>
</div>
<?php $this->endWidget(); ?>

The above form throws an error as

Fatal error: Call to a member function isAttributeRequired() on a non-object in /framework/web/helpers/CHtml.php on line 1414

I am new you yii framework so i am unable to guess where the error is. can anyone help

Edit 1

public static function activeLabelEx($model,$attribute,$htmlOptions=array())
    {
        $realAttribute=$attribute;
        self::resolveName($model,$attribute); // strip off square brackets if any
        $htmlOptions['required']=$model->isAttributeRequired($attribute);
        return self::activeLabel($model,$realAttribute,$htmlOptions);
    }

Edit-2

If remove <?php echo $form->labelEx($model,'oldPassword'); ?> am getting error as

Fatal error: Call to a member function getValidators() on a non-object in CHtml.php on line 2236

Solution

The $model variable does not contain a Model object, check if you you pass the correct object in render function.

ex for the create page:

/* in the controller class */
public function actionCreate() {
    $model=new YourModel;
    $this->performAjaxValidation($model);

    if(isset($_POST['YourModel']))
    {
        $model->attributes=$_POST['YourModel'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}
/* in the create.php view */
....
echo $this->renderPartial('_form', array('model'=>$model));
....


Answered By - luca.caimi.lc
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing