0

In an web application ,I need to add some rules in the model when create a object. But when I add My rules to the model there are some mistake. For example the mobile number user input when sumit the form.

In view page,there are somew inputs need user to complete ,then I want to add some rules to validate the user input to prevent use input some invalided values.That means using my own validate funcion.

Yours who using Yii Framework,I need Your help,thank you.

3
  • Do you need validation rules for attributes or what else ?Please specify Commented Mar 20, 2013 at 5:47
  • what kind of rules you want to add? Commented Mar 20, 2013 at 5:54
  • The required validation is added ,and I need to add other validate .First the inputs can't contain the special code like <html> or *_$...,or use the regular expression to validate. Commented Mar 20, 2013 at 6:01

2 Answers 2

4

EDIT: Using own function in validation rules- In the rule specify the function with attribute like this-

array('username','checkuniquename'),

Then define the function logic in same model Like this:

public function checkuniqueemail($attribute)
{
    $record=Users::model()->findByAttributes(array($attribute=>$this->email));
    if($record!==null)
         $this->addError($attribute, 'This email has been already taken please choose a different one');

}

You can define multiple validation rules on single attribute in Yii models.

return array(
                    array('contact_no','numerical', 'integerOnly'=>true),
                    array('contact_no','length', 'min'=>8 ),
                    array('name, contact_no', 'required'),
        array('name, contact_no', 'length', 'max'=>255),
        array('password','pattern'=>'/^[A-Za-z0-9_!@#$%^&*()+=?.,]+$/u', 'message'=>'Spaces or given characters are not allowed'),
    );

There is lot of validation you can specify in your model.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Saxena,Thank you for your reply.If I use my own function to validate, how to use the validate function work on it.
@SudhanshuSaxena Just a sidenote: You picked a bad example, because there's already a built in unique validator which does exactly the same like your custom validator.
@MichaelHärtl yeah...thanks for considering... i just explaining how to add custom validation rule...i forgot that Yii has already Unique validator.
0

First you need to specify a validation rule in your model like

return array(
.......    
array('duration', 'days_available'),
.....

);

where duration is the field name and days_available will be the function name. Then you need to write the function within the model.

public function days_available($attribute,$params)
{
  //provide proper condition
  $this->addError('duration','validation message');
}

1 Comment

You forget to define the $param in the rules array.You are passing two parameters in the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.