I have this simple checkbox.
My problem is, if I select any of the checkbox, it should remain checked after form submission.
<label>
<input type="checkbox" name="services_offered[]" value="water jet cutting"> Water Jet Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="plasma cutting"> Plasma Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="CNC router cutting"> CNC Router Cutting</label>
I tried to use my code before but no luck,
<?php
function isChecked($value)
{
return (!empty($_REQUEST['services_offered']) && in_array($value,$_REQUEST['services_offered']));
}
<label><input type="checkbox" name="services_offered[]" value="water jet cutting" <?php if(isChecked('water jet cutting')) echo ' checked' ?>> Water Jet Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="plasma cutting" <?php if(isChecked('plasma cutting')) echo ' checked' ?> > Plasma Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="CNC router cutting" <?php if(isChecked('CNC router cutting')) echo ' checked' ?>> CNC Router Cutting</label>
I want it to convert in "laravel way" but I dont know how.
Hope you understand me.
Thanks.
I don't know if I understand you correct but:
The form does not post values of the checkboxes.
So I made hidden inputs associated with each checkbox and maintain there values with javascript something like:
$("#services_offered").change(function() {
if(this.checked) {
$('#services_offeredHidden').attr('value','1');
}
else {
$('#services_offeredHidden').attr('value','0');
}
});
In laravel controller I play only with hidden inputs which I get the from Request $request.
When you return the view from a controller you do like this
return view('nameOfView')->with('checkbox',$checkboxValue);
and the in the blade you can access the value using:
{{$checkbox}}
Hope it helps!
If you want the "Laravel way" of doing things then I suggest you check out the HTML package by Laravel Collective for marking up the HTML forms in your view. Also make sure the methods on your controller are receiving a Request as it's argument and maybe even checkout Laravel's built-in Form Request Validation as well.
Note, your current code is very old fashioned so it might take a while to learn how to do things the modern way but stick with it and you will be rewarded.
In your view you will open your form like this:
{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}
Your method on the controller that receives the form will look something like this:
public function store(Request $request)
{
$name = $request->input('name');
//
}
But in general, because your question is too broad to answer clearly without basically re-writing your code for you, you need to start doing some tutorials to learn Laravel properly. I recommend Jeff Way's video tutorial courses on Laracasts.
I had a similar problem - here is how I solved it:
My blade template rendered a list of checkboxes as html:
<label for="sId1"><input type='checkbox' name='sIds[]' id="sId1" value="1" checked >Draft</label>
<label for="sId2"><input type='checkbox' name='sIds[]' id="sId2" value="2" checked >Submitted</label>
I wanted to a) default them all to checked; and b) preserve their checked/unchecked status between validations.
The key is to use the laravel helper function old() - and to realise the old() function returns an array - so you need to do an in_array(value,old()). You also need to cover when the old() is empty on first load by checking is old()==null:
Here is the blade template snippet I used to do it:
#foreach($wfStatusIdsArray as $wfsId=>$statusName)
<label for="sId{{$wfsId}}"><input type='checkbox' #if(old('sIds')==NULL || in_array($wfsId,old('sIds'))) checked #endif name='sIds[]' id="sId{{$wfsId}}" value="{{$wfsId}}">{{$statusName}}</label>
#endforeach
There is a bug in this - if ALL checkboxes are cleared, on validation, the checkboxes are reloaded as all ticked. This is fine for my situation so I have not fixed it. Let me know if you fix that bug!
Related
My application has a lot of models in the page. I want to detect whether user has changed value of any model on click of save. Using $watch on every model puts too much load, so don't want to use this method. Is there any good approach for this?
Small snippet is like below:
<div>
<div class="ttere2">
<input type="radio" name="nc2-radio3" ng-model="nc2PenaltyAfter" value="specificDays" />
<input class="ggfe1" ng-model="nc2AfterDays" ng-disabled="nc2PenaltyAfter!='specificDays'" type="number" min="1" max="100" step="1" value="1" />days</div>
<div class="admin_wp-line">
<input type="radio" name="nc2-radio3" ng-model="nc2PenaltyAfter" value="immediately"/> Immediately </div>
<div class="acfv1">model 1</div>
</div>
<div style="margin-top: 20px;"><button ng-click="saveData();">Done</button></div>
............too many inputs go here
</div>
Use .$dirty! Angular will set this on every element that is bound using ng-model, automatically, when it has been changed. It will also set it on the entire form. You can access it in code like this:
if ($scope.myForm.$dirty) {
// Your code here
}
Angular will provide six useful variables on the form, and every ngModel-bound element in your form: $dirty and $pristine, $valid and $invalid, and $touched and $untouched. You can mix and match these to drive a lot of useful behaviors, and they're available both in your controller (using the expression shown above) and your template (directly).
I am trying to use AngularJS Validation in order to validate a simple form, however I was having troubles getting my ng-class to show the correct class based off whether or not the input was dirty or not. Then when I looked at the actual HTML of the page, the <form> tags are not even in the document at all!
<form novalidate name="infoForm">
<p>To start, provide some basic information about the project.</p>
<ul class="ulFormGeneral">
<li>
<label>Company name</label>
<input id="CompanyName" ng-class="{ cvError : infoForm.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
</li>
</ul>
</form>
I want the cvError class to be added to this input if it is dirty, but nothing happens when I look at this in the browser. What am I doing wrong that is causing the <form> to just leave the DOM and then not work with my Angular expressions?
Welcome to the Angular world, no forms required! Here, the model is king. It looks like the problem is the ng-model and ng-class are point at different places.
Point everything at form.CompanyName (assuming that is the model name is form in the $scope):
<input id="CompanyName" ng-class="{ cvError : form.CompanyName.$dirty }" ng-model="form.CompanyName" name="CompanyName" maxlength="100" type="text" required />
The ng-model binds to the $scope. When you change the input field, it is automatically updated in the $scope. No form is needed or hitting a submit button to get the data. The $scope is updated with each key stroke.
The controller should do the work of figuring out what to do with the changes in the model. For example, you can add an ng-click to a button that fires a function defined by the controller to save the model.
I'm looking to do use some of the validation features in an Angular Directive that I am building up. However, the directive may or may not be inside of a form. Is there a way to access the validation status of a model without trying to access the state of a form?
My template is along the lines of....
<select id="{{$id}}key" ng-model="newItem.key"
ng-options="key as key.label for key in tableKeys" required>
</select>
<span class="error" ng-show="newItem.key.$error.required">Required!</span>
<input id="{{$id}}value" type="text" ng-model="form.newItem.value" required/>
<span class="error" ng-show="newItem.value.$error.required">Required!</span>
<button ng-click="addItem()">Add Item</button>
(Not seeing any validation messages here)
On top of it, I want to addItem to check the state of validation as well
$scope.addItem = function(){
if(<do something to check validation>)
{
<do some other thing>
}
Any help would be much appreciated!
Thanks,
Andrew
My understanding is that you want to avoid using the form.xyz.$error attributes for error checking.
I do not know how to do it using a directive, but I do know how to do it using the controller.
In that case, you can use the $scope.$watch function on your model.
It would be something like this:-
1.) In your controller,
$scope.$watch('newItem.key',function(){
if( <condition to validate> ==true)
{
$scope.selectError ="errorMessage";
}
});
2.) In your HTML
<select id="{{$id}}key" ng-model="newItem.key"
ng-options="key as key.label for key in tableKeys" required>
</select>
<span class="error" ng-show="selectError">{{selectError}}</span>
Note: This is only for the select element. You can bind the text input to a model, and do the same for it as well.
The $scope.$watch function will watch out for any changes to the specified model, and will execute the accompanying code whenever any change occurs.
I have two tables in my database "cars" and "car_types". "cars" table refers to "car_types" by "car_type_id". For example "car_types" has 2 fields "id" and "car_type". It also has 3 entries "new", "used dealer", "used private". How can I show these 3 entries as checkbox in my view.
I'm trying to adjust the output from:
foreach ($car_types as $car_type)
{
$car_type_new[$car_type['CarType']['id']]=$car_type['CarType']['car_type'];
}
echo $this->Form->input('Car.car_type_id',array('div'=>false,'multiple'=>'checkbox','options'=>$car_type_new,'style'=>"margin-left:20px; padding:0;"));
I also want to remove the wrapper div around each checkbox.
Each checkbox is output by the Form helper like so, even if the div => false option is set:
<input type="hidden" id="CarCarTypeId" value="" name="data[Car][car_type_id]">
<div class="checkbox"><input type="checkbox" id="CarCarTypeId1" value="1" name="data[Car][car_type_id][]"><label for="CarCarTypeId1">New</label></div>
<div class="checkbox"><input type="checkbox" id="CarCarTypeId2" value="2" name="data[Car][car_type_id][]"><label for="CarCarTypeId2">Used Dealer</label></div>
<div class="checkbox"><input type="checkbox" id="CarCarTypeId3" value="3" name="data[Car][car_type_id][]"><label for="CarCarTypeId3">Used Private
</label></div>
the div => false option only removes the div wrapped around the entire collection of checkboxes, not each checkbox.
Any Ideas on how I could remove the div that wraps around each checkbox? And please do tell me if I am doing it wrong.
I know the question is about CakePHP 1.3, but I found this while searching on the Internet, so I'll share the solution that worked for me anyway.
You can pass a corresponding key to the $options for the input() helper function:
'div'=>false
More info: http://api.cakephp.org/2.5/class-FormHelper.html#_input
Taking a look at the API, you can use the after and before array options to control what goes before and after the <input> and <label> pair. Here is the documentation on the input function or the API
Alternatively, you you could use the checkbox function (API) in the Form helper to get around it
I am learning CakePHP. I am trying to develop a login system, but instead of using the login.ctp as a user/view, I wish I could use it as a element because I have the login form in many other pages. How can I declare within my users_controller that my function login() will use just the element. I said that because I used $this->render('/elements/login') and it did work. However, my element login.ctp was within my default.ctp layout, thus, I had two login forms. One was the element and the other as my content in my default.ctp layout.
Thanks!
Layouts are for the "greater" markup of a page: head and meta information, includes, "footer" stuff like your analytics. Generic content pointers go in the middle - so there's no need to mark anything as specific as a form in a layout unless you really do want to include that form on every page that uses that layout.
It sounds like you either called $this->render() in your view? $this->render() is a controller method. $this->element is the view method.
Or you called $this->element('/elements/login'); from in your login.ctp view? That would mean the controlller rendered the default login.ctp view, which called the login.ctp element.
And thus you saw two. To fix:
To return something other than the default view associated with an action (such as your login snippet), call $this->render('name/of/whatever'); as the last line of the controller method. It will return the view you specify; set will pass whatever variables to it, just like a regular view call and if you want to get fancy, specify the layout as ajax and watch the magic start like $this->render('/elements/login', 'ajax').
If you need to call several elements in a single view file, use the method $this->element('/fancy/nav/whatever'); you can also place them in layouts as appropriate (navigation, etc.)
HTH. :)
I had the same thing done to my project and this is what I did.
Basically, I created a new loginElement.ctp and placed it in the element folder. I create new sets of HTML code that would fit the layout where I wanted to use this element and the loginElement.ctp <form would then submit the data to login action in the users_controller.
If you need to and when I get home later, I can post my exact code here.
==================================== EDIT =========================================
These are the codes I used:
First of all you will notice that the action in the login form points to /login.
I have that setup in my /config/routes.php file as such
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Then the other codes are below
/views/elements/thinlogin.ctp
<div id="login">
<form method="post" action="/login" accept-charset="utf-8" class="formBox">
<fieldset>
<input type="hidden" name="_method" value="POST" />
<div class="form-col">
<label for="username" class="lab">Username/Email</label>
<input name="data[User][username]" type="text" id="UserUsername" class="input">
</div>
<div class="form-col form-col-right">
<label for="password" class="lab">Password</label>
<input type="password" name="data[User][password]" id="UserPassword" class="input">
</div>
<div class="form-col form-col-submit">
<input name="" value="Login" class="submit" type="submit">
</div>
<div class="form-col form-col-check">
<label><input name="remember" class="checkbox" type="checkbox">Remember me on this computer</label>
</div>
</fieldset>
</form>
</div>
/views/pages/home.ctp
<div id="home_top_right_top">
<?php
if (!$this->Session->check('Auth.User.id'))
{
echo $this->element('login/thinlogin');
}else{
echo $this->element('login/loggedin');
}
?>
</div>