Model Validation In Rules Function...

Hi everyone, I’m new here and this is my first crack at using yii.

I have a form with 4 input fields: firstname, lastname, street and city_state. I also have set up a number of defaults in my model script below to show on the form before users enter data.

class PeopleForm extends CFormModel


public $firstname = 'First Name*';

public $lastname = 'Last Name*';

public $street = 'Address';

public $city_state = 'City/State';

public function rules()


  return array(array('firstname, lastname', 'required'));



In my controller I have:

if(Yii::app()->getRequest()->getIsAjaxRequest()) {

    echo CActiveForm::validate(array($ppl_model)); 



if(isset($_POST[‘PeopleForm’])) {

if ($ppl_model->validate()) {

//Always get’s here :(



As seen above, I use both ajax and server side validation on my controller. How do I modifiy the rules so that when a user does not enter any text it retruns an error. As it currently is, the form submits the default values for the fields and it passes validation.

You don’t assign POST to model attributes.


if ($ppl_model->validate())




$ppl_model->attributes = $_POST['PeopleForm'];

Thanks 1cichy. I do set the attributes…forgot to type it in here. It doesn’t make a difference. The validate function treats the submitted default values as actual values and does not return an error.

Hi obi1,

Probably you’ve been confused about the attribute values and labels.

The default (initial) values of the attributes should be null, and the labels of them should be defined in attributeLabels() function.

class PeopleForm extends CFormModel


    public $firstname;

    public $lastname;

    public $street;

    public $city_state;


    public function attributeLabels()


        return array(

            'firstname' => 'First Name',

            'lastname' => 'Last Name',

            'street' => 'Address',

            'city_state' => 'City/State',



Thanks softark. I was concerned about the attribute labels, but that is not the problem I am currrently facing. Nevertheless, I appreciate your introducing me to atttribute labels. It does make my error message display better.

The problem I have is similar to this thread but it was not resolved to my satisfaction. My problem is that when the default value on the input box for firstname for instance, is submitted, the validate function accepts it as a valid value. I want the validate function to reject default values (like ‘First Name’, ‘Last Name’, etc) and to return an error message saying that the particular input field is still required.

I think it’s a trade off. Whether you want to make use of “default values” or you want to make use of “require” validator.

It might not be appropriate to use default values for input prompts … I would use "hint" texts instead.

But if you do want to set the default values and treat them as empty, you may write a custom validator … but I won’t thumb up for it. It could be cumbersome, and would not work in client-side in spite of the cost you pay.

Good point. Using ‘hints’ texts is probably a better way to go. Thanks.

Or, “placeholder” attribute of HTML5. :)