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
Related
I am working with a dynamic form where its fields are attached to the dynamic variables as shown below.
<div class="form-group">
<label><?php echo display('name'); ?> *</label>
<div class="row">
<div class="col-sm-6">
<input name="<?php echo "firstname".$seat_list_arr[$i];?>" class="form-control" type="text" placeholder="First Name" id="name" value="<?php echo $this->session->userdata('firstname'); ?>">
</div>
<div class="col-sm-6">
<input name="<?php echo "lastname".$seat_list_arr[$i];?>" class="form-control" type="text" placeholder="Last Name" value="<?php echo $this->session->userdata('lastname'); ?>" id="lastname">
</div>
</div>
</div>
<div class="form-group">
MY QUESTION is how to access these dynamic form names by using the post method in code igniter.
'firstname'=> $this->input->post("firstname".$seat_list_arr[$i]),
'lastname'=> $this->input->post("lastname".$seat_list_arr[$i]),
I tried this method but the post method reads nothing.
I will appreciate your help thank you in advance.
why do not use names as array ?
for example in view <input name="firstname[?<php echo $seat_list_arr[$i]] ?>"
then use it as a normal post variable in the controller. by the code $this->input->post('firstname'); it will return an array to you that you can use a foreach loop to determine exact index you want.
as #ViChel said we need to find how do you work on $i and $seat_list_arr by the way I hope this would help you
I am seeking some more advice on the way to handle this.
I have got one page with links to each admin member which when clicked takes their display name over. On the second page which is a form it takes that display names and populates the subject field with their display name. I need to grab the email address that is associated to that user too but use that as the email address the form on the second page gets sent to as currently my script can only send it to an email address I hard code into it.
So page one is:
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
foreach ($committee as $user) {
echo '
<a href="../contact-form?displayname=' . $user->display_name . '"><b style="font-size:18px;">
<tr>
<td style="padding: 10px;">' .$user->job_title .' - </td>
<td style="padding: 10px;">' .$user->display_name .'</td>
</tr></b></a><br><br>';
}
?>
Page two is:
<?php $displayname = $_GET['displayname'];?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
And my send script has my email hard coded in as:
$mail->From = 'myemail#dummy.com';
So I need that be variable depending on which person you clicked on in the first place. It needs to be sent to both my hard coded email and also the persons email that is associated to them in the Wordpress user database.
Based on our comment discussion, you should be able to do something along the lines of the following on page two. Be sure to correct my email_address, I'm not sure if that's how get_users returns the email address or not.
<?php
$displayname = $_GET['displayname'];
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
$matchingEmail = false;
foreach ($committee as $user) {
if ( !$matchingEmail && $user->display_name == $displayname ) {
// great, we found our match
$matchingEmail = $user->email_address; // I don't know if email_address is right, double check this and modify if necessary
}
}
if ( $matchingEmail ) {
// only proceed if a matching email address is found
?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname; ?>">
<input type="hidden" name="matchingEmail" value="<?php echo $matchingEmail; ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
<?php
} else {
?>
<p>Something is wrong, I can't find your email address! Please try again.</p>
<?php
}
?>
Finally on page three, where you send the email, you can do something like:
<?php $mail->addAddress(stripslashes( $_POST["matchingEmail"] ) ); ?>
CONTROLLER FUNCTION CODE:
$surveydata = $this->getsurveydatabasedonId($SurveyId);
$this->load->view('editsurvey', array('surveydata' => $surveydata));
VIEW CODE:
<h1 class="page-header">Edit Survey</h1>
<?php
$att = array('id' => 'editsurvey_form', 'role' => 'ajax-form');
echo form_open('welcome/editsurvey', $att);
?>
<div class="span5 offset1" id="form_div">
<?php echo validation_errors('<div class="alert alert-danger reg_error">', '</div>'); ?>
<div class="form-group control-group warning">
<input name="SurveyId" style="display:none" type="text" class="form-control" data-validation-required-message="Please enter a Survey Id" id="SurveyId" required placeholder="Please enter the Survey Id" value="<?php echo set_value('SurveyId', $surveydata->SurveyId); ?>">
<label for="SurveyTitle">Survey Title</label>
<input name="SurveyTitle" type="text" class="form-control" data-validation-required-message="Please enter a Survey Title" id="SurveyTitle" required placeholder="Please enter the Survey Title" value="<?php echo set_value('SurveyTitle', $surveydata->SurveyTitle); ?>">
<p class="help-block"></p>
</div>
<div class="form-group control-group warning">
<label for="SurveyIntroduction">Survey introduction</label>
<textarea name="SurveyIntro" type="text" class="form-control" id="SurveyIntro" placeholder="Enter the Survey Introduction"><?php echo set_value('SurveyIntro', $surveydata->SurveyIntro); ?></textarea>
</div>
<button type="submit" class="btn btn-large btn-warning">Edit Survey</button>
</div>
</form>
I am getting the following errors :
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/editsurvey.php
Line Number: 11
I am passing the array but it isn't working could someone help me please?
Please make sure,
You have data in your $surveydata.
If you are fetching results through
$resultSet->result() OR $resultSet->row(), then use $surveydata->SurveyTitle
And if you are fetching the results through:
$resultSet->result_array() OR $resultSet->row_array(), then use $surveydata['SurveyTitle]
Also, please make sure to check count of $surveydata
It should be $surveydata[0]['SurveyId'], not $surveydata->SurveyId
Same for SurveyTitle
It should be $surveydata[0]['SurveyTitle'], not $surveydata->SurveyTitle
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
if i want to changes the tag and style of all the elements in formhelper with cakephp 2.x
example
$this->Form->input("name")
generate:
<div class="input number">
<label for="AfiliadoCedula">Cedula</label>
<input name="data[Afiliado][cedula]" type="number" value="12" id="AfiliadoCedula">
<div class="error-message">name error</div>
</div>
change for:
<div class="input number **myclassfieldset**">
<label for="AfiliadoCedula" class="**myclasslabel**">Cedula</label>
<input name="data[Afiliado][cedula]" type="number" class="**myclassinput**" value="12" id="AfiliadoCedula">
<**label** class="error-message **myclasserror**">name error</**label**>
</div>
Thnks for your help a lot!!
$this->Form->input("name", array(
'div' => array('class' => '**myclassfieldset**'),
'label' => array('class' => '**myclasslabel**'),
'class' => '**myclassinput**'
));