DJ Far
DJ Far

Reputation: 506

How to access cakephp model's validate from custom view template

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?

Upvotes: 2

Views: 427

Answers (1)

jtrumbull
jtrumbull

Reputation: 818

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.

Upvotes: 1

Related Questions