Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13|Last
Lessons:-Views

Validating Uploads

Below is an example validation method you could define in your model to validate whether a file has been
successfully uploaded:

public function isUploadedFile($params) {
$val = array_shift($params);
if ((isset($val['error']) && $val['error'] == 0) ||
(!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')
) {
return is_uploaded_file($val['tmp_name']);
}
return false;
}

Creates a file input:

echo $this->Form->create('User', array('type' => 'file'));
echo $this->Form->file('avatar');

Will output:

<form enctype="multipart/form-data" method="post" action="/users/add">
<input name="data[User][avatar]" value="" id="UserAvatar" type="file">

Creating buttons and submit elements

FormHelper::submit(string $caption, array $options)
Creates a submit button with caption $caption. If the supplied $caption is a URL to an image
(it contains a ‘.’ character), the submit button will be rendered as an image.
It is enclosed between div tags by default; you can avoid this by declaring $options['div'] =
false:

echo $this->Form->submit();

Will output:

<div class="submit"><input value="Submit" type="submit"></div>

You can also pass a relative or absolute URL to an image for the caption parameter instead of caption
text.

echo $this->Form->submit('ok.png');

Will output:

<div class="submit"><input type="image" src="/img/ok.png"></div>

FormHelper::button(string $title, array $options = array())
Creates an HTML button with the specified title and a default type of “button”. Setting
$options['type'] will output one of the three possible button types:

  1. submit: Same as the $this->Form->submit method - (the default).
  2. reset: Creates a form reset button.
  3. button: Creates a standard push button.

echo $this->Form->button('A Button');
echo $this->Form->button('Another Button', array('type' => 'button'));
echo $this->Form->button('Reset the Form', array('type' => 'reset'));
echo $this->Form->button('Submit Form', array('type' => 'submit'));

Will output:

<button type="submit">A Button</button>
<button type="button">Another Button</button>
<button type="reset">Reset the Form</button>
<button type="submit">Submit Form</button>

The button input type supports the escape option, which accepts a bool and determines whether
to HTML entity encode the $title of the button. Defaults to false:

echo $this->Form->button('Submit Form', array(
'type' => 'submit',
'escape' => true
));

FormHelper::postButton(string $title, mixed $url, array $options = array ())
Create a <button> tag with a surrounding <form> that submits via POST.
This method creates a <form> element. So do not use this method in some opened form. Instead
use FormHelper::submit() or FormHelper::button() to create buttons inside opened
forms.

FormHelper::postLink(string $title, mixed $url = null, array $options = array ())
Creates an HTML link, but access the URL using method POST. Requires JavaScript to be enabled in
browser.
This method creates a <form> element. If you want to use this method inside of an existing form,
you must use the inline or block options so that the new form can be rendered outside of the
existing form.

If all you are looking for is a button to submit your form, then you should use
FormHelper::submit() instead

Changed in version 2.5: The inline and block options were added. They allow buffering the
generated form tag, instead of returning with the link. This helps avoiding nested form tags. Setting
'inline' => false will add the form tag to the postLink content block, if you want to use a
custom block you can specify it using the block option instead.

Creating date and time inputs

FormHelper::dateTime($fieldName, $dateFormat = ‘DMY’, $timeFormat = ‘12’, $attributes
= array())
Creates a set of select inputs for date and time. Valid values for $dateformat are ‘DMY’, ‘MDY’,
‘YMD’ or ‘NONE’. Valid values for $timeFormat are ‘12’, ‘24’, and null.
You can specify not to display empty values by setting “array(‘empty’ => false)” in the attributes
parameter. It will also pre-select the fields with the current datetime.

FormHelper::year(string $fieldName, int $minYear, int $maxYear, array $attributes)
Creates a select element populated with the years from $minYear to $maxYear. HTML attributes
may be supplied in $attributes. If $attributes['empty'] is false, the select will not include an
empty option:

echo $this->Form->year('purchased', 2000, date('Y'));

Will output:

<select name="data[User][purchased][year]" id="UserPurchasedYear">
<option value=""></option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
</select>

FormHelper::month(string $fieldName, array $attributes)
Creates a select element populated with month names:

echo $this->Form->month('mob');

Will output:

<select name="data[User][mob][month]" id="UserMobMonth">
<option value=""></option>
<option value="01">January</option>

<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

You can pass in your own array of months to be used by setting the ‘monthNames’ attribute, or have
months displayed as numbers by passing false. (Note: the default months are internationalized and
can be translated using localization.):

echo $this->Form->month('mob', array('monthNames' => false));

FormHelper::day(string $fieldName, array $attributes)
Creates a select element populated with the (numerical) days of the month.
To create an empty option with prompt text of your choosing (e.g. the first option is ‘Day’), you can
supply the text as the final parameter as follows:

echo $this->Form->day('created');

Will output:

<select name="data[User][created][day]" id="UserCreatedDay">
<option value=""></option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
...
<option value="31">31</option>
</select>

FormHelper::hour(string $fieldName, boolean $format24Hours, array $attributes)
Creates a select element populated with the hours of the day.
FormHelper::minute(string $fieldName, array $attributes)
Creates a select element populated with the minutes of the hour.
FormHelper::meridian(string $fieldName, array $attributes)
Creates a select element populated with ‘am’ and ‘pm’.

Displaying and checking errors

FormHelper::error(string $fieldName, mixed $text, array $options)
Shows a validation error message, specified by $text, for the given field, in the event that a validation error has occurred. 

Options:

  • escape’ bool Whether or not to HTML escape the contents of the error.
  • ‘wrap’ mixed Whether or not the error message should be wrapped in a div. If a string, will be used as the HTML tag to use.
  • ‘class’ string The class name for the error message

FormHelper::isFieldError(string $fieldName)
Returns true if the supplied $fieldName has an active validation error.

if ($this->Form->isFieldError('gender')) {
echo $this->Form->error('gender');
}

FormHelper::tagIsInvalid()
Returns false if given form field described by the current entity has no errors. Otherwise it returns the
validation message.

Setting Defaults for all fields

You can declare a set of default options for input() using FormHelper::inputDefaults().
Changing the default options allows you to consolidate repeated options into a single method call:

$this->Form->inputDefaults(array(
'label' => false,
'div' => false,
'class' => 'fancy'
)
);

All inputs created from that point forward will inherit the options declared in inputDefaults. You can override
the default options by declaring the option in the input() call:

echo $this->Form->input('password'); // No div, no label with class 'fancy'
// has a label element same defaults
echo $this->Form->input(
'username',
array('label' => 'Username')
);

 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)