Yii Framework Forum: Conditional Validation - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Conditional Validation How to validate attributes without scenarios Rate Topic: -----

#21 User is offline   ToolMayNARD 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 02-August 10

Posted 04 November 2011 - 10:33 AM

Rules are dynamically built from a database table. I'm trying to use thyseus extension (http://www.yiiframew...user-management) with lot of customization, so there is no a direct rule code.

Anyway, following the code I'm using now to dynamically build the rules:

if ($field->related_field_name)
{
     $parts = explode(';', $field->related_field_name);

     if (count($parts) == 1):
          $split = explode(':', $parts[0]);

          $validatingField = $split[0];
          $compareValue = $split[1];

          $rules[] = array(
              $field->varname,
              'EConditionalValidator',
              'conditionalRules' => array($validatingField, 'compare', 'compareValue'=>$compareValue),
              'rule' => array('required'),
          );
     else:                                

         $conditionalRules = array();
                           
         foreach ($parts as $part) {
             $split = explode(':', $part);

             $validatingField = $split[0];
             $compareValue = $split[1];


             $conditionalRules[] =
                 array(
                    $validatingField,
                    'compare',
                    'compareValue' => $compareValue,
                 );
         }
                                
         $rules[] = array(
             $field->varname,
             'EConditionalValidator',
             'conditionalRules' => array(
                 'group'=>$conditionalRules,
             ),
             'rule'=>array('required'),
         );
    endif;                            
}


In the future I will do a method to manage rules different from 'required', but at the moment I need only this kind of rule type. No time to do it now :D
0

#22 User is offline   Yun 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 28-November 11

Posted 28 November 2011 - 01:12 AM

View PostAntonio Ramirez, on 04 November 2011 - 10:03 AM, said:

I think you got the wrong version... that line was deleted... please, let me check

You were right :), nevertheless, could you paste also your rules here? Not the array, the actual rules


PS: I have updated the version and fixed... thanks Mauro...


Hey, great work. one question though, can this work on an array field? like if I want to validate for every element of $somearray[index][firstname] $somearray[index][lastname] ?

Thanks
0

#23 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 01 December 2011 - 06:35 AM

View PostYun, on 28 November 2011 - 01:12 AM, said:

Hey, great work. one question though, can this work on an array field? like if I want to validate for every element of $somearray[index][firstname] $somearray[index][lastname] ?

Thanks


Never tried, but my guess is that you will have to create a custom validator and then configure the conditional to work with it.
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#24 User is offline   hnam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 10
  • Location:BSP

Posted 05 December 2011 - 03:31 AM

Hi can we use JQuery live validation for same?
On revolutions !
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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