Reference: Model rules validation. This is a reference to be used for Model rule validation and is compiled from the Yii documentation and code.
The purpose is to have all the information gathered in one place instead of scattered. This reference is not an intro. See The Definitive Guide to Yii, Declaring Validation Rules for a tutorial. How validation works ¶ The CModel class uses a method named CModel::rules() to return an array with the rules for validation. public function rules(){ return array( array('username, password', 'required'), array('password_repeat', 'required', 'on'=>'register'), array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'), ); } The code above is an example of what the CModel::rules() function may look like.
These rules are applied by the CModel::validate() method which returns a boolean value. Parameters of a validator ¶ Create your own Validation Rule. Some times the core validation rules provided by Yii won't satisfy all your needs, so you'll need to create your very own validation rule.
Easy approach: inside-model rule ¶ The easiest way to create a new validation rule is inside the model that is going to use it. Let's say that you want to check if a user password is safe enough. Usually you could achieve this result just by using the CRegularExpressionValidator but for the sake of this guide let's pretend that validator does not exist. first of all in your model class you'll have to add two constants const WEAK = 0; const STRONG = 1; then in your rules method you'll have to set the rule make sure that you won't give the rule the name of an existing one, otherwise you are going to have some troubles later. Now the only thing you need to do is create a new method inside the model, named after the validation rule you just declared. The new method you just created accepts two arguments: Complete approach: extending the CValidator class ¶ Links ¶
How to use a single form to collect data for two or more models? Assume we want to use a single HTML form to collect input for both model A and model B, and we want to display input errors (if any) in the same error summary box.
We can define the following action code: public function actionCreate(){ $a=new A; $b=new B; if(isset($_POST['A'], $_POST['B'])) { $a->attributes=$_POST['A']; $b->attributes=$_POST['B']; $valid=$a->validate(); $valid=$b->validate() && $valid; if($valid) { $a->save(false); $b->save(false); } } $this->render('create', array( 'a'=>$a, 'b'=>$b, )); } To add these new fields to your Create form, add your 2nd table fields no stored in a 2nd model. A's create.php: <? You usually place the specific fileds in the _form.php file if you are working off Gii created CRUD files. <? The above approach can also be used if we have more than two models to deal with. Links ¶ Ajax Validation Version.
Working with Forms: Using Form Builder. When creating HTML forms, we often find that we are writing a lot of repetitive view code which is difficult to be reused in a different project.
For example, for every input field, we need to associate it with a text label and display possible validation errors. To improve the reusability of these code, we can use the form builder feature. 1. Basic Concepts ¶ The Yii form builder uses a CForm object to represent the specifications needed to describe an HTML form, including which data models are associated with the form, what kind of input fields there are in the form, and how to render the whole form. Form input specifications are organized in terms of a form element hierarchy. When users submit a form, the data entered into the input fields of the whole form hierarchy are submitted, including those input fields that belong to the sub-forms. 2. Working with Forms: Creating Model. Before writing the HTML code needed by a form, we should decide what kind of data we are expecting from end users and what rules these data should comply with.
A model class can be used to record these information. A model, as defined in the Model subsection, is the central place for keeping user inputs and validating them. Depending on how we make use of the user input, we can create two types of model. If the user input is collected, used and then discarded, we would create a form model; if the user input is collected and saved into database, we would use an active record instead. Both types of model share the same base class CModel which defines the common interface needed by form. Note: We are mainly using form models in the examples of this section. 1.
Below we create a LoginForm model class used to collect user input on a login page. Class LoginForm extends CFormModel{ public $username; public $password; public $rememberMe=false; } 2. 3. Declaring Safe Attributes 4. 5. 6.