Laravel validation required rule not working - validation

I need to add required rule if one field is available. Also need to check if it is an integer and 10 digit. So I added the rule like below.
'id_number' => 'sometimes|required|digits:10|integer'
Validations works only when the field is available. But here required rule is not working. It directly shows integer error even if the field is empty.
I use Laravel 5.1

Finally I figured it!
You need to change the order of required rule to last. It works when I add rule like this,
'id_number' => 'sometimes|digits:10|integer|required'

Related

Laravel Validation 'starts_with' alpha

I am looking to use Laravel validation for user input fields, and all works well aside from one rule I'm looking to enforce: how to ensure a field starts with alpha (where it allows alpha_dash elsewhere). I tried the PHP 'regexp' version [A-Za-z] (shown in code snippet below) but to no avail. I also tried 'starts_with:alpha' also to no avail. I'm hoping to avoid regexp and the like and would rather wait for Laravel solution if there is no simple solution.
Thanks!
'username' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20|unique:users,username',
'firstname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20',
'lastname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:30',
Try this rule instead of alpha_dash & starts_with:
'required|min:2|max:30|regex:/^[A-Z][a-zA-Z0-9_-]+$/'

Vuetify default rules implementation

I am using Vuetify and use its default way of adding rules to input fields.
I know there is this rule:
v => !!v
This checks if the the form input isn't empty. But how can I make it in such a way that it only accepts alphabetical letters, numbers or even apply a regex to it? I can't find anything in the docs. Can someone with any experience help me out?
So I assume that you've probably sorted this now but for anyone finding this from google etc.
To add a new rule, you need to add it to your vue component, either through an import or just adding it straight to your data object. You name it as you would any other data property and it's an array of the tests like the v => !!v one you mentioned. You then add the OR operator followed by the text to show on a failed validation.
So to add a regex that only allows letters you would have this:
data () {
return {
alphaRule: [
v => /[a-zA-Z]+$/.test(v) || 'Field must only contain letters'
]
}
}
then on your form field you would have <v-text-field :rules="alphaRule"></v-text-field>
That said, I would highly recommend adding all of your rules to a Rules.js file and binding the rules globally so that you can access them anywhere, have a centralised repository for them, and it helps keeps your code DRY too.
I have just made a big ol list of rules inspired by Laravel's Validation rules and will edit my answer to include them oncce I have finished testing them.
EDIT
Here are all the rules I'm currently using in production. Hopefully they'll help someone else! You'll need to import them into your component to use them, or you can globally include them through a vue mixin.

Validator::validateNumber does not exist

Image 1
Image 2
Image 3
Why is post request not allowed?
Use numeric instead number
commisionRate => "required|numeric|between:0,0.99",
According to Laravel default validation rules which are here:
https://laravel.com/docs/5.6/validation#available-validation-rules
there are no rule for "number"
according to your third screenshot you are using number validation rules just remove it.
commisionRate => "required|number|min:0.01|max:1
to this
commisionRate => "required|min:0.01|max:1
check your all fiels and do this change. To check number laravel provide you "numeric" validation rules

In CakePHP validation, do I need a notEmpty rule if I have another rule like alphaNumeric and allowEmpty is false?

If I have a validation rule such as
alphaNumeric' => array(
'rule' => array('alphaNumeric'),
'allowEmpty' => false),
Is there any need to have a notEmpty rule? As I understand it, the allowEmpty being set to false will consider empty values a violation of the alphaNumeric rule, so other than if I wanted to define two different error messages, is there any need for a notEmpty rule?
(Another way to ask this question: is there some separate functionality that a standalone notEmpty rule would provide or be necessary for, other than to give a separate custom message, that I'm not seeing?)
To be perfectly clear: I understand that idea that notEmpty is a standalone rule, where allowEmpty is an attribute of a rule. That's not my question. My question is, is there any need or value to adding a notEmpty rule (other than the custom message that it would allow you to have for that rule), if you already have an alphaNumeric (or some other similar) rule you can just add allowEmpty = false to? Is there any difference in what the rule vs the attribute does, other than the rule being stand alone?
It really depends on the "other" rule that you're using.
You can see exactly what each rule is ACTUALLY checking for in the CakePHP Validation utility:
https://github.com/cakephp/cakephp/blob/44b7d013ae304a05699179bb4ea0077956c57e10/lib/Cake/Utility/Validation.php
For instance, in that file you can see the alphanumiric check:
public static function alphaNumeric($check) {
if (is_array($check)) {
extract(self::_defaults($check));
}
if (empty($check) && $check != '0') {
return false;
}
return self::_check($check, '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/Du');
}
In the case of alphanumeric, you can see that it has an empty check already, so you shouldn't also need the allowEmpty=>false rule.
Lastly, to your point, the only benefit I see in adding it as a separate rule is that you can give a better error message to the user.
Please read... http://book.cakephp.org/2.0/en/models/model-attributes.html
Model attributes allow you to set properties that can override the default model behavior and rules in your context is the business logic of your application.
The answer of your question lies in the below link:
http://book.cakephp.org/2.0/en/models/data-validation.html#allowempty
Actually you are absolutely correct, if you there is field in which you have to apply more than one validation, eventually one is nonEmpty in that case you can simply use allowEmpty=>false.
But if your datafield requires only one validation for non empty check in that case you should use nonEmpty for better understanding of your code!
I guess I had made my point...thanks

integer validation for text fields in yii

I had a text field in my form like
$form->textField($model,'test',array('required'=>'true'))
The validation works for required field. Is any way to give validation for an integer like this . I tried Like
$form->textField($model,'test',array('required'=>'true','integer'=>'true'));
But it doesn't work. Is any other way to do it
Thanks in advance
in your model under rules() function add the rule like below
array('test', 'numerical', 'integerOnly'=>true),
This has to be done in the rules. Your code as provided was:
$model = "someString";
$form->textField($model,'test',array('required'=>'true'));
This code itslef will not cause the field to be required, all it will do is add a required attribute with a value of true to the generated HTML, e.g.:
<input name='someString' required='true'>test</input>
That's not going to cause validation to run. Validation will run because of rules defined in the model, and can not be added during the view stage.
We can also use like this -
array('number_value', 'match', 'pattern'=>'/^[0-9]+$/', 'message'=>"{attribute} no. Invalid."),

Resources