Laravel and angularjs form validation - php

I am new to angularjs and im playing around with it.
I'm stuck with one thing, in jQuery it's more easier to retrive the validation error messages json object from laravel, with angular i am able, but i am doing it this way and im sure there is a more effective way
My from
<div class="row">
<div class="col-lg-8">
<h5><?php echo Lang::get('auth.signup') ?></h5>
<div class="page-divider"></div>
<form name="myForm" ng-controller="formController" ng-submit="signupPost()" class="form-horizontal" novalidate>
<div class="form-group">
<label for="first_name" class="col-lg-3 control-label"><?php echo Lang::get('form.first_name') ?></label>
<div class="col-lg-8">
<input type="text" name="first_name" ng-model="formData.first_name" id="first_name" class="form-control input-small">
<span class="help-block" ng-show="errors['first_name'][0]">{{ errors['first_name'][0] }}</span>
</div>
</div>
<div class="form-group">
<label for="last_name" class="col-lg-3 control-label"><?php echo Lang::get('form.last_name') ?></label>
<div class="col-lg-8">
<input type="text" name="last_name" ng-model="formData.last_name" id="last_name" class="form-control input-small">
<span class="help-block" ng-show="errors['last_name'][0]">{{ errors['last_name'][0] }}</span>
</div>
</div>
<div class="form-group">
<label for="username" class="col-lg-3 control-label"><?php echo Lang::get('form.username') ?></label>
<div class="col-lg-8">
<input type="text" name="username" ng-model="formData.username" id="username" class="form-control input-small">
<span class="help-block" ng-show="errors['username'][0]">{{ errors['username'][0] }}</span>
</div>
</div>
<input type="submit" value="<?php echo Lang::get('auth.signup') ?>" class="btn btn-primary">
</form>
</div>
</div>
Angular controller
function formController($scope, $http)
{
$scope.formData = {};
$scope.signupPost = function() {
$http.post('signup', $scope.formData).success(function(data){
if(data.msg == "success")
{
$location.path(data.redirect)
}
else
{
$scope.errors = data.error_msg;
}
});
}
}
And the json what laravel retunrs if the form validation fails
$messages = $val->messages();
$data = array(
'error_msg' => array(
'first_name' => $messages->get('first_name'),
'last_name' => $messages->get('last_name'),
'username' => $messages->get('username'),
'profession' => $messages->get('profession'),
'location' => $messages->get('location'),
'email' => $messages->get('email'),
'gender' => $messages->get('gender'),
'password' => $messages->get('password'),
'dob' => $messages->get('dob'),
'confirm_password' => $messages->get('confirm_password'),
));
}
return Response::json($data);
I tried a few variations and currently it works like this in the form, show the form validation error messages if its set, this way errors['first_name'][0] for all fields.
My question is, is there a more effective way doing this? If someone could show me an example would be great
Thank you

Well you can do something like this
<div class="col-lg-8">
<input type="text" name="first_name" ng-model="formData.first_name" id="first_name" class="form-control input-small">
<span class="help-block" ng-show="errors.first_name[0]">{{ errors.first_name.toString()}}</span>
</div>
The toString() function would concatenate the string array using , as separator. If you want customization of the content your option are to
Write a javascript function, that takes and returns some formatted data.
More angular way would be to do a ng-repeat on the errors.
<span ng-repeat='error in errors.first_name'>
{{error}}
</span>

I know the question is old but I want to share my awesome new angular directive, I made a project on Github and I think that it just rocks compare to whatever is/was available...I based myself on the excellent Laravel PHP Framework and made it available under Angular... Since I use very similar approach compare to Laravel but using an AngularJS Directive (my own directive), you will find my implementation very easy to follow.
<!-- example 1 -->
<label for="input1">Simle Integer</label>
<input type="text" validation="integer|required" ng-model="form1.input1" name="input1" />
<span class="validation text-danger"></span>
<!-- example 2 -->
<label for="input2">Alphanumeric + Exact(3) + required</label>
<input type="text" validation="alpha|exact_len:3|required" ng-model="form1.input2" name="input2" />
<span class="validation text-danger"></span>
So I can define whatever amount of validation rules which I want in a simple directive validation="min_len:2|max_len:10|required|integer" and the error message will always display in the next <span> Don't you guys like it already? 1 line of code for your input, 1 line of code for the error display, you can't be simpler than that...oh and I even support your custom Regex if you want to add :)
No more clustered Form with 10 lines of code for 1 input when the only thing you need is 2 lines, no more, even for an input with 5 validators on it. And don't worry about the form not becoming invalid, I took care of that as well, it's all handled the good way.
Take a look at my Github project Angular-Validation and spread the word =)
EDIT
To make an even more smoother user experience, I added validation on timer. The concept is simple, don't bother the user while he's busy typing but do validate if he makes a pause or change input (onBlur)... Love it!!!
You can even customize the timer as per your liking, I've decided to default it to 1 second within the directive but if you want to customize you can call as for example typing-limit="5000" to make a 5 sec. timeout. Full example:
<input type="text" validation="integer|required" typing-limit="5000" ng-model="form1.input1" name="input1" />
<span class="validation text-danger"></span>
DEMO
Added a live demo on Plunker

Related

Symfony crawler selectButton method can't get form

here is my html
<form name="station" method="post" action="/stations/new" role="form">
<div class="form-group">
<label class="control-label required" for="station_name">Name</label>
<input type="text" id="station_name" name="station[name]" required="required" maxlength="255" class="form-control" >
</div>
<div class="form-group">
<div class="checkbox">
<label for="station_active">
<input type="checkbox" id="station_active" name="station[active]" value="1" />Active</label>
</div>
</div>
<div class="form-group">
<button type="submit" id="station_submit" name="station[submit]" class="btn btn-primary">Ajouter</button>
</div>
<input type="hidden" id="station__token" name="station[_token]" class="form-control" value="aze123aze" >
</form>
i want to get my form using the crawler. I tried the selectButton method like this
$form = $crawler->selectButton('station[submit]')->form(array());
but i get the error : InvalidArgumentException: The current node list is empty.
what is the problem ?
Unfortunately I have no enough rating to just write a comment instead of put it in the answer.
So, could you please show how are you getting $crawler? Problem might be:
$crawler not point to DOM which contains this form
this form appears on page after some java script actions(Ajax for example), but not sure that this is your case.
The selectButton method accept the value The button text. So Try with:
$form = $crawler->selectButton('Ajouter')->form(array());
Hope this help

Laravel testing phpunit press NotFoundHttpException

I'm trying to setup phpunit tests for a project with Laravel 5.1.40 (LTS), php 5.6.28, and phpunit 4.8.27. I'm sorry if this issue has been solved before, but I couldn't find anything.
public function testAdminLogin()
{
$this->visit('/auth/login')
->type('email#address.com', 'email')
->type('1234567890', 'password')
->press('Login');
}
There seem to be an issue with press('STRING') with both <button> and <input> as submit buttons. Below is the error message I receive.
1) ExampleTest::testAdminLogin
A request to [http://localhost/auth/login] failed. Received status code [500].
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:684
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:671
C:\xampp\htdocs\project\tests\ExampleTest.php:52
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
However, when I change the <button> tag to an <a> tag, add an id to it, and replace the press(STRING) function with the click(ID) function, the test passes. I could change the <button> to an <a>, but that would only a temporary fix, and future cases might not allow the tag change.
Below is the HTML form with the <button> tag.
<form action="/auth/login" method="POST" class="form-horizontal">
<div class="form-group">
<label for="email" class="col-sm-4 control-label">E-Mail</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" value="{{ old('email') }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default btn-login">Login</button>
</div>
</div>
</form>
You said you define auth routes manually. In this case you should have POST route for sending login form:
Route::post('auth/login', ....
It works in a href because it sends GET request for which you have route. Form sends POST request by default.

How to pass/access data correctly to controller in Laravel 5.2

New to laravel and followed their intermediate task list through to try grasp the concepts.
I did a php artisan migrate in CLI to add 2 new columns to a task table,The first column is DATE called due & The Second column is TEXT called description,i have also added to the view so it now looks like this
<form action="/task" method="POST" class="form-horizontal">
{{ csrf_field() }}
<!-- Task Name -->
<div class="form-group">
<label for="task-name" class="col-sm-1 control-label">Task</label>
<div class="col-sm-3">
<input type="text" name="name" id="task-name" class="form-control" value="{{ old('task') }}">
</div>
<label for="task-due" class="col-sm-2 control-label">Due Date</label>
<div class="col-sm-4">
<input type="date" name="due_date" id="task-due" class="form-control" value="">
</div>
</div>
<!-- Add Task Button -->
<div class="form-group">
<label for="task-due" class="col-sm-1 control-label">Description</label>
<div class="col-sm-9">
<input type="text" name="description" id="task-description" class="form-control" value="">
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-default">
<i class="fa fa-btn fa-plus"></i>Add Task
</button>
</div>
</div>
</form>
Just some extra inputs called, due_date & description which I want to fill in a post to the database.
Controller :
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
]);
$request->user()->tasks()->create([
'name' => $request->name,
'due'=>$request->due_date,
'description'=>$request->description,
]);
return redirect('/tasks');
}
I'm not sure if I've written it right as when I post, the name of the task does into the database, but the columns due go to 0000-00-00 and column description is empty.
Am I calling the fields properly in my controller? I've tried swapping names around but I thought the $Request variable contained the form data.
All help and explanations are welcome.
You should make sure you have for your Task model set $fillable property with name, due and description. Now you probably have only name so other are filled by default values and not by those from input
Found the solution,
My fault for not reading the page through properly.
protected $fillable = ['name','due','description'];
I had forgot to increase the $fillable variable set inside the model, so the create() method was only putting data inside the $fillable in
Apologies for wasting anyones time

CAKEPHP - 'isUnique' field validation message

I've read all the questions related to the subject but nothing solved my problem.
I'm using cakephp $validate on the model, is working fine, but I need to put a message identifying the error, I've put the attribute message in array but it doesn't appear anywhere.
If anyone can help me and give examples, please comment here.
public $validate = [
'number' => [
'rule' => 'isUnique',
'message' => 'Serial number should be unique.'
],
];
Form input:
<div class="form-group col-md-6">
<label class="form-label" for="serial">Serial number </label><br>
<input id="serial" name="serial" value="<?php echo $data['serial']; ?>" type="text" data-bv-notempty="true" required="required" class="form-control" <?php echo $readonly; ?>>
<div class="help-block with-errors"></div>
</div>
Form create:
echo $this->Form->create(null, array(
"role" => "form",
"data-toggle" => "validator",
));
It's not rendering the error message because you haven't included the code that does so.
The code in your view should look something like this:
<div class="form-group col-md-6">
<label class="form-label" for="serial">Serial number </label><br>
<input id="serial" name="serial" value="<?php echo $data['serial']; ?>" type="text" data-bv-notempty="true" required="required" class="form-control" <?php echo $readonly; ?>>
<div class="help-block with-errors">
<? if ($this->Form->isFieldError('serial')) {
echo $this->Form->error('serial');
} ?>
</div>
</div>
Instead of hard coding the HTML for the input field, you could perhaps use FormHelper::input(), which takes care of everything (including errors) for you.
From the FormHelper page in the manual: Displaying and checking errors

laravel not loading my form section?

im trying to build a user login and registration form and this is my route :
Route::get('/register', function()
{
return View::make('register');
});
Route::get('/register', function()
{
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$username = Input::get('username');
return View::make('registered')->with('username',$username);
});
and this is my html :
<div class="container">
{{ Form::open(array('url' => 'register', 'class' => 'form-horizontal')) }}
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username"></label>
<div class="col-md-4">
<input id="username" name="username" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password"></label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Appended checkbox -->
<div class="form-group">
<label class="col-md-4 control-label" for="appendedcheckbox"> </label>
<div class="col-md-4">
<div class="input-group">
<input id="appendedcheckbox" name="appendedcheckbox" class="form-control" type="text" placeholder="">
<span class="input-group-addon">
<input type="checkbox">
</span>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"> </label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-inverse"> </button>
</div>
</div>
</fieldset>
</div>
few problems :
1.
my form does not loads and i see just
the last button for submitting the form and : ' you have registered in $username ' which i design to loads AFTER user submitted
2.my localhost:8000 loaded laravel first page one time but when i began to work on the project i just receiving blank white page and currently accessing my file like this : http://localhost/vendor/bin/crm/public/register
3.
is hashing in laravel secure enough? or should i do something else ?
4.
my way of doing this is alright or there is a better way for login and reg using laravel ?
You have two routes responding to get requests on /register. Change the second one to Route::post(...) and I would also change both to just register. There isn't a need to prepend a slash onto your routes.
Hashing in Laravel is secure and shouldn't be something you have to worry about.
There really isn't a "right" way of doing things, it really depends on how the rest of your app works, how complicated it is, and how easy it should be to maintain. If it were me though, I would have a LoginController with a method for showing the view and a method for creating the user and have those methods respond to the request rather than putting everything right in your routes.php file.
You are also missing a {{ Form::close() }} at the end of your view as well.

Categories