When do we use Element? When do we use Helper? When do we use View Cells? in CakePHP 3 - cakephp

I am using CakePHP 3.x
I am trying to skin a themeforest theme into a CakePHP plugin.
Midway, I am deciding whether to skin a portlet into helper, element, or view cell.
The portlet html code looks something like this:
<!-- BEGIN SAMPLE FORM PORTLET-->
<div class="portlet box yellow">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-gift"></i> More Form Samples
</div>
<div class="tools">
<a href="" class="collapse">
</a>
<a href="#portlet-config" data-toggle="modal" class="config">
</a>
<a href="" class="reload">
</a>
<a href="" class="remove">
</a>
</div>
</div>
<div class="portlet-body">
<h4>Inline Form</h4>
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail2">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail2" placeholder="Enter email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me </label>
</div>
<button type="submit" class="btn btn-default">Sign in</button>
</form>
<hr>
<h4>Inline Form With Icons</h4>
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail22">Email address</label>
<div class="input-icon">
<i class="fa fa-envelope"></i>
<input type="email" class="form-control" id="exampleInputEmail22" placeholder="Enter email">
</div>
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword42">Password</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<input type="password" class="form-control" id="exampleInputPassword42" placeholder="Password">
</div>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me </label>
</div>
<button type="submit" class="btn btn-default">Sign in</button>
</form>
<hr>
<h4>Horizontal Form</h4>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail1" class="col-md-2 control-label">Email</label>
<div class="col-md-4">
<input type="email" class="form-control" id="inputEmail1" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword12" class="col-md-2 control-label">Password</label>
<div class="col-md-4">
<input type="password" class="form-control" id="inputPassword12" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-4">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me </label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn blue">Sign in</button>
</div>
</div>
</form>
<hr>
<h4>Horizontal Form With Icons</h4>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail12" class="col-md-2 control-label">Email</label>
<div class="col-md-4">
<div class="input-icon">
<i class="fa fa-envelope"></i>
<input type="email" class="form-control" id="inputEmail12" placeholder="Email">
</div>
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-md-2 control-label">Password</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa fa-user"></i>
<input type="password" class="form-control" id="inputPassword1" placeholder="Password">
</div>
<div class="help-block">
with right aligned icon
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-4">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me </label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn green">Sign in</button>
</div>
</div>
</form>
<hr>
<h4>Column Sizing</h4>
<form role="form">
<div class="row">
<div class="col-md-2">
<input type="text" class="form-control" placeholder=".col-md-2">
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder=".col-md-3">
</div>
<div class="col-md-4">
<input type="text" class="form-control" placeholder=".col-md-4">
</div>
<div class="col-md-3">
<input type="text" class="form-control" placeholder=".col-md-2">
</div>
</div>
</form>
</div>
</div>
<!-- END SAMPLE FORM PORTLET-->
The look is like this:
My question is how do we know when we should use Element? When we should use Helper? and When should we use View Cells?
And which case should I use for the above? I am leaning towards Helper.

Element
Use it when you need to repeat presentation related stuff, usually HTML, a lot. For example I have a project in which three tables use records of an addresses table. The form part of all of these three that contains the address data is an element.
Helper
Use it to encapsulate view logik, don't put HTML in it if possible or other presentation related things. For example let it do something and depending on the result you can use an element of that result type to render the data: return $this->_view->render('items/' . $type . '_item');
If you look at the core helpers for example the HtmlHelper you'll see a property $_defaultConfig:
protected $_defaultConfig = [
'templates' => [
'meta' => '<meta{{attrs}}/>',
'metalink' => '<link href="{{url}}"{{attrs}}/>',
/*...*/
These are the template strings that are used to generate the HTML output. This separtes the markup pretty nice from the actual code that generates it. Take a look at the FormHelper as well, it's using widgets to render more complex output.
So this works fine with element like pieces of markup. By a rule of thumb I would say if your markup is longer than what you see there make it an element and call it from within the helper or make it a widget.
View Cells
Think of view cells as "Mini MVC" stacks that have a view and can load multiple models. They're IMHO similar to AngularJS directives if you're familiar with them. See this article for an example. I really suggest you to read it, it explains them and their use cases in detail.
I haven't done much with them yet but they can be used to replace requestAction() calls for example. You won't "pollute" your controller with methods that are not intended to be access by a request.
Taken from the linked article above:
One of the most ill-used features of CakePHP is View::requestAction().
Developers frequently use this all over their applications, causing
convoluted cases where you need to figure out if you are within a web
request or an internal action request, cluttering controllers. You
also need to invoke a new CakePHP request, which can add some unneeded
overhead.
Disclaimer
The above reflects my personal view on these things, there is no ultimate and final rule how you have to use these three things. The goal is always clean and re-useable code and proper separation of concerns. How you archive that is up to you, you've got the tools. :)

Related

all the data is gone in the form when i put ng-model angularjs

I want to load data of a selected row in a form in order to update it. I successfully had the data loaded in the form, the but the problem is when I use ng model to save the data all the stuff have been put in the form fields disappear.
<div class="panel-body pan" ng-if="loadedpr">
<form action="#">
<div class="form-body pal">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="refprojet" class="control-label">
Référence Projet *</label>
<input id="refprojet" type="text" value="{{loadedpr.ref_projet}}" class="form-control" disabled ng-model="ref_projet"/>
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="intitulefr" class="control-label">
Intitulé *</label>
<input id="intitulefr" type="text" value="{{loadedpr.intitule_fr}}" class="form-control" ng-model="intitule_fr" />
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="ctot" class="control-label"> Coût Total (TND) *</label>
<input id="ctot" type="text" value="{{loadedpr.cout_total}}" ng-model="cout_total" class="form-control" disabled ng-model="cout_total" />
</div>
</div>
</div>
<div class="form-group">
<label for="description" class="control-label">
Description</label><textarea id="description" rows="3" value="{{loadedpr.description}}" ng-model="description" class="form-control"></textarea>
</div>
<div class="form-actions text-center pal">
<button type="submit" class="btn btn-primary" ng-click="upadateProjet()">Valider</button>
</div>
</div>
</form>
this is the angularjs method:
$scope.updateProjet= function(){
$scope.projet={'ref_projet':$scope.refprojet,'intitule_fr':$scope.intitule_fr,'description':$scope.description,cout_total':$scope.cout_total};
$http.put("/editprojet", $scope.projet)
.success(function(data,status,headers,config){
});
}
rest controller
#RequestMapping(value="/editprojet",method=RequestMethod.PUT)
public Projet editProjet(#RequestBody Projet p) {
return projetMetier.editProjet(p);
}
you are using button type="submit"
it will clear the form use button tag
<button></button>
$scope.updateProjet= function(projData){
$http.put("/editprojet", projData)
.success(function(data,status,headers,config){
}).error(function(data){
console.log(data)
});
}
<div class="panel-body pan" ng-if="loadedpr">
<form action="#">
<div class="form-body pal">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="refprojet" class="control-label">
Référence Projet *</label>
<input id="refprojet" type="text" value="{{loadedpr.ref_projet}}" class="form-control" disabled ng-model="proj.ref_projet"/>
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="intitulefr" class="control-label">
Intitulé *</label>
<input id="intitulefr" type="text" value="{{loadedpr.intitule_fr}}" class="form-control" ng-model="proj.intitule_fr" />
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="ctot" class="control-label"> Coût Total (TND) *</label>
<input id="ctot" type="text" value="{{loadedpr.cout_total}}" ng-model="proj.cout_total" class="form-control" disabled ng-model="proj.cout_total" />
</div>
</div>
</div>
<div class="form-group">
<label for="description" class="control-label">
Description</label><textarea id="description" rows="3" value="{{loadedpr.description}}" ng-model="proj.description" class="form-control"></textarea>
</div>
<div class="form-actions text-center pal">
<button type="button" class="btn btn-primary" ng-click="upadateProjet(proj)">Valider</button>
</div>
</div>
</form>
</div>
have you tried removing the value attribute? this happened to me when I once added DOM forms on the fly, I managed by using jquery to force capture by:
$(this).find('.inputClass').val();
this sort of jequery is already embeded inside Angular so no need to add the the library.

How to insert array data into database using ajax in codeigniter

How to insert array data into the database using ajax in CodeIgniter?
In my view I use code below, I have stack to find the solution because have many different structures
<form class="form-story" action="" onsubmit="return false" id="form-story">
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #1</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #1" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #1">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #2</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #2" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #2">
</div>
</div>
</div>
<div class="tag-input form-group">
<div class="row">
<div class="col-xs-4 col-md-4 text-right">
<label for="crew" class="control-label">Crew #3</label>
</div>
<div class="col-xs-8 col-md-5">
<input type="text" name="crew_name[]" class="form-control" placeholder="NAME OF CREW MEMBER #3" style="margin-bottom: 5px">
<input type="text" name="crew_email[]" class="form-control" placeholder="EMAIL OF CREW MEMBER #3">
</div>
</div>
</div>
<div id="btn-add-more-wrapper" class="form-group text-center btn-add-more-wrapper">
<a href="javascript:void(0)" id="btn-add-more">
<i class="fa fa-plus"></i> <br> Add More
</a>
</div>
<div class="btn-groups-wrapper">
<div class="form-group">
<input type="submit" onClick="" value="SUBMIT" id="btnSubmit" class="btn btn-default is-disabled">
</div>
</div>
</form>
<script>
$('#form-story').submit(function() {
var dataString = $(this).serialize();
$.ajax({
type : 'post',
url : '<?php echo base_url()?>index.php/the_flow_crew/save_tag_member',
data : dataString,
dataType : 'json',
success : function(res) {
console.log(res);
}
});
return false;
});
</script>
Please help me to give the solution, Thank you
Make your post data into jsonencode and try to store in database. It will work.
For example
$crew_name = json_encode($_POST['crew_name']);
Above code will convert your array into json string. When you try to retrieve the data back in array format try to use json_decode($column_name) it help you to sort out your problem
If you want to store entire post into database pass your $_POST in json_encode($_POST)
Let me know if you need more guidelines on it. I'm happy to help you out.

Angular.js ng-click not registering ng-model on form

So for some reason I am only getting the password on click. I've tried moving the div around. I used a div instead of a form. Been trying to figure this out. Please Help.
<div class="container">
<div class="row">
<div class="col-md-offset-5 col-md-3">
<form class="form-login">
<h4>Welcome</h4>
<input type="text" ng.model="vm.user.name" class="form-control input-sm chat-input" placeholder="username" />
</br>
<input type="text" ng-model="vm.user.password" class="form-control input-sm chat-input" placeholder="password" />
</br>
<div class="wrapper">
<span class="group-btn">
<button type="submit" ng-click="vm.authenticate(vm.user)" class="btn btn-primary btn-md">login <i class="fa fa-sign-in"></i></a>
</span>
</div>
</form>
</div>
</div>
Hya you used ng.model instead of ng-model :3
And a long day it has been indeed

Angular 2 Parsley Prevent Form Submission When Input is Invalid

<div>
<form class="form-horizontal form-label-left parsleyjs" method="post" data-parsley-priority-enabled="false"
(ngSubmit)="submitForm()">
<fieldset>
<div class="form-group row">
<label class="form-control-label col-md-3 col-xs-12" for="task-id">
ID
<span class="help-block">
Number Only
</span>
</label>
<div class="col-md-9 col-xs-12">
<input type="text" id="task-id" name="task-id" class="form-control"
data-parsley-type="number"
required="required">
</div>
</div>
</fieldset>
<div class="form-actions">
<button type="submit" id="task-detail-submit-button" class="btn btn-danger btn-rounded pull-xs-right">Submit</button>
</div>
</form>
</div>
How can the Parsley stop invoking "submitForm()" during form submission when the input is invalid. In this case, "task-id" is not a number.
hope this code give you a idea
<form class="form-horizontal form-label-left parsleyjs" name="formname" method="post" data-parsley-priority-enabled="false"
(ngSubmit)="formname.valid && formname.submitForm()">
This code is not tested if it does not work sorry for it

Jhipster - issue with custom authentication view

I am trying to modify the Jhipster template by doing the following:
If the user is not authenticated , the authentication form is showing (instead of showing a link to the authentication page as it is set by default). I m showing it in the main.html view
<div class="row">
<div class="col-md-4">
<span class="hipster img-responsive img-rounded"></span>
</div>
<div class="col-md-8">
<h1 translate="main.title">Welcome, Java Hipster!</h1>
<p class="lead" translate="main.subtitle">This is your homepage</p>
<div ng-switch="authenticated">
<div class="alert alert-success" ng-switch-when="true"
translate="main.logged.message" translate-values="{username: '{{account.login}}'}">
You are logged in as user "Admin".
</div>
<div ng-switch-when="false">
<div class="row">
<div class="col-md-4">
<h1 translate="login.title">Authentication</h1>
<div class="alert alert-danger" ng-show="authenticationError" translate="login.messages.error.authentication">
<strong>Authentication failed!</strong> Please check your credentials and try again.
</div>
<form class="form" role="form" ng-submit="login()">
<div class="form-group">
<label for="username" translate="global.form.username">Login</label>
<input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" ng-model="username">
</div>
<div class="form-group">
<label for="password" translate="login.form.password">Password</label>
<input type="password" class="form-control" id="password" placeholder="{{'login.form.password.placeholder' | translate}}"
ng-model="password">
</div>
<div class="form-group">
<label for="rememberMe">
{{"login.form.rememberme" | translate}}
<input type="checkbox" class="checkbox inline_class" id="rememberMe"
ng-model="rememberMe" checked>
</label>
</div>
<button type="submit" class="btn btn-primary" translate="login.form.button">Authenticate</button>
</form>
<p></p>
<div class="alert alert-warning" translate="global.messages.info.register">
You don't have an account yet? Register a new account
</div>
</div>
</div>
</div>
</div>
</div>
</div>
and modified apps.js :
.otherwise({
templateUrl: 'views/main.html',
controller: 'LoginController',
access: {
authorizedRoles: [USER_ROLES.all]
}
});
It works if I m not using the ng-switch-when="false" statement.
If I m using this statement, the value of $scope.password and $scope.login are "undefined" in the LoginController.
Of course this statement is mandatory if I want to show the authentication form only if the user is not authenticated.
I can't understand why.
If you can help me on this, it would be great.
Thanks.
It's a common gotcha with ng-switch and other directives that create sub-scopes. This should work:
<div class="row">
<div class="col-md-4">
<span class="hipster img-responsive img-rounded"></span>
</div>
<div class="col-md-8">
<h1 translate="main.title">Welcome, Java Hipster!</h1>
<p class="lead" translate="main.subtitle">This is your homepage</p>
<div ng-switch="authenticated">
<div class="alert alert-success" ng-switch-when="true"
translate="main.logged.message" translate-values="{username: '{{account.login}}'}">
You are logged in as user "Admin".
</div>
<div ng-switch-when="false">
<div class="row">
<div class="col-md-4">
<h1 translate="login.title">Authentication</h1>
<div class="alert alert-danger" ng-show="authenticationError" translate="login.messages.error.authentication">
<strong>Authentication failed!</strong> Please check your credentials and try again.
</div>
<form class="form" role="form" ng-submit="login()">
<div class="form-group">
<label for="username" translate="global.form.username">Login</label>
<input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" ng-model="$parent.username">
</div>
<div class="form-group">
<label for="password" translate="login.form.password">Password</label>
<input type="password" class="form-control" id="password" placeholder="{{'login.form.password.placeholder' | translate}}"
ng-model="$parent.password">
</div>
<div class="form-group">
<label for="rememberMe">
{{"login.form.rememberme" | translate}}
<input type="checkbox" class="checkbox inline_class" id="rememberMe"
ng-model="$parent.rememberMe" checked>
</label>
</div>
<button type="submit" class="btn btn-primary" translate="login.form.button">Authenticate</button>
</form>
<p></p>
<div class="alert alert-warning" translate="global.messages.info.register">
You don't have an account yet? Register a new account
</div>
</div>
</div>
</div>
</div>
</div>
Instead of $parent you may bind values to an object, but then you will need to modify Jhipster's LoginController accordingly (and also make sure to update login.html or create a variant of the controller for the main screen only)

Resources