maryam
maryam

Reputation: 584

I want to define a validation for my field in yii

I want to define a validation for my field : All numbers except zero and two can I define my pattern? what means $ and U ,p and other character?

'pattern'=>'/^[\p{L}\s,0-9]+$/u'

Upvotes: 2

Views: 110

Answers (3)

M Gaidhane
M Gaidhane

Reputation: 531

In model add code

public function rules()
{
    return array(
            array('field_name','match', 'pattern'=>'/^[^02]+$/u','message'=>'Invalid Number.'),
    );
}

Upvotes: 0

DaSourcerer
DaSourcerer

Reputation: 6606

An overly complex regular expression won't do you any good. Instead, take advantage of the not setting, which is effectively inverting the pattern for you:

public function rules() {
    return array(
        array('field_name','pattern'=>'/^[02]$/','not'=>true),
    );
}

Upvotes: 1

Neeraj Kumar
Neeraj Kumar

Reputation: 1058

you do it using your model rule

public function rules() {
    return array(
                   // your other rules
        array('fieldName', 'patternCheck'),
    );
}

public function patterCheck($attribute, $params){
    $pattern = '/^[\p{L}\s,0-9]+$/u';
    preg_match($pattern, $attribute->fieldName, $matches, PREG_OFFSET_CAPTURE);
    return $matches;
}

This will validate your field

Upvotes: 0

Related Questions