CakePHP Number Validation - php

Is there any cakephp validation for numbers only. That input field should only accept Numbers whether its float OR integer.
I have this field validation in model, But I don't know what rule to use.
'payment_commission' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
Any help is much appreciated..

You want to use numeric to test for valid numbers:-
'payment_commission' => array(
'required' => array(
'rule' => 'notEmpty', // use notBlank as of CakePHP 2.7
'message' => 'Required.'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Numbers only'
)
)
You can alternatively use decimal to check for floats or naturalNumber for natural numbers. Full details can be found in the offical docs.
You can also make sure that the input field only allows for numeric values:-
<?= $this->Form->input('payment_commission', ['type' => 'number']) ?>
This has the added advantage that on some mobile devices the keyboard will switch to a number pad for easier input. It's important to ensure that the server-side validation (the Cake part) is in place though as there are ways for people to get round client-side validation.

You can try below cakePHP code for number validation.
'payment_commission' => array(
'numeric' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
)
You can use below html pattern code in for your textbox.
<input type="text" name="payment_commission" pattern="[0-9]{10}" title="Three letter country code">
Here you can manage your pattern by changing [0-9]{10}. ([0-9] = expected integers from User, {10} = length).

You can use following rule to validate numbers(float and integer):
'payment_commission' => array(
'numeric' => array(
'rule' => array('decimal', 2),
'message' => 'Please enter only numbers',
'allowEmpty' => false,
'required' => true,
)
),
Let me know if this works for you.

Related

Phone number validation using zend framwork

I am using below code to validate by phone number in my project,
$phone = new Zend_Form_Element_Text('phone', array(
'label' => $view->_('phone'),
'value' => '',
'class' => 'text-size text hastip',
'title' => $qtips_messages['key_phone'],
'required' => true,
'tabindex' => '13',
'validators' => array(
array('Digits', false, array(
'messages' => array(
'notDigits' => "Phone Invalid Digits, ex. 1234567890",
'digitsStringEmpty' => "",
))),
array('notEmpty', true, array(
'messages' => array(
'isEmpty' => 'Phone can\'t be empty'
)
)),
array('StringLength', false, array(10, 10, 'messages' => array(
'stringLengthInvalid' => "Phone Length Invalid entry",
'stringLengthTooShort' => "Phone Invalid Length , ex. 1234567890"
))),
),
'filters' => array('StringTrim'),
'decorators' => $this->requiredElementDecorators,
'description' => '<img src="'.$baseurl.'/images/star.png" alt="required" />',
'filters' => array('StringTrim')
));
$this->addElement($phone);
This works well when field is empty,invalid,length. my requirement is ,phone field should accept spaces also like 123 456 7890.
Kindly advice on this.
I'm not too sure how the digits validator handles the spaces but I guess it won't allow them (or you wouldn't be posting here). So instead of using the digits validator, change it for a regex validator. I'm not good with regex so instead of me giving you the expression I suggest you take a look at the following page containing several expressions for phonenumbers: A comprehensive regex for phone number validation
Adding the validator should look something like:
array('regex', false, array('/^[0-9 ]+$/'))
Like I said, I'm no good with regex so the regex itself might not be completely accurate, but adding a validator like that should solve your problem.

cakephp show validation errors with jquery

I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?

How to give validator for floating point number in zend form?

Following is the piece of code which has been taken from form,
$debit_amount = new Zend_Form_Element_Text('debit_amount', array(
'label' => 'Debit_amount',
'value' => '',
'class' => 'text-size text',
'required' => true,
'tabindex' => '13',
'validators' => array(
array('Digits', false, array(
'messages' => array(
'notDigits' => "Invalid entry, ex. 10.00",
'digitsStringEmpty' => "",
))),
array('notEmpty', true, array(
'messages' => array(
'isEmpty' => 'Debit_amount can\'t be empty'
)
)),
),
'filters' => array('StringTrim'),
//'decorators' => $this->requiredElementDecorators,
//'description' => '<img src="' . $baseurl . '/images/star.png" alt="required" />',
));
$this->addElement($debit_amount);
When i try submit the form using floating number , it throws my error "Invalid Enter....". It does not validate floating point number. Kindly advice.
Use Zend_Validate_Float:
'validators' => array(
array('Float', ...
You are getting the error message because the Digits validator only validates digits. There is a comment about it in the Zend_Validate documentation:
Note: Validating numbers
When you want to validate numbers or numeric values, be aware that this validator only
validates digits. This means that any other sign like a thousand separator or a comma
will not pass this validator. In this case you should use Zend_Validate_Int or
Zend_Validate_Float.
As #Zyava says, using Zend_Validate_Float is the way to go.

CakePhp: Form validation on several fields?

I've some conditions to a form to be valid that has to be on several fields instead one, how to do this.
Some example for a registration:
enterprise or firstName+lastName filled
mobile phone number OR static phone number filled
How to do this? Is there an implemented way or I've to do it myself every time?
Thank you
Write your own validation rules. Cake Book: Custom validation rules
Attach a rule to the enterprise field what checks if it is filled or the first, last names are filled. Attach another rule to the name fields to check if the names or the enterprise fields are filled. Similar to the phone fields. You are in the model so you can reach all passed fields in $this->data
I am not sure if I understand the question correctly but you can create your own validation rule and then apply it for the desired fields (not really the other way around). See here
Otherwise Cakephp has a lot of pre-built validation rules here is an example:
var $validate = array(
'title' => array(
'titleRule1' => array (
'rule' => array('minLength', 1),
'required' => true,
'allowEmpty' => false,
'last' => true,
'message' => 'Please enter a title.'
),
'titleRule2' => array(
'rule' => array('between', 1, 100),
'message' => 'Your title must be between 1 and 100 characters long.'
)
),
'description' => array(
'rule' => array('minLength', 1),
'required' => true,
'allowEmpty' => false,
'last' => true,
'message' => 'Please write a description.'
)
);

CakePHP multirule validation

I am trying to get multiple rules to run during an upload validation. One validation is built in and one is custom. The custom one works fine however the built in one is not working. The custom one extension was working on another field earlier just fine. Do I have this setup correct?
var $validate = array(
'description' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'required' => true
),
'title' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'required' => true
),
'Filedata' => array(
'rule' => array('FileExtCheck'),
'message' => 'Please supply a valid type.',
'required' => true
),
'Thumbdata' => array(
'dimensions'=>array(
'rule' => array('dimensions','120','142'),
'message' => 'Your image dimensions are incorrect: 120x142'
),
'extension' => array(
'rule' => array('extension'=>array('jpg','jpeg','png')),
'message' => 'Please supply a valid type.',
'required'=>true
)
)
);
The one I am having issue with is Thumbdata. I want the Thumbdata field to be required and make sure it has correct dimensions and is an image of jpg, jpeg or png. I don't want animated gif's.
I guess, you have a syntax error - unnecessary =>. Should be:
'rule' => array('extension', array('jpg','jpeg','png')),

Categories