emersonthis
emersonthis

Reputation: 33408

CakePHP how to modify validation from shell script

I'm using CakePHP v2.6 and I'm writing a shell script which needs to have slightly different validation rules then those in the $validates property of the model.

I've read the section of the book about modifying validations on the fly, but all the examples are from within a model. And when I try to do $this->MyModel->validator() in the shell script, I get:

Fatal Error Error: Call to undefined function validator()

Why is this?

Update: Strangely, the following code DOES work:

unset($this->MyModel->validate['fieldName'] );

My code:

<?php
App::uses('AppShell', 'Console/Command');
App::uses('CakeSchema', 'Model');

class ScrapeShell extends AppShell {

    public $uses = array('Listing', 'Neighborhood', 'ListingPhoto');

    function __construct() {
       parent::__construct();
       //initialize some variables
   }

   public function myMethod() {

       #bypass validation on description to allow HTML
       unset($this->Listing->validate['description'] ); //this works
       //$this->Listing->validator()->remove('description', 'noTags'); //this errors
       ...

Upvotes: 0

Views: 146

Answers (2)

T.J. Compton
T.J. Compton

Reputation: 405

At least part of the problem is that you've broken CakePHP's shell construction.

You need to change your __construct method to read as follows:

public function __construct($stdout = null, $stderr = null, $stdin = null) {
    parent::__construct($stdout, $stderr, $stdin);
    //your code here
}

As for the model's child objects, it's hard to say what could be interfering with that instantiation without seeing your model code, but if you've overwritten model constructors too, that can interfere with the proper model setup.

Upvotes: 1

floriank
floriank

Reputation: 25698

Are you sure your shell and web app are running the same CakePHP core version? Try to debug the CAKE_CORE_INCLUDE_PATH.

I'm using a global version myself, so when I just call "cake" it will execute the global version, if I call .\bin\cake it will use the one from the repo (3.0 in this case)

And are you sure the app is finding the right core version? The second line of core in question will work in any case because it was there before 2.6. I'm pretty sure your model does not exist the correct version of the core Model class.

Upvotes: 0

Related Questions