laravel 4 validation after:date - get date from database - php

I want to validate a date field in my form and I also have a date field in a table and I want to check if the date in the form is after the date in the table.
Can I do this with the Validation function after:date?
I think with this function I need to define the date in the model (where I got the validation rules).
Or do I need to check it with a custom validation rule?
Thanks

You should be able to use the after:date validation if you retrieve the date to be checked against from the database table first, eg.
$date_from_table = call to retrieve date from table;
$validator = Validator::make(
array('date' => $date_from_form),
array('date' => 'after:'.$date_from_table)
);
Which as you note could go in the model if that is where you are defining the validation rules - in which case it would probably be slightly different to the above as you may not be making the validator instance in the model, just specifying the validation rules, eg.
protected $rules = array(
'date' => 'after:'.$this->date_from_table
);
If this works it probably isn't worth the bother of creating a custom validation rule unless you are going to be doing the same sort of thing in a number of different models.

Related

Validation in Laravel on relationship

In Laravel, I have a persons model that has a many-to-many relationship with its group. The person's name needs to be unique in its group but not on the persons table. How would ensure that?
My validation is done in the controller store method using $request->validate(['name => ...
I currently save the new person in a controller using simply - Person::create([...
My simple approach is using a composite primary keys on pivot table and use basic exception handling like try catch stuff whenever inserting data is fail due to migration
$table->foreignId('group_id') // Add any modifier to this column
$table->foreignId('person_id') // Add any modifier to this column
$table->primary(['group_id', 'person_id']);
If you want to do it on controller, make sure to setup relationship. Then just use Rule::notIn() Validation
'name' => [
'required',
Rule::notIn(/* put your logic here */),
],
You can use 'exist' rule in Laravel Validation like that:
'name' => 'exists:group,name,person_id,'.$id
For more info you can check here:
https://laravel.com/docs/9.x/validation#rule-unique

Laravel Request validation rename field

I use pickadate.js and have a problem with the laravel validation. If I send the form with the datepicker to my application, then there are two fields. birthday and birthday_submit. The last is created by pickadate.js and contains the date in the following format: yyyy/mm/dd.
My model has the column birthday. And my validation looks the following:
$request->validate([
"first_name"=>"required_without:last_name",
"email"=>"nullable|email",
"birthday_submit"=>"nullable|date"
]);
If I pass it to the create() function (Eloquent) then I get the error, that birthday_submit doesn't exist.
Is there a way to rename birthday_submit to birthday so I can mass assign it? Also the error messages would be better because the user shouldn't get the error message that birthday_submit is invalid.
You can 'interfere' with the $request parameter by doing something like this before validation:
$request->merge(['birthday' => $request->birthday_submit]);
Basically you insert another field inside the $request with key birthday and the value of birthday_submit
Then you can use this field in the validation.

Laravel 5.2 Unique Validation always invalidate on UPDATE

I have been trying to figure this one out for quite a whiale and have gone through every post from here and Laracast to figure this out but in vein.
I have done this before and it worked but I am not quite sure why it doesn't now.
Basically setting up a rule in your form request should follow the format below:
<?php
class MyFormRequest extends Request{
public function rules(){
return [
'field' => 'required|unique:table_name:field,' . $this->input('field');
];
}
}
This to me should work but according to my experience at the moment doesn't. I have tried to separate the rules by checking the incoming request method and assign rules based on whether the request is an update or create.
Shouldn't what I have suffice for this requirement? What would be the best way of re-using my FormRequest but making sure that I am able to validate uniqueness of as many table fields as I want regardless of their data types because I am sensing that perhaps this is related to the fact that Laravel seems to be doing an aggregate on the field being validated and if it is not an integer for example it will keep on displaying the error message.
Another possible way of solving this is if I implement my own validation method for the fields I am interested to validate. How would I go about doing this?
This rule works only if the primary key of "table_name" is "id" but if it is different then you have to write it as the fourth parameter to unique rule as follows:
<?php
class MyFormRequest extends Request{
public function rules(){
return [
'field' => 'required|unique:table_name:field,' . $this->input('field').','.'primary_key_field';
];
}
}
hope it help you !!!
You need to tell the validator which rows to exclude from duplicate search.
If your primary key is id, you need to pass its value:
'field' => 'required|unique:table_name,field,' . $this->input('id')
Your code has also some other errors:
'field' => 'required|unique:table_name:field,' . $this->input('field');
^ ^
The rule "unique" needs aditional arguments when updating. Those aditional arguments will be responsible for ignore the actual registry you're working on. If not applied, this rule will always invalidate, since the registry always will be found on database.
Your thirdy argument should be the ID you want to ignore. (you're passing a value...)
*Another thing: I picked up a misstype from your rule. Check below the right sintax with new arguments.
'field' => 'required|unique:table_name,field,'.$this->input('id')'
PS: if your id column from your table has a different name than "id", you need to specify this column name as the fourth parameter.
'field' => 'required|unique:table_name,field,'.$this->input('id').',column_name'

Laravel validate model

I've created an Employer model in my Laravel 4 application and, in Employer.php I have created the following function to validate user input before saving it to the database:
public static function validate($input)
{
$validator = Validator::make($input, static::$rules);
if ($validator->fails() {
return $validator;
}
return true;
}
This works fine when I'm creating a new record in the database, because I am passing in values for all the rules where I have specified a particular field is required.
However, there are certain fields in the database I don't want the user to edit after they have been created (for example, business_name). On the controller's edit method I create a form and omit those fields from the form. But validation fails because business_name is required by the $rules.
As a temporary work around, I tried just creating a hidden field in the edit form and populating it with the business_name. However, this is also required to be unique and fails when I PATCH my form to the update method!
Any advice? Is there any way I can specify which validation rules should be applied depending on the method calling it? Or should I create a new method in Employer.php specifically to validate on the update method?
You could use the required_without validation rule. Since an newly instantiated model doesn't have an id field yet, you can require some fields only when id is not present. This should work:
public static $rules = array(
'business_name' => 'required_without:id'
);
http://laravel.com/docs/validation#rule-required-without

Yii validation rule - unique

in Yii framework, can I use unique validation rule to check uniqueness of an field within some condition (I know there is criteria, but this condition is a bit tricky)? Ie, i want to check num_days unique by property_id.
table:
NUM PROP_ID
3 4
3 5
validation should pass in case i try insert 3, 6, but fail in case of 3, 4
Check out UniqueAttributesValidator, and also this answer. In the links you'll see that they have used $this->attributename for the params array of the criteria option of CUniqueValidator, but for some reason $this->attributename was null for me. I believe that this is because the $this is not being passed correctly to the validator, so anyway it would be best to use the UniqueAttributesValidator class, because it has been made just for these kinds of situations.
The resulting sql will have a WHERE clause like this:
SELECT ... WHERE (`num`=:value) AND (`prop_id`=:prop_id) ...
which will easily fail for 3, 4 and pass for 3, 6. So it should work for your situation.
First Create unique field in you table
and in model add this to your rules()
array('field_name', 'unique'),
For combination of two fields unique use this code
public function rules() {
return array(
array('firstKey', 'unique', 'criteria'=>array(
'condition'=>'`secondKey`=:secondKey',
'params'=>array(
':secondKey'=>$this->secondKey
)
)),
);
}
Create your custom Validator or validation function, it's simple.

Categories