1
votes

I have a model which contains emails with some other fields. I want a custom filter in Yii CGridView's advance search which when applied, lists only Invalid Email IDs (using regular expression '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$')

Note: I dont want to add any column in CGridView

1
That's not the regex for valid email adresses; that would look more like (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_}`{|}~-]+)*|”(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*”)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]); but actually is way more complicated. - Marcus Müller
Thanks for your correction, But can you please tell me the answer of my real problem - Mohammad Qasim
Sorry, I'm no Yii expert, just wanted to point out that email addresses usually are not uppercase alphanumerical...; I would have written an answer if I had one. However, your question is not really well-written. Maybe add what you've already tried, why the official documentation has failed you, etc. - Marcus Müller

1 Answers

0
votes

I hope to help you out,

you will need several things

  1. the line for filtering in the column of your choice...(i recommend you create a new attribute for this, ask me in comments if you want to know more why.) this goes in the cgridview of course:

    'filter'=>CHtml::activeCheckBox($model, $attributeEmail)

  2. the condition in your search of function that brings up the model.

Supposing you have a criteria inside your search in your model wich help you with your filtering what you need is ...

if($this->EMAIL == TRUE)
{
    $criteria->addCondition("\"t\".\"EMAIL\" email NOT LIKE '%_@__%.__%'");
}

Why not to use regex and make a KISS approach ? better read this first...

Sql script to find invalid email addresses

I'd be glad to hear your comments it is interesting question for yii dev's btw