laravel validation check field equal something - validation

how to check a field is exactly equal to a string or number
I want to check a field named course_id is equal a field of database id in course database. now I want to check if course_id is equal to id.

You can validate it by using the exists validation rule:
$validationRules = ['course_id' => 'exists:course,id'];

create rule as below and use validator on input
$rules = array(
'id' => 'exists:your_table_name'
);
for more help
https://laravel.com/docs/5.2/validation#rule-exists

Related

Laravel Validation of Array

Laravel Help with validation
Hi everyone I want to validate an array of a foreign key to make sure it exists I am doing
'activities' => 'required|array',
'activities.*' => 'exists:admin_activities,id',
but it's skipping this validation
any solution for this
You can validate each element of an array in Laravel with "dot notation" to For example, to validate that each id in a given array input field is exists in admin_activities , you may do the following:
'activities.*.id' => 'required|exists:admin_activities,id',

Laravel updateOrCreate with where clause

How to implement updateOrCreate function with where clause.
For example. I would want to update a field only if certain column contains a specific value otherwise don't update.
Is it possible with updateOrCreate function?
updateOrCreate is an update with where-clause:
$user = User::updateOrCreate(
[
'id' => $userId,
'type' => $userType,
],
[
'name' => $userName,
'email' => $userEmail,
]
);
This query will update the user with $userName and $userEmail if a user exists with matching $userId and $userType. If no matching user is found, a new user is created with $userId, $userType, $userName and $userEmail.
So the first array is the "where-clause" that has to match for the update, second array is the values that should be updated if a match is found and a merge of both arrays are used when creating a new user if no match was found.
See docs here
updateOrCreate mainly used when you upadate one column value in your model.You don't know the condition either this row is exists or not.If the row is exists then it just upadte your column value otherwise it create that row.
$update_value = YourModel::updateOrCreate(
['your_checking_column' => 'your_value',],
['your_updated_coumn' => 'your_updated_value']
);
updateOrCreate doesn't provide this functionality as far as I know. Instead you can use regular where clause followed by update. From your question I see that you don't need to create at all.
Something::where('column', 'value')->first()->update(['otherColumn' => 'some value']);
If you're trying to set a value based on some criteria you can use a ternary:
'user_id' => ($request->user_id && is_numeric($request->user_id) ? $request->user_id : \Auth::user()->id)
This is the equivalent of saying, if user_id is provided and numeric, set the value to $request->user_id, otherwise take the user_id from the authenticated user, a simpler example:
'user_id' => ($request->user_id ? $request->user_id : null)
If a user_id is give in the request use that value, otherwise set value to null.

Change default column name 'id' when validating

I am trying to validate an email field so that I make sure it's unique, but not in a row with a certain id, which I do like so:
'email' => 'required|email|unique:seller_user,email,'.$seller_id,
That works, but it automatically searches for a column named id, whereas in my table that column is actually called seller_id, so how can I change that?
You can specify the field to search using the fourth parameter to the rule:
'email' => 'required|email|unique:seller_user,email,'.$seller_id.',seller_id',

Laravel validation: unique with multiple columns and soft_delete

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.

How do I validate non-required fields in CodeIgniter?

I'm building an API in CodeIgniter, and I'm having trouble with the validation for an UPDATE method. My table has 3 columns (plus id):
first_name
last_name
city
I'd like the client to be able to update any of the 3 columns, and apply validation rules (such as min_length[1]) to each one the client wants to update.
The issue is that if the client POSTs this:
first_name=Foo&last_name=
It skips validating last_name since CodeIgniter sees it as "empty" and it's not "required" by my validation. But then when active record attempts to UPDATE the record in MySQL, it sets last_name to "" (empty).
I don't want the fields to be required, but if they are set, they should have a minimum length.
Any ideas?
Try this i am giving you an example for your scenario the fields which are not mandatory and for first time they has values that are stored in db and for second time user just sent blank any of the mandatory fields just unset that index in your to store in db so the previous value will remain in DB
$first_name=$this->input->post("first_name");
$last_name=$this->input->post("last_name");
$city=$this->input->post("city");
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'city' => $city
);
if(empty($last_name)){
unset($data['last_name'])
}
if(empty($city)){
unset($data['city'])
}
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Hope it makes sense

Resources