I want to store the score value as a number when is_pass is true. If is_pass is false then score should be null. So I added validation like below for score
'score' => 'required_if:is_pass,true|numeric'
But I have an issue when is_pass is false then also it's expected score value as numeric. How to solve this issue? Is there any diffrent option is there other than custom validation? I am using laravel 7.
You should define a variable to keep you rules.
Then base on condition change your rules.
if((bool) $request->is_pass === true)
$rules['score'] = 'required_if:is_pass,true|numeric';
else
$rules['score'] = 'required_if:is_pass,false|null';
You can use nullable validation with numeric here and here
'score' => 'nullable|numeric'
what this will do is check if the value is not null then apply numeric validation otherwise skip validation and accept it as null.
i have a field in Laravel with the name "company_url". and its stored like this.
$post['company_url'] = "http://example.org";
and then i have a rule string which i have stored for validation which must be applied on this individual field. which is stored like this
$post['rule'] = "required|max:24";
now i am trying this code to get validation errors. which is not working.
$validator = Validator::make([$post['name']], [$post['rules']]);
tell me what is the way to get errors on this validation?
The data you're passing to make() is in incorrect format. It should be in key-value pair format.
Also I don't know where the $post['name'] coming from. I assume it is company_url not name.
$post['rules'] is also undefined. It should be $post['rule']
The following should work:
$validator = Validator::make(['company_url' => $post['company_url']], ['company_url' => $post['rule']]);
I have an input with an array of entities with an ID which must be unique
I've tried this:
'authors.*.id' => 'different:authors.*.id'
But it says 'The authors.0.id and authors.0.id must be different'
So what is a right way to validate this?
You want to use distinct rule.
When working with arrays, the field under validation must not have any duplicate values.
'foo.*.id' => 'distinct'
Asking about codeigniter 3 form validation with callback.
Form validation - thats validating an array of data - in this case a group of products - and one rule has a callback. Like:
$this->form_validation->set_rules('productid[]', 'Product ID', 'required');
$this->form_validation->set_rules('productsku[]', 'Product SKU', 'callback_sku_check');
and then the callback goes to the method: sku_check($str)
All that works fine, churns through and checks an array of product skus no problem. Now is there a way to pass the correct Product ID from the array to the callback method? So i could have
sku_check($str,$productid)
where the product id matches the specific sku in the array?
I am trying to do a Laravel validation rules as follow:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
The explanation to the rules is:
I have a table "Posts" in my system with the following fields (among others): hotel_id, permalink, deleted_at. If MySQL would allow make an unique index with null values, the sql would be:
ALTER TABLE `posts`
ADD UNIQUE `unique_index`(`hotel_id`, `permalink`, `deleted_at`);
So: I just add a new row IF: the combination of hotel_id, permalink and deleted_atfield (witch must be NULL) are unique.
If there is already a row where the permalink and hotel_id field are the same and 'deleted_at' field is NULL, the validation would return FALSE and the row wouldnt be inserted in the database.
Well. I don't know why, but the query Laravel is building looks like:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `hotel_id` = the-permalink-value AND `NULL` <> deleted_at)
What the heck...
The query I was hoping Laravel build to validation is:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `permalink` = 'the-permalink-value' AND `hotel_id` = ? AND `deleted_at` IS NULL
Could someone explain me how this effectively works? Because everywhere I look it looks like this:
$rules = array(
'field_to_validate' =>
'unique:table_name,field,anotherField,aFieldDifferentThanNull,NULL',
);
Does anyone could help me?
Thank you
all.
Finally, I got a proper understanding of the validation (at least, I think so), and I have a solution that, if it is not beautiful, it can helps someone.
My problem, as I said before, was validate if a certain column (permalink) is unique ONLY IF other columns values had some specific values. The problem is the way Laravel validation string rules works. Lets get to it:
First I wrote this:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
And it was generating bad queries. Now look at this:
'column_to_validate' => 'unique:table_name,column_to_validate,id_to_ignore,other_column,value,other_column_2,value_2,other_column_N,value_N',
So. The unique string has 3 parameters at first:
1) The table name of the validation
2) The name of the column to validate the unique value
3) The ID of the column you want to avoid (in case you are editing a row, not creating a new one).
After this point, all you have to do is put the other columns in sequence like "key,value" to use in your unique rule.
Oh, easy, an? Not so quickly, paw. If you're using a STATIC array, how the heck you will get your "currently" ID to avoid? Because $rules array in Laravel Model is a static array. So, I had to came up with this:
public static function getPermalinkValidationStr() {
$all = Input::all();
# If you are just building the frozenNode page, just a simple validation string to the permalink field:
if(!array_key_exists('hotel', $all)) {
return 'required|alpha_dash|max:255';
}
/* Now the game got real: are you saving a new record or editing a field?
If it is new, use 'NULL', otherwise, use the current id to edit a row.
*/
$hasId = isset($all['id']) ? $all['id'] : 'NULL';
# Also, check if the new record with the same permalink belongs to the same hotel and the 'deleted_at' field is NULL:
$result = 'required|alpha_dash|max:255|unique:posts,permalink,' . $hasId . ',id,hotel_id,' . $all['hotel'] . ',deleted_at,NULL';
return $result;
}
And, in the FrozenNode rules configuration:
'rules' => array(
'hotel_id' => 'required',
'permalink' => Post::getPermalinkValidationStr()
),
Well. I dont know if there is a easiest way of doing this (or a much better approach). If you know something wrong on this solution, please, make a comment, I will be glad to hear a better solution. I already tried Ardent and Observer but I had some problems with FrozenNode Administrator.
Thank you.