PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Yii Framework; How to check if a model attribute has a SPECIFIC validator?

 March 03, 2022     forms, validation, yii     No comments   

Issue

I am making a custom ActiveForm method, but it requires the model to have a certain custom validator attached the the attribute that is being passed through (otherwise who knows what will happen!?)

My question is simply... is there a way to run this check in the code that is reliable?

I don't want to add the validator at runtime. That would create confusion and possibly let someone use this type of field where it ought not be used.

So I want to say something like:

if( model NOT HAVE validationMethod ON property)
  throw Exception;

Solution

I'm also not sure why you want to do this, but in addition to viewing the rules array you can do:

$model->getValidators($attribute)

to check which validators are active for a particular attribute (or all attributes, if the arg is null. (I'm assuming $attribute = property in your example.)

This will return all the validator objects that are active for the current scenario and you can check if a predefined or custom class exists. It also gets you a bit more info than just the rules array (i.e., the properties of the validator class).



Answered By - ldg
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing