Using Angular to design a form with 3 inputs (based on Scotch.IO tutorial for Node+Express+Angular).
I would like the user to compulsorily select values for each input before submitting the form. If he doesn't, then I would like to display an error message.
<form>
<div class="form-group">
Start Date: <input type="date" class="form-control input-lg text-center" ng-model="formData.fromDate" placeholder="yyyy-MM-dd">
</div>
<div class="form-group">
End Date: <input type="date" class="form-control input-lg text-center" ng-model="formData.toDate" placeholder="yyyy-MM-dd">
</div>
<div class="form-group">
Report Type: <select class="form-control input-lg text-center" ng-model="formData.reportType">
<option value="csv">CSV</option>
<option value="pdf">PDF</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-lg" ng-click="findStat()">Add</button>
</form>
In other words, i would not like to pass 'undefined' values back to my Node.js code.
I have tried using 'required' options, which doesn't prevent undefined values from being passed back.
Give your form a name:
<form name="myForm">
Also give your inputs a name, and add validity constraints on them (required, etc.):
<input type="date" name="date" ...
Then in your findStat() function, you can access the form and check if it's valid:
$scope.findStat = function() {
if ($scope.myForm.$invalid) {
$scope.displayError = true;
}
else {
$scope.displayError = false;
...
}
}
See http://code.angularjs.org/1.2.15/docs/api/ng/directive/form and http://code.angularjs.org/1.2.15/docs/api/ng/type/form.FormController for more information.
Related
I'm using this code below in a Laravel controller but on errors it doesn't keep the input data when it returns to the form.
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'intro' => 'required|max:140|min:40',
]);
If the intro is too short it sends the user back to the form but with empty fields.
The blade looks like this
<form action="{{route('member.store')}}" id="form" role="form" data-toggle="validator" method="post">
<div class="form-group">
<input type="text" value="" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<textarea class="form-control" id="intro" rows="2" name="intro" placeholder="A short 15-30 word intro" required></textarea>
</div>
</div>
</form>
Doesn't is come with a return to the form with the data input?
You should use old('input') in the value of the input.
For your code it should be like this:
<div class="form-group">
<input type="text" value="" class="form-control" id="name" name="name" placeholder="Name" value="{{ old('name') }}" required>
</div>
<div class="form-group">
<textarea class="form-control" id="intro" rows="2" name="intro" placeholder="A short 15-30 word intro" value="{{ old('intro') }}" required></textarea>
</div>
I use the same form to create and update my data. When i select a record to update, it fills the form correctly but if I submit, the values are undefined
<form (ngSubmit)="create(addMountForm)" #addMountForm="ngForm" ngNativeValidate>
<div class="form-row" style="margin: 0 auto;">
<div class="form-group col-md-2">
<label for="name">Name(*)</label>
<input type="text" class="form-control" [(ngModel)]="name" name="name" id="name" placeholder="Countdown-Name" required value="{{current_name}}">
</div>
<div class="form-group col-md-2">
<label for="datepicker">Datum(*)</label>
<input type="datetime-local" class="form-control" [(ngModel)]="datepicker" name="datepicker" id="datepicker" placeholder="Datum" required value="{{current_timestamp * 1000 | date: 'yyyy-MM-ddThh:mm'}}">
</div>
</div>
The variables are defined like this:
current_name:string;
current_timestamp:number;
The submitted form contains the values like this:
value:
name: undefiend
datepicker: undefiend
Html template below
<form class="form" #pubForm="ngForm">
<div class="form-group">
<label for="name">Publisher Name:</label>
<input type="text" #name id="name" ngControl="name" #name="ngForm" class="form-control" placeholder="Enter Name" style="width:50%;" required maxlength="50">
<div *ngIf="name.touched && name.errors">
<div class="alert alert-danger" *ngIf="name.errors.required" style="width:50%;">
Name is Required (Maxlength is 50 characters)
</div>
</div>
</div>
<div class="form-group">
<label for="status">Status:</label>
<label class="radio-inline">
<input type="radio" name="options" (click)="model.options = 'active'" [checked]="'active' === model.options">Active
</label>
<label class="radio-inline">
<input type="radio" name="options" (click)="model.options = 'inactive'" [checked]="'inactive' === model.options">Inactive
</label>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default" (click)="onSubmit(pubForm.value)">Submit</button>
<button type="cancel" class="btn btn-default">Cancel</button>
</div>
</form>
Component code below
model = { options: 'active' };
onSubmit(form:any) : void {
form.status = this.model.options;
console.log(form);
}
On console log now I get object on form submit but I want to pass radio button value as name input box value is passed automatically on form submit in my form above. How to do pass radio button selection as form data ? The input name above is being passed automatically on click of Submit.
EDIT I am not using resource controller but I believe my route is correct
I have a form on it called recordings I have the form like:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/recordings/create') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Client Name</label>
<div class="col-md-6">
{!! Form::select('ClientName', $client_options, '', array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">File</label>
<div class="col-md-6">
<input type="file" class="form-control" name="FileUpload">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
Then in my RecordingsController
public function store()
{
var_dump(Input::file('FileUpload'));
var_dump(Input::get('ClientName')) ;
}
My route:
Route::get('recordings/create', 'RecordingsController#create');
Route::post('recordings/create', 'RecordingsController#store');
Why is it the var_dump is null? I have a dropdown which has values in it and I already selected one. The other one is file input filed which I also selected already a file.
Try:
public function store(Request $request) {
$ClientName = $request->ClientName:
if ($this->request->hasFile('FileUpload'))
{
$files = $this->request->file('FileUpload');
....
Or simple use
dd($request);
Always a good idea here is to use Firebug - to check out which values are submitted to your script.
Works now. Problem is I am sending huge data in my post. So I did is changed the post_max_size in my php.ini. Weird though I am not getting error regarding that.
I recently started working with the Smarty Template system in my CodeIgniter projects.
All works fine and I was able to echo strings and dates. But now I have a problem with the 'form_open()' function.
view_login.tpl
<form class="contact-form" action="login/process" method="post">
<div class="row">
<div class="form-group">
<label for="username">USERNAME</label>
<input type="text" class="form-control" id="username" placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">PASSWORD</label>
<input type="password" class="form-control" id="password" placeholder="Enter Password">
</div>
<button type="submit" class="btn btn-flat flat-color">LOGIN</button>
<button type="button" class="btn btn-flat pull-right">Forgot your password?</button>
</div>
</form>
As soon as I replace the HTML tags the view doesn't load anymore. I need the form_open() because of the Cross Site Request Forgery (CSRF).
So far I've tried:
{ form_open('login/process') }
{ form url='login/process' }
{{ form_open('login/process') }}
{php} echo form_open('login/process'); {/php}
Anyone else had the same problem or knows how to fix this?
Without Smarty I never had problems with this.
Solved!
I autoloaded the form helper file.
$autoload['helper'] = array('url', 'form');
And then I used the following code:
{form_open('login/process')}
<div class="row">
<div class="form-group">
<label for="username">USERNAME</label>
<input type="text" class="form-control" id="username" placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">PASSWORD</label>
<input type="password" class="form-control" id="password" placeholder="Enter Password">
</div>
<button type="submit" class="btn btn-flat flat-color">LOGIN</button>
<button type="button" class="btn btn-flat pull-right">Forgot your password?</button>
</div>
{form_close()}
Looking at the code given above, it does not seem that the reason why the error occurred was because of the not loading the helper functions, though that may be one of the reason.
I believe the error is mainly because you added spaces between the delimiters { and }.
The default value in Smarty for the right and left delimiters are { and } respectively. Meaning:
{form_open('')} // Ok!
{ form_open('') } // Not Ok!
So, if you would like to use the second version, you will have to change your smarty settings for the right and left delimiter:
$smarty->left_delimiter = '{ ';
$smarty->right_delimiter = ' }';
Also, when you tried
{php} echo form_open('login/process'); {/php}
I believe it did not work because you are using Smarty 3. The {php} tag is deprecated in Smarty 2 and removed in Smarty 3. If you still want to use the {php} tag, you would have to use SmartyBC.