CakePHP 2.x: How to manually set validationErrors without a model?

Koen

After reading cakePHP: how set error validation to input field manually in controller, I was wondering how to show a validationError from a controller if we use a form without a model?

So for example, we have a view checkSomething.ctp, with a Form that we can submit.

echo $this->Form->create(false); // If we put "SomeModel" here, it would work.
echo $this->Form->input("myField");

And say we are on /Home/CheckSomething/.

class HomeController extends AppController{
    public function CheckSomething(){

        // So manually validate a field
        if(strlen($this->request->data["myField"]) < 5){

            // myField is not valid, so we need to show an error near that field
            $this->SomeModel->invalidateField("myField", "You must enter at least 5 characters"); 

            // How to do this?

        }
    }
}

We cannot use a model here... How to set a validationError for a field without a model? How to manually invalidate a field that comes from such a form?

Holt

The easiest way would be to send the error to the view directly:

$errors = [];
if (strlen($this->request->data["myField"]) < 5) {
    $errors['myField'] = 'You must enter at least 5 characters'; 
}
$this->set('errors', $errors);

And in your view:

echo $this->Form->create(false);
echo $this->Form->input('myField', [
    'error' => isset($errors['myField']) ? $errors['myField'] : false
]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CakePHP 2.x Accessing Another Model

From Dev

CakePHP - Save fails; validationErrors is empty

From Dev

installing cakephp 3 manually, without composer

From Dev

rename Model Name from find('all') with cakePHP 2.X

From Dev

Why is my model not being saved? (CakePHP 2.x)

From Dev

Highcharts/Highstocks: How to manually set the X-axis range

From Dev

cakePHP accessing table without model

From Dev

set condition on another model in cakephp

From Dev

How call specific action using only attributes without using controller and action name in link CAKEPHP 2.x

From Dev

how to manually set/persist a primary key in ORM Doctrine2

From Dev

How to set current location manually in the map (sencha touch2)?

From Dev

How to do Password hashing in cakephp 3 manually?

From Dev

How To set timezone in cakephp?

From Dev

multiple model validation cakephp 2

From Dev

How to set a header and render a twig template without renderView() method in symfony2.X controller

From Dev

Python: how to check if an item was added to a set, without 2x (hash, lookup)

From Dev

Get inserted ID with CakePHP without working with model

From Dev

Invalidate CakePHP Model Cache without turning on Debug

From Dev

How to resolve "ajax 403 error forbidden in CAKEPHP 2.x"

From Dev

How to convert cakephp 2.x component and helper to 1.3

From Dev

How to send a 410 gone in cakephp 2.x

From Dev

How to defer inline jQuery in CakePHP 2.x

From Dev

How pass a variable during ajax rendering in cakephp 2.x

From Dev

How would I get write this query in cakePHP 2.x

From Dev

How to send a 410 gone in cakephp 2.x

From Dev

How to access SESSION of loggedInUser outside CakePHP 2.x

From Dev

CakePHP 2.7 save() failing with a single empty key in validationErrors

From Dev

How to load Model in Cakephp directly

From Dev

how to set locale correctly manually?

Related Related

  1. 1

    CakePHP 2.x Accessing Another Model

  2. 2

    CakePHP - Save fails; validationErrors is empty

  3. 3

    installing cakephp 3 manually, without composer

  4. 4

    rename Model Name from find('all') with cakePHP 2.X

  5. 5

    Why is my model not being saved? (CakePHP 2.x)

  6. 6

    Highcharts/Highstocks: How to manually set the X-axis range

  7. 7

    cakePHP accessing table without model

  8. 8

    set condition on another model in cakephp

  9. 9

    How call specific action using only attributes without using controller and action name in link CAKEPHP 2.x

  10. 10

    how to manually set/persist a primary key in ORM Doctrine2

  11. 11

    How to set current location manually in the map (sencha touch2)?

  12. 12

    How to do Password hashing in cakephp 3 manually?

  13. 13

    How To set timezone in cakephp?

  14. 14

    multiple model validation cakephp 2

  15. 15

    How to set a header and render a twig template without renderView() method in symfony2.X controller

  16. 16

    Python: how to check if an item was added to a set, without 2x (hash, lookup)

  17. 17

    Get inserted ID with CakePHP without working with model

  18. 18

    Invalidate CakePHP Model Cache without turning on Debug

  19. 19

    How to resolve "ajax 403 error forbidden in CAKEPHP 2.x"

  20. 20

    How to convert cakephp 2.x component and helper to 1.3

  21. 21

    How to send a 410 gone in cakephp 2.x

  22. 22

    How to defer inline jQuery in CakePHP 2.x

  23. 23

    How pass a variable during ajax rendering in cakephp 2.x

  24. 24

    How would I get write this query in cakePHP 2.x

  25. 25

    How to send a 410 gone in cakephp 2.x

  26. 26

    How to access SESSION of loggedInUser outside CakePHP 2.x

  27. 27

    CakePHP 2.7 save() failing with a single empty key in validationErrors

  28. 28

    How to load Model in Cakephp directly

  29. 29

    how to set locale correctly manually?

HotTag

Archive