Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:14:16+00:00 2026-06-17T06:14:16+00:00

I am new to Yii framework & having some confusion regarding validation rules which

  • 0

I am new to Yii framework & having some confusion regarding validation rules which are written inside model. For ex:
array(’email’,’safe’),
array(‘username, password’, ‘required’, ‘on’=>’login, register’),

1- what is the use of safe validator & where to use it?
2- what is the use of on scenario and how to use it? Suppose I have wriiten ‘on’=>’register’, then whether ‘register’ is action name or anything else.

Even after going through lots of document, still it is not very clear for me. Can someone explain it with example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:14:17+00:00Added an answer on June 17, 2026 at 6:14 am

    what is the use of safe validator & where to use it?

    safe validator can be used to indicate “this field can accept anything and no validation should be performed”. It’s relevant during mass-assignment:

    $model->attributes = $_POST['Model'];
    

    If the field has no rules set, its value will not be updated after this assignment. If a field does not require any validation, but should still be updated in this case, you can use the safe rule to indicate this (but be careful with that, validation is a good thing).


    what is the use of on scenario and how to use it? Suppose I have wriiten ‘on’=>’register’, then whether ‘register’ is action name or anything else.

    In this case, register is a scenario (an arbitrary string of your choice, describing what is happening to the model). You assign the model’s scenario in the controller, before performing mass assignment and other work. When the time comes to validate the model, Yii will look at the model’s scenario property and run the validators based on that. (In a reasonably sized project, you will want to use class constants instead of arbitrary strings for consistency.)

    Example: user password change. You can set up the following validator to mark the password field as required only in this scenario, and not otherwise:

    // validator in the model class
    array('password', 'required', 'on' => 'change-password'),
    
    // code in the right controller's action
    $user->scenario = 'change-password'; // set the scenario
    $user->attributes = $_POST['User'];
    $user->validate(); // if the password is not set, this will result in a validation error
    

    Look at this answer for some more information and links to further reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Yii framework, and just came across module , extension &
I am new to Yii framework and i need to define a function which
I'm POST-ing some data to PHP server in Yii framework. Login works fine. which
I am pretty new to the Yii framework, and I needs some help with
I am using PHP Yii Framework with MongoDB(yiimongodbsuite). I have created a Model which
I'm prety new to yii framework so i could use some help with something.
I am new to Yii framework and I am having trouble with retrieving data
I'm new using Yii framework. I show a list a checkbox. But it's not
Possible Duplicate: Yii CHtml::radioButtonList - CSS to align horizontally Am new to yii framework
I'm building a web application using Yii framework, and i'm quite new to it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.