2
votes

I have created a 'form' template to bake custom theme views (Cake 2.6.0). I am trying to access field properties from the Model's $validate array. However, accessing $model->validate shows an empty array. My model has several fields with rules defined in it's $validate property.

Is the $validate property not accessible while baking custom views? If not, how do I find out whether a field is required, or if it uses 'rule =>' 'url', for example?

1
i don't think there is a way to access the rules from the view , i suggest to use some ajax plugin for validation where you can have the custom rules ... - may saghira
It's actually not in the view. It's in the form.ctp template the bake shell uses to create the view. Some model attributes are available, but some, like the model's $validate array, just seem not to be set. - DJ Far
can you more share/describe of your code??? - Supravat Mondal

1 Answers

1
votes

The view template(s) used by cake bake view are an instance of class TemplateTask and does not have direct access to the Model, View or Controller. What you want to do is import the controller to your custom view template:

Console\Templates\[themename]\views\[template].ctp

<?php
// The Controller's name
$controllerName = Inflector::pluralize($modelClass).'Controller';

// Import the Controller
App::import('Controller', $controllerName);

// Instantiate the Controller
$Controller = new $controllerName();

// Load the Controller's classes
$Controller->constructClasses();

//...the rest of your template

You now have access to your controller @ $Controller. To access your validate property, you would use $Controller->{$modelClass}->validate.