I need to validate that a customer is using a valid subdomain name as well as making sure some other required fields are present. I have created the following rule in my model class:
public function rules()
{
return array(
array(',c_customerSubdomain, c_custAdminUser,c_adminPassword,c_adminContact', 'required', 'message' => "{attribute} is TEST required."),
array('c_customerSubdomain', 'match', 'pattern' => '/^[a-z0-9_]*$/', 'message' => "Customer Subdomain should contain only alphanumeric and underscore.", "allowEmpty" => false),
);
}
I am testing whether or not the rule is firing by changing the message for required values to: {attribute} is TEST required.
When I submit the form with all of the field blank, the result is as expected.
However, when I populate the field for subdomain with an illegal value, such as "ASD# a", I am expecting to get a validation error. However, instead, the rules bypass all of the other validation errors and attempt to save the model.
Related
I have the following validation code in a controller. The problem is that laravel is sending the column name in the validation error , which is most of the time abbreviations. In this case the column name is "ans". Is there a way to show a different name to the user ?
Validation :
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
]);
Error :
The ans field is required.
If you want to customize the attribute name you can do that; you don't have to customize the message to do this. The validate method takes an array of "custom attribute names" to use (4th argument), just like it takes an array of custom messages (3rd argument).
$this->validate(
$request,
['ans' => 'required|max:5000|min:10'], // rules
[], // custom messages
['ans' => 'Answer'] // custom attribute names
);
You should always have the option to specify custom messages and attributes with the validation methods that are available. These custom attribute names get replaced in the messages where :attribute is used.
If you don't do it this way you would have to create 3 custom messages, one for each rule you have defined to use for your field 'ans', since any of those rules could fail and will include the attribute name.
Yes, you can achieve with the following code.
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], [
'ans.required' => 'The answer field is required.'
]);
Take a look at the laravel documentation to know more.
You can send custom validation message,define all
your validation message in a variable like this
$messages = [
'ans.required' => 'The answer field is required.'
]
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], $messages);
Thanks
According to Laravel Documentation, bail rule should stop running validation rules after first validation failure.
I have a file outside App\Http\* namespace with the following code in it:
if(Validator::make($params, [
'email' => 'bail|required|email|max:60|exists:customers,email',
'password' => 'required|max:60|string',
'password' => new CustomerCheckPassword($params['email']),
]) -> fails())
throw new Exception('message here');
Works like a charm, except bail rule from email attribute does not stop the validation when $params['email'] is not contained by customers.email, and goes on to password as well. Do you have any ideas or elegant workarounds for this issue?
In a different section of the same docs page (it won't link directly to it), it explains that bail only applies to the current attribute. So for instance, if email is missing, it will not check anything after required, but password is a new attribute so it will validate it as normal.
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
One way to accomplish this would be to use the sometimes method
Validator::make($params, [
'email' => 'bail|required|email|max:60|exists:customers,email',
'password' => 'required|max:60|string',
])->sometimes('password', new CustomerCheckPassword($params['email']), function ($input) {
// Check input to decide if rule should execute
});
Im updating a category name, but i need to be unique and also case is the current record let it update, but it is not working my validation.
Example:
$this->validate($request, array(
'category' => 'required|unique:categories,name,:id|min:2'
));
From unique() rule description:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
In my Laravel 5.2 app, I have the following validation rules to check a field:
$rules = array(
'file' => 'mimes:jpg,png,pdf,doc,docx',
'file' => 'validate_file'
);
Now, the validate_file is a custom validation rule. I want to run it only only if the first mimes validation passes.
I know I could simply validate/redirect with errors - in two batches, first the mimes, then the validate_file, but it seems like an overkill.
Any way I can do it the smart way in Laravel 5.2?
Use "bail" attribute. It will stop validation after first failure.
$this->validate($request, [
'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);
https://laravel.com/docs/5.2/validation#validation-quickstart search for "bail"
I'm using cake 2.3.8 version. I have a registration form where users can enter in a username and password in the form.
My model validation looks like:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
),
'alphanumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
) );
Now the weird thing is in my site when I enter in a username with space in it, the validation is displayed twice. But when I use the Family registration form and enter a username with space in it, the validation error only displays once. Does anyone know what could be the issue?
Generally this is because validation is triggered twice. What exactly causes it to be triggered twice is pretty hard to tell without seeing more code (especially involved controllers, components, behaviours and models).
Check whether you are maybe calling Model::validates() manually, additionally to validation that is triggered by the models save operation, or maybe you are even calling it twice manually.
It could also be triggered by a third party component or a behaviour or whatever... you'll need to do some debugging.
In my case it happened because in the controller:
I did a $this->Model->save() which returned false (first
validation)
Then, to show validation error I did
$this->Model->invalidFields() which validates again the fields
(second time) and returned the message
To fix it I changed $this->Model->invalidFields() to $this->Model->validationErrors to get the error message
I also got this issue once. But, in my case I found out that, though I had written validate code in the Model, I validated it again in the controller because of which it was showing double validation errors. If you have done the same, then remove
$this->Model->validates()
from controller.