Setting Validation Rules at Runtime in CakePHP

My problem right now is that a Model has a set of Validation rules like so:

var $validate = array(
    'title' => array(
        'rule' => 'notEmpty'
    ),
    'uri' => array(
        'slugged' => array(
            'rule' => '/^[a-z0-9-_]+$/i',
            'message' => 'This field should only contain characters, numbers, dashes and underscores'
        ),
        'uniqueUrl' => array(
            'rule' => array('uniqueUrl'),
            'message' => 'A page has already acquired this url'
        )
    ),
    'meta_keywords' => array(
        'rule' => 'notEmpty'
    ),
    'meta_description' => array(
        'rule' => 'notEmpty'
    ),
    'layout' => array(
        'rule' => 'notEmpty'
    )
);

The problem is that in another model that has hasOne relationship its controller also inserts data into it. I want to NOT require the title, uri and layout from that page. How do I do it?

I have a Post Model and I set Page values from there.

Array
(
    [Post] => Array
        (
            [title] => data[Post][title]
            [body] => 

Post Body

        )

    [Category] => Array
        (
            [Category] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

        )

    [Page] => Array
        (
            [meta_keywords] => data[Page][meta_keywords]
            [meta_description] => data[Page][meta_description]
        )

)

I do this from the controller to set info for the Page model

$this->data['Page']['title'] = $this->data['Post']['title'];

It turns to be like this:

Array
(
    [Post] => Array
        (
            [title] => data[Post][title]
            [body] => 

Post Body

        )

    [Category] => Array
        (
            [Category] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

        )

    [Page] => Array
        (
            [meta_keywords] => data[Page][meta_keywords]
            [meta_description] => data[Page][meta_description]
            [title] => data[Post][title]
        )

)

My problem is that I do not require a Page field when saving. Post belongsTo Page.

I don't require [Page][layout] when saving a Post as a Post uses the default view of the method in the Post Controller. A page uses static pages and require them when creating a Page, not when creating a Post.

Answers


You're preprocessing the data before it goes for validation, so you're taking some control away from validation. As you seem to be making the decision (in code) whether or not certain fields need to be artificially populated you are rendering those parts of the validation redundant and you should remove them. If you have multiple validations in php, you're going to end up confused.

Where to do this or how to do it more cleanly? WellbeforeValidate might be the 'correct' place to do this, but I would do it wherever it best fits with the logic of your application. Function should come before elegance.


Need Your Help

Dojo DataGrid Not Showing

javascript dojo dojox.grid.datagrid

In my dojo.xhrGet I have specified this load::

Why use body-parser module in node.js?

node.js parsing body-parser

I Am using the MEAN stack to develop an application following some video tutorials

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.