显示错误消息yii html表单

时间:2016-05-22 12:06:28

标签: yii

你好我在yii中有一个html表单我希望控制器中的操作返回到表单并显示错误消息,因为我不知道如何显示表单显示错误有我的代码:< / p>

CONTROLLER

class ProfileController extends Controller {
    public function actionSave() {   
        $patients = new Patients;
        $questions = new PatientsRegistrationQuestions;
        $payments = new PaymentTypes;
        $gender = new Gender;
        if (isset($_POST['Patients'])) {
            $patients->save_personal_data();
            $patients->name = $_POST['Patients']['name'];
            $patients->mobile = $_POST['Patients']['mobile'];
            $patients->email = $_POST['Patients']['email'];
            $patients->dob = $_POST['Patients']['dob'];
            $patients->weight = $_POST['Patients']['weight'];
            $patients->gender_id = $_POST['Patients']['gender_id'];
            $patients->height = $_POST['Patients']['height'];
            //$patients->payment_type_id = $_POST['Patients']['payment_type_id'];
            $patients->activation_date = '0000-00-00';
            $patients->credit = 0;
            $patients->archive = 0;
            $patients->balance = 0;
            $patients->first_login = 0;
            $patients->guide_status = 0;
            $patients->passcode = '';
            $patients->patient_id = $questions->patient_id;
            // $patients->gender_id = $gender->gender_id;
            if ($patients->validate()) {
                $patients->save(false);
            } else {
        $this->redirect(Yii::app()->request->urlReferrer);
        }
   }
}

观点

 <form  id="register-form" method="post" action=" <?php echo Yii::app()->getBaseUrl(true) . '/profile/save' ?>">
 <div class="form-group" id="errors">
            </div>
                    <!--the first form-->

                    <div id="register-form1">


                        <fieldset id="step1">
                            <div class="form-group">
                                <input size="60" maxlength="200" placeholder="<?php echo Yii::t('translation', 'First Name') ?>" class="form-control valid" name="Patients[firstname]" id="Patients_name" type="text">  
                            </div>

                            <div class="form-group">
                                <input size="60" maxlength="200" placeholder="<?php echo Yii::t('translation', 'Last Name') ?>" class="form-control valid" name="Patients[lastname]" id="Patients_name" type="text">  
                            </div>
                            <div class="form-group">
                                <input size="60" maxlength="255" placeholder="<?php echo Yii::t('translation', 'Mobile') ?>" class="form-control valid" name="Patients[mobile]" id="Patients_mobile" type="text">
                            </div>
                            <div class="form-group">
                                <input size="60" maxlength="250" placeholder="<?php echo Yii::t('translation', 'Email') ?>" class="form-control valid" name="Patients[email]" id="Patients_email" type="text"> 
                            </div>

                            <div class="form-group">
                                <input size="60" maxlength="255" placeholder="<?php echo Yii::t('translation', 'Password') ?>"class="form-control valid" name="Patients[password]" id="Patients_password" type="password">  
                            </div>

                            <div class="form-group">
                                <input size="60" maxlength="255" placeholder="<?php echo Yii::t('translation', 'Confirm Password') ?>"class="form-control valid" name="Patients[repeatpassword]" id="Patients_repeatpassword" type="password">  
                            </div>
                            <div class="form-group">
                                <input type="checkbox" name="agree" aria-required="true" class="valid" aria-invalid="false">I agree to <a href="#" data-toggle="modal" data-target="#paymentTermsModal">Terms and conditions</a>
                                <div class="errorAgree"><label id="agree-error" class="error" for="agree" style="display: none;"></label></div>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-gate pull-right"> <?php echo CHtml::link(Yii::t('translation', 'Cancel'), Yii::app()->createUrl('/site/index')); ?></button>
                                <a class="btn btn-gate pull-right" id="next" href="#register-form2">Next</a>

                            </div>
                        </fieldset>

                    </div>
</form>

2 个答案:

答案 0 :(得分:0)

基于某些模型保存记录的机制是这样的:

public function actionSave()
{
    $model = new ModelName;

    // ...

    if(isset($_POST['ModelName']))
    {
        $model->attributes = $_POST['ModelName'];

        if($model->validate())
        {
            $model->save(false);
            $this->redirect(...); //redirect to some page after successful save
        }
    }

    $this->render('viewName', array(
        'model'=>$model // <-- passing variable to view, so that you can show error messages and data in form
    ));
}

现在,在视图文件中,检查模型是否有错误:

<?php if($model->hasErrors()): ?>
    // show what is stored in $model->getErrors() and show it using foreach loop for example
<?php endif; ?>

顺便说一下,建议使用CHtml类来呈现表单元素而不是手动编写,或者更好的方法 - 使用CActiveForm。更多信息,例如:http://www.larryullman.com/2011/01/20/creating-forms-with-the-yii-framework/

答案 1 :(得分:0)

我不需要在我的控制器中创建一个chtml表单

?php echo $ form-&gt; errorSummary($ model); ?&GT;

相关问题