0

1) I have an HTML Form that post all the gathered information(I.e when the user enters the details and clicks on submit )it will post to the to the Php file for processing

2) The problem is when i am not passing any value to the date Fields I am getting the below error.

Errors: For date fields: is not a valid value for the type xsd:date

How can i make my Post to accept the Null value for the fields and have a successfully submit.

Example:

The value passing from my form for Date Fields, if there is no value in the field is string(0) "".How can i stop this error and post it sucessfully.

Form:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <form class="form-horizontal" action="CandidateCreationPage.php" method="post" enctype="multipart/form-data">
<td class="label-cell"><label>First Name <span class="required-input letters-only">*</span></label></td>
 <td>
<input class="required-field letters-only" type="text" name="firstname" id="first-name" maxlength="25" />
</td>
 </tr>
 <tr>
 <td class="label-cell"><label>Last Name <span class="required-input">*</span></label></td>
<td>
<input class="required-field letters-only" type="text" name="lastname" id="last-name" maxlength="30" />
</td>
   </tr>
  <tr>
 <td class="label-cell"><label>When (If Applicable)</label></td>
<td>
<input type="date" name="workedpreviouslywhen" id="worked-previously-when" />
</td>
</tr>
<tr>
 <td class="label-cell"><label>Available to Start</label></td>
 <td>
<input type="date" name="availabletostart" id="available-to-start" />
 </td>
  </tr>
    </form>
    </body>
    </html>

Post PHP:

$fields = array (
'First_Name__c' => $_POST['firstname'],
'Last_Name__c' => $_POST['lastname'],
'Primary_contact_number__c'=>$_POST['primarycontactnumber'],
'Secondary_contact_number__c'=>$_POST['secondarycontactnumber'],
'Email__c'=> $_POST['email'],
'How_did_you_learn_of_this_Opportunity__c'=>$_POST['hearthisopportunity'],
'Please_Specify__c'=>$_POST['pleasespecify'],
'Status__c'=>$_POST['status'],
'Have_you_worked_with_us_before__c'=>$_POST['workedpreviously'],
**if (isset($_POST['workedpreviouslywhen']) || !ctype_space($_POST['workedpreviouslywhen']) || empty($_POST['workedpreviouslywhen'])) {
'When__c'=>$_POST['workedpreviouslywhen'],
}**
**if (isset($_POST['availabletostart']) || !ctype_space($_POST['availabletostart']) || empty($_POST['availabletostart'])) {
'Available_to_start__c'=>$_POST['availabletostart'],
}**
'Position__c'=>$queryResult->records[0]->Id[0],                                                 
'Street_Address_1__c'=>$_POST['streetaddressone'],
'Street_Address_2__c'=>$_POST['streetaddresstwo'],
'Province__c'=>$_POST['province'],
'City__c'=>$_POST['city'],
'Country__c'=>$_POST['country'],
'Postal_Code__c'=>$_POST['postalcode'],
'Currently_Employed__c'=>intval($_POST['currentlyemployed']),
'Current_Employer__c'=>$_POST['currentemployer'],
**if (isset($_POST['startdateone']) || !ctype_space($_POST['startdateone']) || empty($_POST['startdateone'])) {     'Start_Date_Work_Exp1__c'=>$_POST['startdateone'],
}**
**if (isset($_POST['enddateone']) || !ctype_space($_POST['enddateone']) || empty($_POST['enddateone'])) {
'End_Date_Work_EXP_End1__c'=>$_POST['enddateone'],
}**
'Rate_of_Pay_1__c'=>$_POST['rateofpayone'],
'Previous_Employer__c'=>$_POST['previousemployertwo'],
**if (isset($_POST['startdatetwo']) || !ctype_space($_POST['startdatetwo']) || empty($_POST['startdatetwo'])) {                             
'Start_Date_Prev_Work_1__c'=>$_POST['startdatetwo'],
}**
**if (isset($_POST['enddatetwo']) || !ctype_space($_POST['enddatetwo']) || empty($_POST['enddatetwo'])) {
'End_Date_Prev_Wor_END_1__c'=>$_POST['enddatetwo'],
}**
'Rate_of_Pay_2__c'=>$_POST['rateofpaytwo'],
'Reason_for_Leaving__c'=>$_POST['reasonforleavingtwo'],
'Previous_Employer_1__c'=>$_POST['previousemployerthree'],
**if (isset($_POST['startdatethree']) || !ctype_space($_POST['startdatethree']) || empty($_POST['startdatethree'])) {
'Start_Date_Prev_Work_2__c'=>$_POST['startdatethree'],
}**
**if (isset($_POST['enddatethree']) || !ctype_space($_POST['enddatethree']) || empty($_POST['enddatethree'])) {                                                 'End_Date_Prev_Wor_END_2__c'=>$_POST['enddatethree'],
}**
'Rate_of_Pay_3__c'=>$_POST['rateofpaythree'],
'Reason_for_Leaving_1__c'=>$_POST['reasonforleavingthree'],
'Higher_Education__c'=>$_POST['highereducation'],
'Licenses_Certfications__c'=>$_POST['licensesandcerts'],
);

1 Answer 1

0

From the sounds of things you want to do some PHP validation prior to posting? You'd want to do something like this, firstly getting your variables from $_POST[], validating things (in this instance, whether or not fields are empty), then if that's successful posting it.

You could do this using an errors array, adding errors if fields fail validation and then checking whether or not that array is empty. If it is, post the data, otherwise, display the errors.

Something like this?

$firstName = $_POST['firstname']; // Get variables
$lastName = $_POST['lastname'];
$errors = array(); // Create array

if ($_POST) { // Has the user clicked submit?
  if (empty($firstname) || empty($lastname)) { //Check if they're empty
    $errors[] = 'First and last name required!'; // Add error message to the errors array
  }

  if (empty($errors)) {
    // Post to Salesforce
  }
  else {
    foreach($errors as $error) {
      echo $error."<br><br>";
    }
  }
}

Hopefully that's a good enough explanation!

Edit Whilst this should work, it's not secure. You should look to performing other types of validation as well then match what you have in Salesforce. Field length, only accepting integers if it's a number field, only accepting URLs if it's a URL field etc...

Edit 2 From your comment:

The value passing from my form for Date Fields and currency fields if there is no value in the field

I would suggest one solution would be only to post data to Salesforce if it exists. You could do this a few ways, a simple way would be:

$fields = array (
  if (isset($_POST['firstname']) || !ctype_space($_POST['firstname']) || empty($_POST['firstname'])) {
    'First_Name__c' => $_POST['firstname'],
  }                                                                                                                                                                                                                                                                    
)

This is a pretty simple, long way of doing it. Firstly, isset($var) will check whether or not the variable is set and exists, ctype_space($var) will check whether or not the field is just white space. We use ! because we don't want it to contain just white space. However, this would return false if the field is empty hence we don't want anything empty.

Assuming all this passes, you'll then add that value to the $fields array.

There's cleaner ways of doing this, in fact you may even want to use a function, but this should avoid passing anything empty to Salesforce at all.

6
  • I am having the validation on the form .I maintaining two forms one for the user to fill and the other to post the Data to SF DC. For Example: In my form i put a validation saying if the field(Pick list ) value is yes then the field is mandatory in the form.but when the user submits my post is expecting this value and giving me the error.I have problem with only the Date and the currency fields
    – Donald
    Dec 23, 2015 at 16:11
  • The value passing from my form for Date Fields and currency fields if there is no value in the field is string(0) "" **.I am getting the error as **is not a valid value for the type xsd:date and for the currency fields is not valid for the type xsd:double.How can i stop this error and post it sucessfully
    – Donald
    Dec 23, 2015 at 16:43
  • @PraveenKumar see my edit.
    – Dan Jones
    Dec 24, 2015 at 9:20
  • I have implemented the solution said above .But I am getting the error ** Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' in C:\xampp\htdocs\Salesforce\CandidateCreationPage.php on line 78 ** .Please see the updated post.php in my question.
    – Donald
    Dec 24, 2015 at 15:09
  • can you please review the code i have provided above
    – Donald
    Jan 4, 2016 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .