I have a form that would has the field that inputs 2 different values in a single field.
First field is made up with select option dropdown. Once you have selected an option from the first field, the value from the first field will print on the input field. Then in the input field you need to add or type it manually another value to make it work. So the output would look like this:
Selected value - Value added manually
So my problem is how do I validate the field if each one of them is either empty.
I have no idea what I'm doing but this is what I've tried so far but it's not working.
'field' => 'required|in:' . implode(",", Model::values()) . '|unique:table'
This should be the expected output:
If the field value is Selected value - Value added manually, it is Valid.
If the field value is Selected value -, it should validate to input the 2nd value.
If the field value is - Value added manually, it should validate to input the 1st value.
I have searched on google but I'm not sure on what keywords should I searched on but my title sums up on what I have searched recently but I haven't found same problem as mine but if there is, please link it down thanks.
I would suggest using two separate inputs: A select for "selected value" and an input for "Value added manually". This will simplify the rules:
'field1' => 'required|in:' . implode(",", Model::values()),
'field2' => 'required',
But since you currently have a unique validation rule I suspect that's the reason you have it as a single field. In that case, you can create some custom validation rules.
Validate first value exists:
return false !== strpos($value, '-') && strlen(trim(explode('-', $value)[0])) > 0;
Validate first value is valid:
return in_array($value, Model::values());
Validate second value exists:
return false !== strpos($value, '-') && strlen(trim(explode('-', $value)[1])) > 0;
You can then add those rules to the chain:
'field' => ['required', new FirstValueExists, new FirstValueValid, new SecondValueExists, 'unique:table'],
The reason I have two rules for the first value is to allow for more specific error messages ("Value 1 missing" and "Value 1 invalid"). If you don't care about that you can combine them into a single rule.
Note that I haven't actually tested this but it should work.
Related
I am trying to add validation for the min and max values. Both these values are coming from input fields.
I want to validate that max value (max_price) should always be greater than min value (min_price).
I am working on the Laravel 5.7
$validator = validator($request->all(),[
'min_price' => 'required|min:1"',
'max_price' => 'required|numeric|min:min_price',
]);
You can use the code below
$validator=validator($request->all(),[
'min_price'=>'required|min:1',
'max_price' => 'required|gt:min_price'
]);
References
https://laravel.com/docs/5.7/validation#rule-gt
https://laravel.com/docs/5.7/validation#rule-min
You can use the rule gt (Greather than) that expect the number of another field as first argument:
'min_price'=>'required|numeric|min:30',
'max_price'=>'required|numeric|gt:min_price'
I'm trying to validate a request to load stock into a table. Up until now stock has always had a positive value and the following validation rule worked exactly as expected:
[
"value" => "required|integer|min:0"
]
Stock is stored and can have multiple values and now stock can have a value of zero (0), I don't think it works with the 'required' rule.
I have changed it to use 'present' which I thought should suffice however it still fails, and adding 'nullable' also doesn't work:
[
"value" => "present|integer|min:0"
]
Are there validation rules to specify that a field must be present but the value can be zero?
Your initial validation rule just keeps working as desired; required doesn't throw an error on 0:
[
"value" => "required|integer|min:0"
]
From the Laravel documentation:
The field under validation must be present in the input data and not
empty. A field is considered "empty" if one of the following
conditions are true:
The value is null.
The value is an empty string.
The value is an empty array or empty Countable object.
The value is an uploaded file with no path.
So the issue was actually with my use of $request->intersect(...) in that it treats keys with a value of zero (0) as false and therefore removes them from the request data array.
For anyone else who may encounter this issue, here is the solution to treat zero (0) values as truthy while; null values, empty strings, and false will be treated as false.
Nb. $params, $rules, and $messages are arrays. See https://laravel.com/docs/5.4/validation#manually-creating-validators for more information.
return \Validator::make(array_filter($request->only($params), function($param) {
// This is needed to strip out empty values but treat zero (0) as truthy (default array_filter behaviour is
// to treat zero (0) as false) but we want these values to be present in the validated request data array as
// zero (0) in the context of a denomination is valid now that we will hold unactivated stock in the Vault.
return ($param !== null && $param !== false && $param !== '');
}), $rules, $messages);
I am reading input from the user (in Laravel) and have rules associated to each of those inputs. If the user selects a checkbox (for that specific table), I would like to remove all the rules from the $rules associative array for that table.
To better illustrate, I have created two arrays mimicking the behaviour.
My input array is as follows:
$input = array("TB1_course" => ['0'=>'CHEM 1E03', '1'=>'ENG 1D04'
],
"TB1_section" => ['0'=>'CHEM 1E03', '1'=>'ENG 1D04'
],
"TB1_checkbox" => "1",
"TB2_course" => ['0'=>'CHEM 1E03', '1'=>'ENG 1D04'
],
"TB2_checkbox" => "0"
);
$rules= array(
'TB1_course.*' => 'required_with',
'TB1_section.*' =>'required_with',
'TB2_course.*' =>'required_with'
);
You can see from the input array that TB1_checkbox has a value of 1. If it has a value of 1, I would like to remove all the rules associated with TB1 (i.e. remove the elements in $rules with a key containing 'TB1').
I attempted to do as such, and was partially successful. My code looks like:
foreach ($input as $key=>$value){//go thru every element of the inputs
if ((strpos($key, 'checkbox')!==false) && $value==1){//if it contains 'checkbox'
//and checkbox is selected
$table_name= substr($key, 0,3);//name of table
//now go thru $rules and remove all elements which contain a word of $table_name
$rules=array_filter($rules, function($x){
return strpos($x, $table_name)!==0;//problem lies here
}, ARRAY_FILTER_USE_KEY);
}
}
However my code isn't working. The strpos() function is unable to read the $table_name variable, and leads to problems. If I manually pass in a string, it ends up working (i.e. 'TB1' instead of $table_name), but I have to have the flexibility of checking all my tables (so the rules containing 'TB2' have to be removed if "TB2_checkbox" has a value of 1). Is there any way to solve this issue?
Thank you for the help.
I can print the hidden posted value properly but when i submit to database value is shown as zero in database table .im posting a integer value and int is the datatype given in database.
$array = array(
"unit_id"=>$this->input->post("unit_id"),
"head_id"=>$this->input->post("unit_head_id"),
);
$this->db->insert('request', $array);
Its not issue of hidden fields
in controller you have to match correct key value for the fields, please cross verify it
$array = array(
"unit_id"=>$this->input->post("unit_id"),
"head_id"=>$this->input->post("unit_head_id"),
);
$this->db->insert('request', $array);
I just discorvered that Form::model binding existed and I'm delighted with (it's wonderful). I tried it with text, email, and even select, every single time it worked.
My question is, will it work with a <select multiple>? If it does, how am I supposed to use it and what is the correct way to save an array in the database? (This may be awful but I concatenate all the options of the array with a separator and save it as text, I'm sure it's not the correct way to do it).
just like this :
Form::select('menus[]', $menus, null, array(
'multiple' => true,
'class' => 'form-control'
));
take a note :
param 1 : should be your field name (if want multiple add array tag aftrer field name e.g :menus[])
param 2 : list menu (array) e.g : array('value1' => 'text1', 'value2' => 'text2')
param 3 : selected values. (should be null because Form::model will perform automatically matching values from database. and make sure the field name has same with key data result from database)
param 4 : is property for element <select> you can add class, id, and etc..