Asked  7 Months ago    Answers:  5   Viewed   40 times

I use following date validation for incoming POST request.

'trep_txn_date' => 'date_format:"Y-m-d H:i:s.u"'

This will only allow a date of this kind i.e. 2012-01-21 15:59:44.8

I also want to allow date without the TIME e.g. 2012-01-21, which when sent to mysql db will automatically store as 2012-01-21 00:00:00.0

Is there a way I can do this using a Laravel's existing validation rules. Is there a way to define multiple formats in date_format rule something like below.

'trep_txn_date' => 'date_format:"Y-m-d H:i:s.u","Y-m-d"' //btw this didn't work.

Thanks,

K

 Answers

63

The date_format validator takes only one date format as parameter. In order to be able to use multiple formats, you'll need to build a custom validation rule. Luckily, it's pretty simple.

You can define the multi-format date validation in your AppServiceProvider with the following code:

class AppServiceProvider extends ServiceProvider  
{
  public function boot()
  {
    Validator::extend('date_multi_format', function($attribute, $value, $formats) {
      // iterate through all formats
      foreach($formats as $format) {

        // parse date with current format
        $parsed = date_parse_from_format($format, $value);

        // if value matches given format return true=validation succeeded 
        if ($parsed['error_count'] === 0 && $parsed['warning_count'] === 0) {
          return true;
        }
      }

      // value did not match any of the provided formats, so return false=validation failed
      return false;
    });
  }
}

You can later use this new validation rule like that:

'trep_txn_date' => 'date_multi_format:"Y-m-d H:i:s.u","Y-m-d"' 

You can read more about how to create custom validation rules here: http://laravel.com/docs/5.1/validation#custom-validation-rules

Wednesday, March 31, 2021
 
wavyGravy
answered 7 Months ago
62

I guess as per laravel upgrade guide we should return HttpResponseException

protected function failedValidation(Validator $validator)
{
    $errors = $validator->errors();
        $response = new ResponseObject();

        $response->code = ResponseObject::BAD_REQUEST;
        $response->status = ResponseObject::FAILED;
        foreach ($errors as $item) {
            array_push($response->messages, $item);
        }

    throw new HttpResponseException(response()->json($response));
}
Wednesday, March 31, 2021
 
Zeth
answered 7 Months ago
43

Your rule performs two checks that are independent of one another; just because the external_id field is not required when the type_id != 3, does not mean the integer check is ignored.

What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :

$validator = Validator::make($data, [
    'type_id'   => 'required|integer'
]);

$validator->sometimes('external_id', 'required|integer', function($input) {
    return $input->type_id == 3;
});

When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance() method:

class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer'
                ];
        }

        protected function getValidatorInstance() {
            $validator = parent::getValidatorInstance();
            $validator->sometimes('external_id', 'required|integer', function($input) {
                return $input->type_id == 3;
            });
            return $validator;
        }
}
Saturday, May 29, 2021
 
nfechner
answered 5 Months ago
85

After re-reading the docs, it is clear that this is intended behaviour. The docs say

As an alternative to mocking, you may use the Event facade's fake method to prevent all event listeners from executing.

As described in a comment, I have to create two tests. One for making sure that the event has been fired and contains the expected data and a second one for making sure that the event listeners get fired.

So, first Test:

Event::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Event::assertDispatched(PaymentWasCompleted::class, function ($e) use ($order) {
    return $e->order->id === $order->id;
});

Second test:

Queue::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Queue::assertPushed(GenerateInvoiceJob::class, function ($job) use ($order) {
    return $job->order->id === $order->id;
});

As expected, this tests pass.

Saturday, May 29, 2021
 
Amber
answered 5 Months ago
93

You can chain your wheres directly, without function(q). There's also a nice date handling package in laravel, called Carbon. So you could do something like:

$projects = Project::where('recur_at', '>', Carbon::now())
    ->where('recur_at', '<', Carbon::now()->addWeek())
    ->where('status', '<', 5)
    ->where('recur_cancelled', '=', 0)
    ->get();

Just make sure you require Carbon in composer and you're using Carbon namespace (use CarbonCarbon;) and it should work.

EDIT: As Joel said, you could do:

$projects = Project::whereBetween('recur_at', array(Carbon::now(), Carbon::now()->addWeek()))
    ->where('status', '<', 5)
    ->where('recur_cancelled', '=', 0)
    ->get();
Thursday, June 17, 2021
 
tedders
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :