Question about CValidator.

Hi guys,

I have quite interesting task to resolve, I have a model with some built in validation rules.

I have a text area in my view. Which takes as an input multiple lines. User need to be able to add multiple lines to this textarea. And I need to validate them line by line.

For example I am having this user input in a text area:

rule1 blahblahblah

rule2 skflskfslfsl

rule3 blahblah

I am using match shortcut to validate user input, like this:

array(‘rules’,‘match’,‘pattern’=>’/regex goes here/i’,‘message’=>‘Not validated’),

My problem is that match validator validates only first line of user’s input and stops further processing.

But I need to be able to validate all lines one by one with regex which I got.

For example if first line was validated and second line contain some incorrect syntax, I am not getting the message.

If you don’t use a framework you can split user input and then process it line by line like this:

$exported = explode("$delimeter",$_POST[‘userinput’]);

How can I develope the same functionality in Yii?

Add ‘m’ modifier to your regex pattern. See: http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Hi this is not exactly what I wanted, if you’ll read my post it is saying I need to check textarea line by line. Not multi line regex.

I’ve extended CValidator by myself, and putting code here in case anyone will need something like that in future.





class CMultiLineValidator extends CValidator

{

        public $pattern;


	protected function validateAttribute($object,$attribute)

	{

               $value=$object->$attribute;


               if($this->pattern===null)

			throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.'));

                

                $arr = explode("\n", $value);


                reset($arr);


                foreach($arr as $line):


                    if(!preg_match($this->pattern,$line))

                    {

                        $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is invalid.');

                        $this->addError($object,$attribute,$message);

                    }


                endforeach;

	}

}