To add a new validation to Laravel I have done this:
made a new file called customValidation.php in app/start/
and then included it in app/start/global.php and tested it with an echo() and it worked. So it is loaded into the application now.
Then I wrote the following code to validate checkboxes in Laravel:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class customValidate extends Illuminate\Validation\Validator
{
public function validateCheckbox($attribute, $value, $parameters)
{
//if(isset($value)) { return true; } else { return true; }
echo "this is the: " . $value;
}
}
/**
* Resolvers for Custom Validations
*/
Validator::resolver(function($translator, $data, $rules, $message){
return new customValidate($translator, $data, $rules, $message);
});
However, in my validation rules, I define :
`array('sex'=>'checkbox')`
But it does not work. Nothing happens. No error is thrown. The application behaves as if it has not executed the function at all. Also when I echo something from within the function,nothing get echoed, another evidence for the function not being called at all.
I would create custom app/validators folder for this then.
1, Create app/validators/CustomValidate.php
<?php
class CustomValidate extends Illuminate\Validation\Validator
{
public function validateCheckbox($attribute, $value, $parameters)
{
echo "this is the: " . $value;
}
}
2, run php artisan optimize or composer dumpautoload
3, Somewhere register your custom validator. Perhaps add app/validators.php into start/global.php
Validator::resolver(function($translator, $data, $rules, $message){
return new CustomValidate($translator, $data, $rules, $message);
});
4, Validate
$rules = ['agreed' => 'checkbox'];
$data = ['agreed' => 0];
$v = Validator::make($data, $rules);
dd($v->passes());
When submitting the form, the radios were not set in their initial states, and this made Laravel not to look for the validator of the item. So, it doesn't have any workaround for that. What I did was I simply added an initial state to the radios and they worked.
Much Like said by Andreyco
Related
I have some custom routes
Route::get('/files/', [
'as' => 'read',
'uses' => 'Myname\MyPlugin\Http\Controllers\FilesController#read'
]);
Somewhere in my class I have a function to validate a path
private function getPath()
{
$path = Input::get('path');
if (!$path)
{
throw new MyException('parameter is missing. path required', 400);
}
return base_path().'/'.$path;
}
I have set a custom error handler with a JSOM but it's the error handler of OctoberCMS that render the error in HTML format.
Do you know a way to replace default error handler of OctoberCMS by a custom one ?
Thanks
Just found the anwser in the documentation : https://octobercms.com/docs/services/error-log#exception-handling
October provide App:error to manage Exception in your plugin.
App::error(function(MyException $exception) {
//do what you want here
}
Don't forger to create a custom Exception for your plugin. If you use the generic Exception, you'll catch all the Exceptions.
I am posting an alternative solution here for a custom error handler because of this questions's visibility on Google.
I had problems using App:error due to this issue on Github: https://github.com/octobercms/october/issues/3416
Rather than using October's App::error for a custom error handling, try the following:
Create a custom error handler that inherit's from October's error handler. For example, create the following class in plugins/{AUTHOR}/{PLUGIN}/classes/CustomHandler.php (assuming you are developing a plugin for OctoberCMS). Override the render method of the handler.
<?php
namespace {AUTHOR}\{PLUGIN}\Classes; //<-- CHANGE ME OBVIOUSLY FOR YOUR PLUGIN
use October\Rain\Foundation\Exception\Handler;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Response;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException; // As an example exception you want to handle ...
/* Custom error handler which replaces the default error handler for OctoberCMS. */
class CustomHandler extends Handler
{
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Exception $exception
* #return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
/* Custom JSON response for ModelNotFoundException exceptions. */
if($exception instanceof ModelNotFoundException){
return Response::json(['error' => 'A record was not found for the resource that was requested.'], 404);
}
/* The rest of this code is just the 'default' code from OctoberCMS' error handler. */
/* The only change is the code above this comment where I handle a specific exception in a unique way.*/
/* i.e. I decided to return JSON for the error rather than an HTML error page (in debug mode). */
if (!class_exists('Event')) {
return parent::render($request, $exception);
}
$statusCode = $this->getStatusCode($exception);
$response = $this->callCustomHandlers($exception);
if (!is_null($response)) {
return Response::make($response, $statusCode);
}
if ($event = Event::fire('exception.beforeRender', [$exception, $statusCode, $request], true)) {
return Response::make($event, $statusCode);
}
return parent::render($request, $exception);
}
}
Then, in your plugin registration file Plugin.php, add a boot method with the following code:
<?php namespace {AUTHOR}\{PLUGIN};
use System\Classes\PluginBase;
use {AUTHOR}\{PLUGIN}\Classes\CustomHandler; //<-- IMPORTANT
use Illuminate\Contracts\Debug\ExceptionHandler; //<-- IMPORTANT
class Plugin extends PluginBase
{
/**
* #var array Plugin dependencies
*/
public $require = [];
public function registerComponents()
{
}
public function registerSettings()
{
}
public function boot(){
/* Replace the default error handler of OctoberCMS to return JSON format responses instead. */
/* Also, this is used in order to control the responses for certain types of errors/exceptions. */
/* We are going about it this way because App::error (as mentioned in the documentation for OctoberCMS), */
/* was not returning the response properly presumably because of a bug. Argh... */
$this->app->bind(
ExceptionHandler::class,
CustomHandler::class
);
}
}
Since, the new CustomHandler.php was added to the classes directory, the new class should already be picked up. No need for composer dump-autoload -o or changes to the composer.json.
That is it. When the ModelNotFoundException occurs, the response given to the web browser will be JSON that you wrote (from the custom error handler we created).
Relevant links:
https://blog.sarav.co/registering-custom-exception-handler-laravel-5/
https://laravel.com/docs/5.8/container#binding
https://octobercms.com/forum/post/is-it-posible-to-bring-back-the-app-directory-for-native-laravel
I have the following
controller function
public function show()
{
$users_id = Request::segment(4);
// $this->user_bank_details_repository->setPresenter(new UserBankAccountPresenter);
$account = $this->user_bank_details_repository->findByField('users_id', $users_id, $columns = ['user_bank_details_id','bank_name','bank_account_number','bank_ifsc_code','beneficiary_name','bank_account_type','bank_branch','created_at']);
if(!$account)
{
return $this->response->noContent();
}
return $this->response->item($account, new UserBankAccountTransformer);
}
Transformer
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Api\V1\Transformers;
use App\Entities\UserBankDetails;
use League\Fractal\TransformerAbstract;
class UserBankAccountTransformer extends TransformerAbstract {
public function transform(UserBankDetails $bank_details)
{
return [
'id'=> (int) $bank_details->user_bank_details_id,
'bank_name' => $bank_details->bank_name,
'bank_account_number' => (int) $bank_details->bank_account_number,
'bank_account_type' => $bank_details->bank_account_type,
'bank_beneficiary_name' => $bank_details->beneficiary_name,
'bank_branch'=> $bank_details->bank_branch
];
}
}
I am using repository pattern design and using dingo for my REST framework.
I always get the response as
{
"user_bank_details": [
{
"user_bank_details_id": 1,
"bank_name": "jbjb",
"bank_account_number": "939393933939",
"bank_ifsc_code": "ABCD0000047",
"beneficiary_name": "Gaf",
"bank_account_type": "savings",
"bank_branch": "Mad(E)",
"created_at": "2015-12-23 17:05:39"
}
]
}
instead of the transformed json. I dont get any errors as well. Tried using
return $this->response->item($account, new UserBankAccountTransformer::class);
But still the response is not getting transformed. I tried whatever I could but I dont get it worked :(
Even though this question is over a year ago, however for the sake of anyone who would have similar issue, the suspect here is the fact that you used: item instead of collection.
If the $account contains a collection, then it means this answer is correct: that is, this:
return $this->response->item($account, new UserBankAccountTransformer::class);
Should be:
return $this->response->collection($account, new UserBankAccountTransformer::class);
PS: I also Encountered this on Laravel 5.2.* thats why I feel this might help.
I am currently working on a big application that uses monolog for logging and was asked to hide any sensitive information like passwords.
What I tried to do, was extending monolog so it would automatically replace sensitive information with asterics, but even though the data seems to be altered, in the end the original text gets logged.
use Monolog\Handler\AbstractProcessingHandler;
class FilterOutputHandler extends AbstractProcessingHandler
{
private $filteredFields = [];
public function __construct(array $filteredFields = [], $level = Monolog\Logger::DEBUG, $bubble = true)
{
$this->filteredFields = array_merge($filteredFields, $this->filteredFields);
parent::__construct($level, $bubble);
}
protected function write(array $record)
{
foreach($record['context'] as $key=>$value){
if(in_array($key, $this->filteredFields)){
$record['context'][$key] = '*****';
}
}
return $record;
}
}
And when I initialize my logger I do this:
$logger->pushHandler(new FilterOutputHandler(['username', 'password']));
$logger->debug('Sensitive data incoming', ['username'=> 'Oh noes!', 'password'=> 'You shouldn\'t be able to see me!']);
I also tried overridding the handle and processRecord methods of the AbstractProcessingHandler interface but in vain. Can this be done in monolog?
Looks like I was trying the wrong thing.
Instead of adding a new handler to my logger, I had to add a new processor by using the pushProcessor(callable) method.
So, in my specific use case, I can add filters to my context like this:
function AddLoggerFilteringFor(array $filters){
return function ($record) use($filters){
foreach($filters as $filter){
if(isset($record['context'][$filter])){
$record['context'][$filter] = '**HIDDEN FROM LOG**';
}
}
return $record;
};
}
And later I can add filters simply by
(init)
$logger->pushProcessor(AddLoggerFilteringFor(['username', 'password']));
...
(several function definition and business logic later)
$logger->debug('Some weird thing happened, better log it', ['username'=> 'Oh noes!', 'password'=> 'You shouldn\'t be able to see me!']);
I'm building a toy app in Lithium (PHP framework) based upon the Union of RAD's Framework project. It's all working great in the browser but when running integration tests, routes.php is not loaded, so the routing isn't working.
Here's the code I'm testing:
class StaffController extends \lithium\action\Controller {
public function add() {
$staff = Staff::create();
if (($this->request->data) && $staff->save($this->request->data)) {
return $this->redirect(array('Staff::view', 'args' => array($staff->id)));
}
return compact('staff');
}
My test:
public function testAdd() {
//Router::connect('/{:controller}/{:action}/{:args}');
$request = new Request();
$request->data = array('name' => 'Brand new user');
$controller = new StaffController(array('request' => $request));
/* #var $response \lithium\action\Response */
$response = $controller->add();
$this->assertEqual(302, $response->status['code']);
}
Notice the commented out line - Router::connect('/{:controller}/{:action}/{:args}'); - if I uncomment that, it's all good.
What I'm puzzled about is why, when running in unit tests, app/config/routes.php (where I define my routes) isn't loaded. From what I can determine, app/config/bootstrap/action.php adds a filter to the "run" method of the Dispatcher which loads routes.php.
Of course, it's possible that I am totally missing the point here! I'd appreciate any guidance you can give me!
Lithium has a lithium\action\Dispatcher used for http requests and a lithium\console\Dispatcher for console commands.
I'm assuming you are running tests from the command-line. I'm looking at the "framework" project's app/config/bootstrap/action.php file (here on github).
It is only including the routes.php file for the lithium\action\Dispatcher which is not loaded from the command-line. The app/config/bootstrap/console.php also doesn't include routes.php for the console.
My suggestion is to edit the console.php file and change the filter to look like this:
Dispatcher::applyFilter('run', function($self, $params, $chain) {
Environment::set($params['request']);
foreach (array_reverse(Libraries::get()) as $name => $config) {
if ($name === 'lithium') {
continue;
}
$file = "{$config['path']}/config/routes.php";
file_exists($file) ? call_user_func(function() use ($file) { include $file; }) : null;
}
return $chain->next($self, $params, $chain);
});
I have tried showing an error message in a Controller and it doesn't work, but when I use dd, it works.
My Code:
if ($validation->fails())
{
/*Doesn't work
foreach ($validation->fails() as $messages) {
$messages // Doesn't work
}
*/
dd($validation->errors); //This works
}
I noticed none of the provided examples here actually work! So here you go. This was my found solution after realizing that validator->messages() returns a protected object which isn't retrievable.
if ($validator->fails())
{
foreach ($validator->messages()->getMessages() as $field_name => $messages)
{
var_dump($messages); // messages are retrieved (publicly)
}
}
I would reference MessageBag, which is what messages() returns. And for additional acknowledgement of the Validator class - reference this.
$validation->fails() returns a boolean of whether or not the input passed validation. You can access the validation messages from $validation->messages() or pass them to the view where they will be bound to the $errors variable.
See the validator docs.
This is what I have just used in an artisan 5.0 console command, to validate the arguments. This is in the fire() method:
// Create the validator.
$validator = Validator::make(
$this->argument(),
['field1' => 'required|other|rules']
);
// Validate the arguments.
if ($validator->fails())
{
// Failed validation.
// Each failed field will have one or more messages.
foreach($validator->messages()->getMessages() as $field_name => $messages) {
// Go through each message for this field.
foreach($messages AS $message) {
$this->error($field_name . ': ' . $message);
}
}
// Indicate the command has failed.
return 1;
}
This is an extension on the answer from #tfont
You may need to change where the message is sent ($this->error()) if this command is not being run as an console command, ie CLI, command-line.