getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
631 stars 94 forks source link

Data validation #86

Open sulaymankhan opened 8 years ago

sulaymankhan commented 8 years ago

It will be really helpful if you include data validation example in Saving Data section.

PrafullaKumarSahu commented 8 years ago

Here I am including my case you can describe your problem better to solve your problem.

In my plugin page, I am creating a panel

$panel->add([
    'type' => 'panel',
    'as'   => 'mainPanel',
    'title' => 'Analytica',
    'rename' => 'General',
    'slug' => 'plugin-admin-settings',
    'icon' => 'dashicons-chart-area',
    'uses' => __NAMESPACE__ . '\Controllers\AdminController@index',
    'post' => __NAMESPACE__ . '\Controllers\AdminController@save',
]);

now when my form will be submitted like

<form id="poststuffs" action="{{ panel_url('Analytica::mainPanel') }}" method="post">

the data will be received by save method

class AdminController {

public function save(Http $http)
{   
        if($http->only($this->option)){
            $inputs = $http->only($this->option);
        }

        if( ! $this->validate($inputs) || ! $this->check($inputs) )
        {
                }
   }

public function validate($inputs){
      //validate your data
}

public function check($inputs){
    //sanitize your data
}
extrakun commented 8 years ago

If you want, you can include Illuminate\Validation.

In the command line, type composer require illuminate/validation

Put this code in a PHP file and then add it to the require section in herbert.config.php:


// register validator
$container->bind('translator', function(){
   return new \Symfony\Component\Translation\Translator('en_US', new \Symfony\Component\Translation\MessageSelector());
});

$container->register('Illuminate\Validation\ValidationServiceProvider');
// end register validator
``
Get the validator like so:
herbert('validator')->make(....)
PrafullaKumarSahu commented 8 years ago

@extrakun that's great, thank you for this, I am going to use it now +1:

jeremyzahner commented 8 years ago

@extrakun Would be so kind to create a pull request for your example to the docs (https://github.com/getherbert/docs)? =)

moazam1 commented 8 years ago

@extrakun It doesn't work because Laravel upgraded their version. Also, your method does not show translated validation error.

@jeremyzahner I did it but took good amount of time. I think it's more like a submitting tutorial then sending a PR.

giwrgos88 commented 8 years ago

@moazam1 can you please tell me how you use illuminate/validation?