Field validated, display success message.


I’m trying to display a success message, if there are no validation errors for each model attribute.

In Short: Display a Success Message instead of an error Message, the same way there is addError, i want addMessage but it would allow the form to submit. I’ve been trying to do it via flashes, but its not working and It won’t work via ajax.

Here I’m just trying to test it for the firstName attribute in my model/form

if(!$model->hasErrors(‘firstName’)) {

            Yii::app()->user->setFlash('success','First name looks great!');


Except that its always doing it, when the page reloads…simply because it has no errors when the page is loaded.

I wanted it to check if it has passed its validated state, then display the message.



why doing it by attribute and not on model->validate or model->save()?

I want to do it, attribute by attribute. As soon as they tab to the next field and that field was validated. Just like twitter signup.




Well you can use onAfterValidate and check whether they are no errors or not and setFlash with content that you will after display extracting the messages from it.


However is there anyway to do it via like addError, instead its like addMessage. If I add an error, the page won’t validate and save. Flashes won’t work on ajax forms. They will only work if I refresh the form or something.

Essentially, it would be nice if there is a addMessage or addError that allows validation and saving. An addError that allows a form to validate.

If anyone has a good solution, please let me know :)

this is a strange requirement but it is so common in china .

may be should add some methods like : addSuccessMessage() :lol:

any way there is a sentence : "no message is a good message!"

Dear Friends

This is quiet possible by creating a ghost object in parallel with the regular object of CFormModel or CActiveRecord.

If validation succeeds for a particular attribute, success messages can be added as error messages to ghost object.

Then these success messages can be made visible on the form in normal request and as well as AJAX request.

I made an attempt on this in the following post.

Displaying Success Messages in CActiveForm