Obtain the error message through rules verification in Yii CModel, yiicmodel

Source: Internet
Author: User

Obtain the error message through rules verification in Yii CModel, yiicmodel

Define public function rules () {return array ('name, Email ', 'required'), array ('email', 'unique ', 'message' => '{value} error'), // {value} is the added data);} use $ model = new myModel () in controller (); // instantiate the model class $ model-> attributes = $ _ POST // assign a value to attributes for the data submitted (to be verified, the field is safe $ model-> validate () in rules; // The verification rule rules $ model-> getErrors () is automatically called here (); // get error information of all verification fields $ model-> getErrors ('email '); // get error information of the currently passed field $ model-> getError ('email '); // 'message information'

 


How does YII get the wrong form information and save it to the string?

Use
CHtml: errorSummary ($ model1)
You can check the manual. The returned result is a string.

In which file is the yii form error message?

Your problem is not clearly described. Let me explain the Yii Form Verification for you!
For Yii form verification, you can refer to the actionContact method of SiteController of blog in demos:
Public function actionContact ()
{
$ Model = new ContactForm;
If (isset ($ _ POST ['contactform'])
{
$ Model-> attributes = $ _ POST ['contactform'];
If ($ model-> validate ())
{
$ Headers = "From: {$ model-> email} \ r \ nReply-To: {$ model-> email }";
Mail (Yii: app ()-> params ['adminmail'], $ model-> subject, $ model-> body, $ headers );
Yii: app ()-> user-> setFlash ('Contact ', 'Thank you for contacting us. We will respond to you as soon as possible .');
$ This-> refresh ();
}
}
$ This-> render ('Contact ', array ('model' => $ model ));
}
I won't list the models here. Let's take a look at them in the demo!
This method is used after the form is submitted.
If (isset ($ _ POST ['contactform']) to determine whether the form is submitted

$ Model-> attributes = $ _ POST ['contactform']; assign the form data to the model attribute.

If ($ model-> validate () is to call the verification in the model. if an error occurs, an error is returned, and the view is reloaded, in this way, the error section in the view will show incorrect output!

<? Php echo $ form-> errorSummary ($ model);?>

This part comes from the error output section in the view!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.