Yii Framework Forum: Custom Validation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom Validation

#1 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 15 October 2013 - 07:22 AM

Hi everyone
Excuse me for my very bad english
What directory do I save custom validation class and how import it
thanks
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,857
  • Joined: 17-January 09
  • Location:Russia

Posted 15 October 2013 - 08:00 AM

No need to import anything. Just put the class under corresponding namespace and then write "use" statement where using it.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,335
  • Joined: 21-February 11
  • Location:Greece

Posted 21 November 2013 - 07:25 AM

Hi

you could use either protected/extensions or protected/components (Personally Used components for this type of class)

check also this
http://www.yiiframew...alidator-class/

I agree with Gustavo :)
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#4 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 01 January 2014 - 11:37 AM

I put my custom validator file under component directory but I received an error

Fatal Error – yii\base\ErrorException
Class 'phonenumber' not found
0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,857
  • Joined: 17-January 09
  • Location:Russia

Posted 02 January 2014 - 04:21 PM

What is the name of the class? You need to use fully qualified name including namespace.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#6 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 02 January 2014 - 07:19 PM

View Postsamdark, on 02 January 2014 - 04:21 PM, said:

What is the name of the class? You need to use fully qualified name including namespace.

Thanks for you'r reply.
When I put my custom validation file under yii/validators directory and use fully qualified name this work. but when I put under another directory doesn't work.
I dont know how and where I add Yii::setAlias to auto load that file.
Thanks again.
0

#7 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,857
  • Joined: 17-January 09
  • Location:Russia

Posted 03 January 2014 - 05:56 AM

You don't need to use setalias. Just use fully qualified name of the class.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#8 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 03 January 2014 - 12:06 PM

For example I extend validator class named PhoneValidator in file PhoneValidator.php and put in under myproject/additionalValidators and in rules I write
[['phonenumber'], 'yii\validators\PhoneNumberValidator']

but this not work but if I put PhoneValidator.php under yii/validators it work.
For first situation realy not nead use setalias?
Again excuse me for my bad english.
0

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,857
  • Joined: 17-January 09
  • Location:Russia

Posted 03 January 2014 - 04:01 PM

Well, you need to use the same class name as you're using for the class. i.e. if you're putting your class to myproject/additionalValidators and the class namespace is myproject/additionalValidators you should write myproject\additionalValidators\PhoneNumberValidator in rules.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#10 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 03 January 2014 - 07:25 PM

I did what you said but doesn't work until I add
Yii::setAlias('@myproject', realpath(__DIR__ . '/../../myproject'));

to params.php file
also in official document
is wrote

Quote

It is recommended that a path alias be defined for each root namespace so that you can use Yii the class autoloader without any further configuration

0

#11 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,857
  • Joined: 17-January 09
  • Location:Russia

Posted 04 January 2014 - 01:19 PM

Well, that's true if you have your own root namespace that doesn't match application namespace.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#12 User is offline   mojtaba6485 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 26-February 13

Posted 04 January 2014 - 02:00 PM

View Postsamdark, on 04 January 2014 - 01:19 PM, said:

Well, that's true if you have your own root namespace that doesn't match application namespace.

Thanks alot for you'r responsibility.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users