1
votes

i just write my first article so please tell me if i've done something wrong! My Problem: I want to validate data given by url.

../Logs/requests?from=2011-10-18T16:15:00&to=2011-10-18T16:30:00&fmt=csv

I have just find out that there is an option to validate with the rules added to the Model.

public $validate = array(
    'request_id' => array(
        'alphaNumeric' => array(
            'rule'     => 'alphaNumeric',
            'required' => true,
            'message'  => 'Alphabets and numbers only'
        ),
    )
);

Using "ModelName->set($params)" in the Controller and after that the "ModelName->validates()"-function should deliver the answer if its valid or not. The only differenz between my solution and the solution at http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html is that my controller using a couple of Models to collect the data for the response. The problem is that the "validates()"-function just return "valid" even if i put in special-characters or other stuff what should be "not valid"-signed by the model-rules. Help!

1
Can you add the content of the $params variable to your question? You may need to add a ModelAlias key to the $params array, like this; $params = array('MyModelAlias' => $params); - thaJeztah
i've just test it with this "$params = array('request_id'=>'4*G/&2')". - user2140111
add: the thing is, i dont want to insert. I just want to validate and select later on. (thanks for the fast response :)) - user2140111
Did $params = array('request_id'=>'4*G/&2') work? If not try this; $params = array('ModelName' => array('request_id'=>'4*G/&2'));. You are correct that you are able to manually 'validate' the data, without saving it, however, the model will look inside the 'params' data for a key named after the 'alias' of the model (e.g. 'ModelName') and only validate fields inside that key. If the ModelAlias key is missing, it will find no 'fields' to validate - thaJeztah
Well i have changed it to "$params = array('LogBackend' => array('request_id'=>'4*G/&2'));" - user2140111

1 Answers

0
votes

This is not an answer, but added to assist the OP;

I've created a test controller/model to test your situation. I deliberately did not extend the 'AppController' / 'AppModel' to remove any code in those from causing problems.

My test model (app/Model/Some.php)

class Some extends Model
{
    public $name = 'Some';
    public $useTable = 'false';


    public $validate = array(
        'request_id' => array(
            'alphaNumeric' => array(
                'rule'     => 'alphaNumeric',
                'required' => true,
                'message'  => 'Alphabets and numbers only'
            ),
        )
    );
}

My test controller (app/Controller/SomeController.php)

class SomeController extends Controller
{
    public $uses = array('Some');

    public function index()
    {
        $this->autoRender = false;

        $params = array('Some' => array('request_id'=>'4*G/&2'));
        $this->Some->set($params);
        $result = $this->Some->validates();
        debug($result);

        $params = array('Some' => array('request_id'=>'AAAA'));
        $this->Some->set($params);
        $result = $this->Some->validates();
        debug($result);

    }
}

Outputs:

\app\Controller\SomeController.php (line 32)
false
\app\Controller\SomeController.php (line 37)
true

This test setup seems to work as planned, so you may try to test these in your application as well to narrow down the cause of your problem. Maybe some behavior is attached to your AppModel that contains a 'beforeValidate()' callback and disables the validation of the request_id field?