Cmodel->Adderror() Attribute Label In Message

Hi

I have some model with custom validation rule




    public function rules()

    {

        return array(

             ...

             array('role_id', 'exist', 'attributeName'=>'id', 'className'=>'UserRole', 'message'=>'Select {attribute}!'),

             array('mail', 'someFunction'),

             ...

        );

    }


    public function attributeLabels()

    {

        return array(

            ...

            'mail' => 'E-mail',

            'role' => 'User role',

            ...

        );

    }


    public function someFunction($attribute,$params)

    {

        if(!$this->hasErrors())

        {

            .....


            if(....)

            {

                $this->addError('mail','{attribute} ...!');

            }

            elseif()

            {

                $this->addError('mail','....{attribute} ...!');

            }


        }

    }

When model can not be validated by role_id field rule, I get message ‘Select User role!’

But if model can not be validated by mail filed, I get messages like ‘{attribute} …!’

So I want to get {attribute} substituted by attribute label, but it is not substituted. Why?

Dear Friend

We have to put the message inside a double quotes.

for example.




$this->addError($attribute,"invalid $attribute");



Regards.

It works wrong, cause it return me not label value, but label key in error message. Not “E-mail” but ‘mail’

I put Form.php into components folder

All my form model files nested it.


class Form extends CFormModel

{

    public function returnAttributeLabel($attribute)

    {

        $array = $this->attributeLabels();


        if(isset($array[$attribute]))

            return $array[$attribute];

        else 

            return 'None';        

    }    

}

Dear Friend

Sorry I have misunderstood your requirement.

I hope the following will serve the purpose.




public function validateAttribute($attribute,$params)

{

	if($this->$attribute > 100)

	    $this->addError($attribute,$this->getAttributeLabel($attribute). " is greater than 100");

		

}



It will generate nice looking attribute label if even attribute label is not set.

If you want attribute values in error message we can do the following.




public function validateAttribute($attribute,$params){

		if($this->$attribute > 100)

		   $this->addError($attribute,$this->$attribute. " is greater than 100");

		

		}



Thank you!!!