Allow access to NotEmpty

New to CakePHP here. I look at the documentation on the site, trying to collect some basic data for the model I am creating. This will probably be the first of many questions I have about CakePHP.

In CakePHP Book , validation rules seem to indicate two different methods to ensure that the field is not empty - AllowEmpty and NotEmpty .

Question: Is there any tangible difference between the two? CakePHP states that validation rules must be present in your model or controller - is it better for the model and the other is better for the controller? The book says nothing about it. I assume this is an older method that is just still around?

What gives? Should I use a specific or both, or does it not matter?

Edit: I decided to check the CakePHP 1.3 class documentation for it (to check the default value of the AllowEmpty attribute), but it doesn't even appear. It is not in the source code , either ... is there something that I am missing?

+6
source share
3 answers

Welcome to the cake. I hope you will like it.

This is definitely one of the weird aspects of Cake.

notEmpty is a rule in itself. You can define it in your $validation attribute. You can assign a message when this check failed. You can consider this as any other validation rule.

allowEmpty is a variant of another validation rule, usually not notEmpty . This is not an in-and-of-yourself validation rule. This would allow, for example, to determine that the varchar field allows an empty string, '' or a string without more than 20 characters.

Edit:

Here is some code

 // model validation using 'notEmpty' $validation = array( 'fieldName' => array( 'notEmpty' => array( 'rule' => 'notEmpty', 'message' => 'This value may not be left empty!' ), ... // other rules can go here ), ... // other fieldName can go here ); // model validation using 'allowEmpty' to create an optional field $validation = array( 'fieldName' => array( 'maxLength' => array( 'rule' => array('maxLength', 20), 'message' => 'This field may only contain 20 characters!', 'allowEmpty' => true // we'll also accept an empty string ), ... // other rules can go here ) ... // other fieldName can go here ); 
+8
source

It is very simple to do server side validation in cakephp
Here is the code to check (noEmpty, maxlength) for the same field.

 'fieldName' => array( 'rule' => array('maxLength', 20), 'message' => 'fieldName should be less than 20 characters', 'allowEmpty' => true ), 'fieldName' => array( 'notEmpty' => array( 'rule' => array('notEmpty'), 'message' => 'Please enter field name', ), ), 
0
source

I found a case where I had to use 'allowEmpty' => false instead of the rule => 'notEmpty'. I had a form with loadable input (type = 'file') that had a notEmpty validation rule, and it continued to refuse validation, even if the debugger showed that the array [] was loaded. When I removed the rule "notEmpty" and set allowEmpty => false, it worked, throwing an error when no file was selected and did not accept it when selected. It should have something to do with the value being an array, not a text value.

0
source

Source: https://habr.com/ru/post/891851/