Access Field Level Validation Rules

I have a situation where I would like to use the max length element of a rule in a model.

So in my model I would have a few rules like this -


			

        	array('int_text', 'length', 'max'=>255),			

			array('int_name, int_vname, int_str', 'length', 'max'=>30),

			array('int_plz', 'length', 'max'=>20),

			array('int_tel', 'length', 'min' => 7, 'max'=>20), // minimum number of characters is 7



In my controller I would like to get the int_text’s ‘max’ value. Is this value associated with the $model->int_text field in an easy way to access it?

Thanks

Sandy

yes just add the

[color=#000000]array[/color][color=#666600]([/color][color=#008800]’[/color][color=#008800][font=arial, verdana, tahoma, sans-serif]int_text,[/font][/color][color=#008800][font=arial, verdana, tahoma, sans-serif]int_name, int_vname, int_str’[/font][/color][color=#666600][font=arial, verdana, tahoma, sans-serif],[/font][/color][color=#000000][font=arial, verdana, tahoma, sans-serif] [/font][/color][color=#008800][font=arial, verdana, tahoma, sans-serif]‘length’[/font][/color][color=#666600][font=arial, verdana, tahoma, sans-serif],[/font][/color][color=#000000][font=arial, verdana, tahoma, sans-serif] [/font][/color][color=#008800][font=arial, verdana, tahoma, sans-serif]‘max’[/font][/color][color=#666600][font=arial, verdana, tahoma, sans-serif]=>[/font][/color][color=#006666][font=arial, verdana, tahoma, sans-serif]30[/font][/color][color=#666600][font=arial, verdana, tahoma, sans-serif]),[/font][/color]

Sorry I wasn’t more clear -

I have the validation rules in my model already and they are working fine. BUT in the controller I would like to access the ‘max’ property that tells the max field length so I can do some hacky magic to a text field before I save it and ensure that I do not exceed the set limit which would cause a validation error. I know I can access the model’s field names by looking at the attributes but not sure their is a specific property for an attributes validation rules.

About the only way I can see doing it is to look at the return array of the active records rules() method. This looks to just return an array of the validation array’s with no specific way to look up a rule by field name (if that makes any sense).

Sandy

If $model->int_text is limited in the underling table to 255 then something like:




$model->int_text->columnSchema->size;

This is OTMH, so no guarantees. I have in the past been able to get a list of ENUM options from the table using tableSchema somehow, but don’t remember exactly how.

I will give that a try, I’m was thinking of looking at the validation rule but looking at the field size from the table is OK as well, trying not to hard code the value just to be nice.

Thanks for the idea, I’ll give it a try.

Sandy

What is your goal? If you want to change max value dynamically, you can add new property to the model class:




// in the model's class (Post for example)

class Post extends CActiveRecord

{

   public $max = 255;


   public function rules()

   {

      return array(

         array('title', 'length', 'max'=>$this->max),

         // other validation rules         

      );

   }

   

   // other stuffs

}


// in the controller's class (PostController for example)

class PostController extends CController

{

   public function actionCreate()

   {

      $model = new Post();

      $model->max = 10;

      $model->title = 'just new article';

      $model->body = 'some text...';

      if($model->save())

      {

         // if success

      }

      else

      {

         // if there are wrong data

      }

   }

   

   // other stuffs

}



Nothing quite so complicated, but want to append data to the models text field and ensure I do not go over the max field size or the data will not validate when I call save(). The idea is to pull the max from the database text_field size or the validation rule setting and use that number to ensure that I don’t append more then the field size.

Sandy

I gave this a try, but it doesn’t work. I don’t think the object has that information. I think the field is just an attribute name that is used for looking up the rest of the meta data somewhere. I’m sure if I dig around in the codebase I can find where to get the meta data for the field, for now going to just hard code the size ;)

Thanks for the suggestions, I’m going to revisit this as I would like to know if I can find out the field information just for the fun of it.

Sandy

Sorry it took so long, Haven’t been to the forums in awhile. Try:




$model->tableSchema->columns['int_text']->size;



It does not cause an error, but does not return anything for the value of the field. I’ll poke around some more and see if I can see what behind it. I used the call just after creating an instance of the model by ‘new’. And nothing yet done. It may not have the needed data yet until something is actually done with the db, but not sure all speculation :)

Thanks for the follow up!

Sandy