Saturday, 15 February 2014

php - include helper file in blade template page -



php - include helper file in blade template page -

i wondering how include helpers function form validation. i'm new laravel have basic understanding of how works don't know how include file has functions in want utilize validate form error checking. want utilize in forms. how have page on global scale on pages have forms

helper.php

<?php public function haserror($error) { if(strlen($error) > 0) { echo "has-error"; } }

and insdie page utilize haserror create.blade.php

<div class="form-group <?php haserror($errors->get("keywords")) ?>"> {{ form::label('keywords', 'keywords', array('class' => 'col-sm-2 control-label')) }} <div class="col-sm-10"> {{ form::text('keywords', null, array( 'placeholder' => 'keywords', 'class' => 'form-control' )) }}

how handle validation in controller? when homecoming after validation:

// homecoming error homecoming redirect::back() ->withinput() ->witherrors($validator);

you can show errors this:

<small class="red">{{{ $errors->first('keywords') }}}</small>

and can utilize show particular classes:

{{ $errors->has('keywords') ? 'has-error' : '' }}

example:

<div class="form-group {{ $errors->has('keywords') ? 'has-error' : '' }}">

--

for creating helper file, , access them 'globally'

create folder named: libraries -> app/libraries

create file(class) in library: helper.php

then add together code helper.php:

<?php class helper{ public function haserror($error) { if(strlen($error) > 0) { echo "has-error"; } }

edit composer.json in root of application, , add: "app/libraries"

example (composer.json):

"autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/testcase.php", "app/libraries" ] },

in console type:

composer dump-autoload

now can phone call 'function' 'anywhere':

helper::haserror($error);

php laravel laravel-4

No comments:

Post a Comment